开发者

Type annotation for none abstract property

开发者 https://www.devze.com 2022-12-11 09:58 出处:网络
Any ideas on how I could add type annotation to fix this error? I开发者_运维问答\'m getting a red squiggly under Foo.Bar in getFooBar and the following error message for it.

Any ideas on how I could add type annotation to fix this error?

I开发者_运维问答'm getting a red squiggly under Foo.Bar in getFooBar and the following error message for it.

Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.

[<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


I do not understand why this is needed but simply annotate the result of Foo seems to work:

[<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


You can also just annotate the type of the Foo property:

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


Just tried making an anonymous function and it works.

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

Is there a better way?

0

精彩评论

暂无评论...
验证码 换一张
取 消