F#泛型不那么通用

Cli*_*int 2 generics f# c#-to-f#

我几次反对这一点,但我真的不知道为什么会发生这种情况.

我有一个受歧视的联盟,如:

type MStep<'A, 'B> =
| Shuttle of Quotations.Expr<'B> * Quotations.Expr<'B>
Run Code Online (Sandbox Code Playgroud)

工会还有更多,但这显示了基本问题.

如果我做:

let s1 = Shuttle(<@ l.SomeIntProp @>, <@ r.SomeIntProp @>)
let s2 = Shuttle(<@ l.SomeStrProp @>, <@ r.SomeStrProp @>)
Run Code Online (Sandbox Code Playgroud)

我收到编译器错误:

该表达式应该具有int类型,但这里有类型字符串

同样,如果我以其他顺序创建它们(字符串然后是int),我得到相同的错误,但反过来.

我可以看到编译器可能会'B根据我的用法进行推断,但是如果我想要'B真正的通用呢?


根据要求,这是一个更完整的例子:

type MStep<'A, 'B> =
    | Shuttle of Quotations.Expr<'B> * Quotations.Expr<'B>
    | Ident of Quotations.Expr<'B>
    | Trans of Quotations.Expr<'A> * Quotations.Expr<'B> * ('A -> 'B)

let doMig (f:Table<'A>, t:Table<'B>, s:('A * 'B -> MStep<'C, 'D> list)) =
    ignore()

let a = doMig(bpdb.Adjustments, ndb.Adjustments, (fun (l,r) ->
    [
        Shuttle(<@ l.Id @>, <@ r.Id @>)
        Shuttle(<@ l.Name @>, <@ r.Name @>)
    ]
    ))
Run Code Online (Sandbox Code Playgroud)

这会产生编译器错误,如上所示.

注意:

bpdb并且ndbSqlDataConnection类型提供程序提供的数据库上下文.

打开的命名空间是:

open System
open System.Data
open System.Data.Linq
open Microsoft.FSharp.Data.TypeProviders
open Microsoft.FSharp.Linq
open System.Xml
open System.Xml.Linq
open Microsoft.FSharp.Quotations.Patterns
open System.Reflection
open System.Diagnostics
Run Code Online (Sandbox Code Playgroud)

Joh*_*mer 7

这个问题很明显:

let t = [ //inserted t to have a concrete variable
    Shuttle(<@ l.Id @>, <@ r.Id @>); 
    Shuttle(<@ l.Name @>, <@ r.Name @>)
]
Run Code Online (Sandbox Code Playgroud)

究竟是什么类型的t.第一个元素给出MStep<_,int> list,第二个元素给出MStep<_,string>不同的元素.

您只能将类型相同的元素放入列表中.