小编rob*_*kuz的帖子

内联时扩展方法出错

我想扩展一些系统类型,然后通过内联使用它们

type System.String with  
    member this.foo n = this + "!" + n 

type System.Boolean with  
    member this.foo n = sprintf "%A!%A" this n 
Run Code Online (Sandbox Code Playgroud)

现在我调用这些扩展方法

let x = "foo".foo "bar"
let y = true.foo "bar"
Run Code Online (Sandbox Code Playgroud)

这给了我这个

- val x : System.String = "foobar"
- val y : string = "true!"bar""
Run Code Online (Sandbox Code Playgroud)

所有罚款和花花公子 - 但现在我想将调用包装.foo成内联函数

let inline foo n v = (^T : (member foo : ^N  -> ^S) v, n)
let z = foo "bar" "baz" 
Run Code Online (Sandbox Code Playgroud)

只是现在我收到编译错误告诉我

> The …
Run Code Online (Sandbox Code Playgroud)

f#

4
推荐指数
1
解决办法
110
查看次数

推杆断开和超时

我的服务器会收到有关客户端断开连接的通知吗?如果断开连接发生如下,我认为答案是肯定的.

pusher.disconnect()
Run Code Online (Sandbox Code Playgroud)

但是,如果用户只是关闭浏览器会发生什么?

另一件事是有一种方法通知服务器一段时间某个通道尚未被客户端使用?

pusher

3
推荐指数
1
解决办法
4104
查看次数

GroovyShell().parse 传递参数

我有一个 groovy 脚本,需要从外部 groovy 脚本解析一个类。我不确定如何传递参数。这是有效的:

我正在运行的 Groovy 脚本使用这一行来解析 external.groovy 中的外部类:

new GroovyShell().parse(new File('External.groovy'))
Run Code Online (Sandbox Code Playgroud)

这是 external.groovy 的样子:

class External {
    public external() {
        println "Hello"
    }
}
Run Code Online (Sandbox Code Playgroud)

有用。

我遇到的问题,我找不到将参数传递给外部方法的方法。这是 external.groovy 的样子:

class External {
    public external(String name) {
        println name
    }
}
Run Code Online (Sandbox Code Playgroud)

如何向正在运行的脚本添加参数:

new GroovyShell().parse(new File('external.groovy')) //need to include the 'Name' parameter to this
Run Code Online (Sandbox Code Playgroud)

groovy groovyshell

3
推荐指数
1
解决办法
3817
查看次数

F#带有curried函数的类型推断

我有以下代码

let bar foo baz = foo, baz 

let z = bar 3
let z1 = z 2
Run Code Online (Sandbox Code Playgroud)

但如果我注释掉最后一行,let z1 = z 2我会收到一个错误

let z = bar 3
----^

stdin(78,5): error FS0030: Value restriction. 
The value 'z' has been inferred to have generic type
val z : ('_a -> int * '_a)    
Either make the arguments to 'z' explicit or, if you do not intend for it to be generic, 
add a type annotation.
Run Code Online (Sandbox Code Playgroud)

我完全迷失了如何正确地注释函数.

f# types type-inference

3
推荐指数
1
解决办法
82
查看次数

如何组合2个任意实例以匹配测试方法签名

我有一个函数,应该得到两个实际参数进行测试.这两个值都应该由任意实例创建,因为它们需要具有一些不能完全随意的良好形式.
所以我创建了以下代码

let updating (x:SomeType) (y:SomeOtherType) =
    let result = update x y
    result.someProp = x.someProp
    && result.otherProp = y.otherProp

let arbSomeType = 
    Arb.generate<SomeType> 
        |> Gen.filter fun x -> x.checkSomeStuff
        |> Arb.fromGen

let arbSomeType = 
    Arb.generate<SomeOtherType> 
        |> Gen.filter fun x -> x.checkPropertiesOfThis
        |> Arb.fromGen
Run Code Online (Sandbox Code Playgroud)

但是,我现在如何组合这两个任意实例,以便它们与测试方法的签名相匹配?

//let prop = Prop.forAll arbSomeType + arbSomeType updating

