2016-06-21 84 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

相關問題