2016-06-21 82 views
2

给这个以下类型和其apply如何定义类型参数列表?

type Result<'TSuccess, 'TError> = 
    | Success of 'TSuccess 
    | Error of 'TError 
    with 
    member this.apply (fn:'a) : 'b = 
     match (fn, this) with 
     | Success(f), Success(x) -> Success(f x) 
     | Error(e), Success(_) -> Error(e) 
     | Success(_), Error(e) -> Error(e) 
     | Error(e1), Error(e2) -> Error(List.concat [e1;e2]);; 

成员实现我得到这样的警告(等等)

member this.apply (fn:'a) : 'b = 
     -----------^^^^ 
    /Users/robkuz/stdin(385,12): warning FS0064: This construct causes code to 
    be less generic than indicated by the type annotations. The type variable 
    'TError has been constrained to be type ''a list'. 

而这个错误

type Result<'TSuccess, 'TError> = 
-----------------------^^^^^^^ 
/Users/robkuz/stdin(381,24): error FS0663: This type parameter has been used 
in a way that constrains it to always be ''a list' 

我试图将其更改为

type Result<'TSuccess, 'TError list> = 

type Result<'TSuccess, List<'TError>> = 

两个给我一个语法错误。

我能做些什么来解决这个问题?

+2

'| | 'TError list'错误? – ildjarn

+0

这应该如何工作?看起来好像'fn'应该是'Result <'a ->'b,'TError list>'?但是您已经将类型指定为“a”。 – Lee

回答

4

正如@ildjarn所说的,您需要更改您的Error案例的定义。不过,这会给你更多关于'b的警告。最好的事情还真是去掉所有类型的注释,并让F#做的工作:

type Result<'TSuccess, 'TError> = 
    | Success of 'TSuccess 
    | Error of 'TError list 
    with 
    member this.apply fn = 
     match fn, this with 
     | Success f, Success x -> Success (f x) 
     | Error e, Success _ -> Error e 
     | Success _, Error e -> Error e 
     | Error e1, Error e2 -> Error (List.append e1 e2) 

我认为,如果你写多一点你打算使用这种类型做这将帮助 - apply函数的类型为Result<('TSuccess->'a),'TError>->Result<'a,'TError>这是否意味着:在成功的情况下,你从一个源获得一个函数,一个来自另一个源的值,并且你将一个应用到另一个源?

+0

试图通过内联实现应用程序 – robkuz

相关问题