2016-12-29 30 views
3
type TotalSizeToken = TotalSizeToken with 
    static member TotalSize(_: TotalSizeToken, (a,b,c,d,e)): int = a*b*c*d*e 
    static member TotalSize(_: TotalSizeToken, (a,b,c,d)): int = a*b*c*d 
    static member TotalSize(_: TotalSizeToken, (a,b,c)): int = a*b*c 
    static member TotalSize(_: TotalSizeToken, (a,b)): int = a*b 
    static member TotalSize(_: TotalSizeToken, x: int): int = x 

let inline size_to_total_size x = 
    ((^s) : (static member TotalSize: TotalSizeToken * ^s -> int) TotalSizeToken, x) 

let t = size_to_total_size (1,5) // Error: Expecting a type supporting the operator TotalSize, but given a tuple type. 

我没有想到size_to_total_size中的x参数需要TotalSize成员。这感觉就像一个编译器错误。如何获得具有可变数量参数的整数元组的产品?

我不知道如何让这个工作。有任何想法吗?

回答

8

你错过了其他的“帽子”类型:

let inline size_to_total_size x = 
    let call (t:^T) = ((^s or ^T) : (static member TotalSize: TotalSizeToken * ^s -> int) t, x) 
    call TotalSizeToken 

必须有两个人,正在传递的参数,并表示包含重载类中的一个。

否则,它不会查看类,只能在元组中找到,而元组类型中没有这样的方法。

1
type TotalSizeToken<'u> = TotalSizeToken of 'u with 
    static member TotalSize(x: TotalSizeToken<int*int*int*int*int>): int = 
     match x with TotalSizeToken (a,b,c,d,e) -> a*b*c*d*e 
    static member TotalSize(x: TotalSizeToken<int*int*int*int>): int = 
     match x with TotalSizeToken (a,b,c,d) -> a*b*c*d 
    static member TotalSize(x: TotalSizeToken<int*int*int>): int = 
     match x with TotalSizeToken (a,b,c) -> a*b*c 
    static member TotalSize(x: TotalSizeToken<int*int>): int = 
     match x with TotalSizeToken (a,b) -> a*b 
    static member TotalSize(x: TotalSizeToken<int>): int = 
     match x with TotalSizeToken x -> x 

let inline size_to_total_size x = 
    ((^s) : (static member TotalSize: ^s -> int) x) 

let t = size_to_total_size (TotalSizeToken(1,5)) 

这种替代形式的作品。有一些额外的拳击,但从好的方面来说,我不需要到处传递lambda。奇怪的是,如果size_to_total_size被写为let inline size_to_total_size x = ((^s) : (static member TotalSize: TotalSizeToken< ^s> -> int) x),那么它不起作用。

相关问题