Check.QuickThrowOnFailure prop
Run Code Online (Sandbox Code Playgroud)

f# fscheck

3
推荐指数
1
解决办法
93
查看次数

我可以在F#中表达具有类型约束的泛型类型的递归关系吗?

给出以下类型

type WorkflowStep<'next, 'prev, 'cancel> =
     abstract member Next : unit -> 'next
     abstract member Prev : unit -> 'prev
     abstract member Cancel : unit -> 'cancel
Run Code Online (Sandbox Code Playgroud)

我想表达的事实'next,'prev并且'cancel也应该是类型WorkflowStep或类型的unit是这可能与F#的类型级别编码?

f#

3
推荐指数
1
解决办法
82
查看次数

如何摆脱这种"无法概括"的错误?

我有这个接口声明

type IModel<'value, 'search, 'target when 'target :> IModel<'value, 'search, 'target>> =
    abstract token: string with get
    abstract value: 'value with get
    abstract search: 'search with get
    abstract GetEmpty: unit -> 'target
    abstract ReInitWith:  #IModel<_, 'search, _> -> 'target

type IModelSimple<'value, 'search> =
    inherit IModel<'value, 'search, IModelSimple<'value, 'search>>
    abstract Update:  ?token:string * ?value: 'value * ?search: 'search -> IModelSimple<'value, 'search>
Run Code Online (Sandbox Code Playgroud)

和这个创建对象表达式的函数

let rec mkModelSimple<'value, 'search> vctor sctor token value search =
    {
        new IModelSimple<'value, 'search> with
            member this.token = token …
Run Code Online (Sandbox Code Playgroud)

f#

3
推荐指数
1
解决办法
119
查看次数

扩展歧视工会

是否可以使用 mappend/conditional 类型来转换这个 DU

type MyDU =
| {kind: 'foo'}
| {kind: 'bar'}

type Transformed = DUTransformer<MyDU>
Run Code Online (Sandbox Code Playgroud)

这样我们就得到了以下结果

type Transformed =
| {kind: 'foo', foo: boolean}
| {kind: 'bar', bar: boolean}
Run Code Online (Sandbox Code Playgroud)

typescript mapped-types

3
推荐指数
1
解决办法
131
查看次数

如何使用映射类型表达“至少一个现有属性并且没有其他属性”

请原谅我稍微长一点的标题。

给定以下类型

type A = {
    foo: string;
    bar: number;
    baz: boolean;
}
Run Code Online (Sandbox Code Playgroud)

我喜欢创造一个新的“部分”B型

type B = Partial<A>
Run Code Online (Sandbox Code Playgroud)

这样 B 必须至少包含 A 的属性之一,并且只允许 A 的属性

//compiles
const b1 = {
   foo: "yeah"
}

//errors
const b2 = {}
const b3 = {lala: "lala"}
const b4 = {foo: "foo is alowed", but_not_this: false}
Run Code Online (Sandbox Code Playgroud)

typescript mapped-types

3
推荐指数
1
解决办法
569
查看次数

确保记录类型中文字值的唯一性

给出以下代码

const VALUES = {
    field1: "fieldA",
    field2: "fieldB",
} as const


export type RecToDU<T> = {
    [K in keyof T]: T[K]
}[keyof T]

type VALUESLiterals = RecToDU<typeof VALUES>
Run Code Online (Sandbox Code Playgroud)

这会正确产生

type VALUESLiterals = "fieldA" | "fieldB"
Run Code Online (Sandbox Code Playgroud)

现在我想检查我的类型中的文字值是否都是唯一的,以便

const VALUE = {
    field1: "fieldA",
    field2: "fieldB",
    field3: "fieldA" // "fieldA" is again a literal value
} as const

type VALUESLiterals = RecToDU<typeof VALUES>
Run Code Online (Sandbox Code Playgroud)

现在将产生never结果而不是

type VALUESLiterals = "fieldA" | "fieldB"
Run Code Online (Sandbox Code Playgroud)

as als 包含已由 定义的field3文字值。因此,如果存在重复的文字值,则整个类型应该是fieldAfield1never

typescript mapped-types

3
推荐指数
1
解决办法
846
查看次数