Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 非抽象属性的类型批注_F#_Properties_Types_Annotations - Fatal编程技术网

F# 非抽象属性的类型批注

F# 非抽象属性的类型批注,f#,properties,types,annotations,F#,Properties,Types,Annotations,关于如何添加类型注释来修复此错误,有什么想法吗 我在getFooBar中的Foo.Bar下得到一个红色的波形,并显示以下错误消息 基于此程序点之前的信息查找不确定类型的对象。在此程序点之前可能需要类型注释来约束对象的类型。这样可以解决查找问题 [<AllowNullLiteralAttribute>] type Test(foo : Test, bar : int) = let getFooBar(test : Test) = test.Foo.Bar

关于如何添加类型注释来修复此错误,有什么想法吗

我在getFooBar中的Foo.Bar下得到一个红色的波形,并显示以下错误消息

基于此程序点之前的信息查找不确定类型的对象。在此程序点之前可能需要类型注释来约束对象的类型。这样可以解决查找问题

[<AllowNullLiteralAttribute>]
type Test(foo : Test, bar : int) =

    let getFooBar(test : Test) =
        test.Foo.Bar 

    member this.Foo with get() = foo
    member this.Bar with get() = bar
[]
类型测试(foo:Test,bar:int)=
让getFooBar(测试:测试)=
test.Foo.Bar
使用get()=Foo成员this.Foo
使用get()=Bar成员this.Bar

刚刚尝试创建一个匿名函数,它可以正常工作

let getFooBar(test : Test) =
    test.Foo |> fun (x:Test) -> x.Bar

有更好的方法吗?

刚刚尝试了一个匿名函数,它就工作了

let getFooBar(test : Test) =
    test.Foo |> fun (x:Test) -> x.Bar

有更好的方法吗?

我不明白为什么需要这样做,但只需对
Foo的结果进行注释即可
似乎有效:

[<AllowNullLiteralAttribute>]
type Test(foo : Test, bar : int) =
    let getFooBar(test : Test) =
        (test.Foo : Test).Bar

    member this.Foo with get() = foo    
    member this.Bar with get() = bar
[]
类型测试(foo:Test,bar:int)=
让getFooBar(测试:测试)=
(test.Foo:test).Bar
使用get()=Foo成员this.Foo
使用get()=Bar成员this.Bar

我不明白为什么需要这样做,但只需对
Foo的结果进行注释即可
似乎有效:

[<AllowNullLiteralAttribute>]
type Test(foo : Test, bar : int) =
    let getFooBar(test : Test) =
        (test.Foo : Test).Bar

    member this.Foo with get() = foo    
    member this.Bar with get() = bar
[]
类型测试(foo:Test,bar:int)=
让getFooBar(测试:测试)=
(test.Foo:test).Bar
使用get()=Foo成员this.Foo
使用get()=Bar成员this.Bar

您也可以只注释
Foo
属性的类型:

type Test(foo, bar : int) = 
   let getFooBar(test : Test) = 
       test.Foo.Bar
   member this.Foo with get() : Test = foo
   member this.Bar with get() = bar

您也可以只注释
Foo
属性的类型:

type Test(foo, bar : int) = 
   let getFooBar(test : Test) = 
       test.Foo.Bar
   member this.Foo with get() : Test = foo
   member this.Bar with get() = bar

F#的类型推理系统有时会受到脑损伤。有趣的是,在您的示例中,您现在可以在构造函数中删除类型注释,它仍然有效:type Test(foo,bar:int)。F#的类型推理系统有时会造成大脑损伤。有趣的是,在您的示例中,您现在可以在构造函数中删除类型注释,它仍然有效:type Test(foo,bar:int)。我将成员this.Foo与get()=Foo:TestI放在一起,看我做错了什么。我将成员this.Foo与get()=Foo:Test放在一起