Inline function and type extension

本小妞迷上赌 提交于 2019-12-18 04:53:02

问题


Consider I have two different library types:

type Foo = { foo : string }
type Bar = { bar : int32 }

I want to implement generic function zoo that will work for either Foo or Bar instances. And I cannot change Foo and Bar because they are part of library code.

Here's my first attempt using type extensions and inline function as explained here:

// Library.fs
module Library

type Foo = { foo : string }
type Bar = { bar : int32 }

// Program.fs
type Foo with
    static member zoo (f : Foo) = "foo"

type Bar with
    static member zoo (b : Bar) = "bar"

let inline zoo (x : ^t) =
    (^t : (static member zoo : ^t -> string) x)

let f = zoo { foo = "1" } // error FS0001: The type 'Foo' does not support the operator 'zoo'

Why don't inline function definition relies on type extensions? How could I solve my problem without changing of the initial Foo and Bar type definitions?


回答1:


Use method overload.

The problem with extension methods is that they are not taken into account when solving member constraints .

So you can use method overload, as shown already in your own answer or you can go further and create an inline generic function by using an intermediate type and an intermediate method (in this case an operator for simplicity) to do the trick:

type T = T with
    static member ($) (T, x:Foo) = "foo"
    static member ($) (T, x:Bar) = "bar"

let inline zoo x = T $ x

let f = zoo { foo = "1" }

Here you have more details about how this works.

Be aware that this function will be inlined, so for instance you won't be able to call it from C#, if this is required don't use a function, use simple and standard method overload.




回答2:


The best thing I could get so far is

type Ext =
    static member zoo (f : Foo) = "foo"
    static member zoo (b : Bar) = "bar"

let f = Ext.zoo { foo = "1" } // "foo"
let b = Ext.zoo { bar = 2 } // "bar"

It is not the best and not very generic solution but at least it works.



来源:https://stackoverflow.com/questions/32913955/inline-function-and-type-extension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!