Sto riscontrando un problema nel far funzionare la DU come previsto. Ho definito un nuovo DU che o ha un risultato di tipo < 'a> o qualsiasi eccezione derivato da System.ExceptionF # Numero di tipo Unione discriminato
open System
// New exceptions.
type MyException(msg : string) = inherit Exception(msg)
type MyOtherException(msg : string) = inherit MyException(msg)
// DU to store result or an exception.
type TryResult<'a, 't> =
| Result of 'a
| Error of 't :> Exception
//This is fine.
let result = Result "Test"
// This works, doing it in 2 steps
let ex = new MyOtherException("Some Error")
let result2 = Error ex
// This doesn't work. Gives "Value Restriction" error.
let result3 = Error (new MyOtherException("Some Error"))
non riesco a capire il motivo per cui mi permette di creare un 'errore', se Lo faccio in 2 passaggi, ma quando sto facendo la stessa cosa su una singola riga, ottengo un errore di restrizione del valore.
Cosa mi manca?
Grazie
UPDATE
Guardando il post di @kvb, aggiungendo informazioni sul tipo ogni volta che ho bisogno di creare un errore sembrava un po 'prolisso, così ho avvolto in su in un ulteriore metodo che crea un errore ed è un po 'più sintetico.
// New function to return a Result
let asResult res : TryResult<_,Exception> = Result res
// New function to return an Error
let asError (err : Exception) : TryResult<unit,_> = Error(err)
// This works (as before)
let myResult = Result 100
// This also is fine..
let myResult2 = asResult 100
// Using 'asError' now works and doesn't require any explicit type information here.
let myError = asError (new MyException("Some Error"))
io non sono sicuro se specificando un errore con 'unità' avrà alcuna conseguenza non ho ancora previsti.
TryResult<unit,_> = Error(err)
Grazie. Questo ha senso. Ho aggiornato la mia domanda aggiungendo un altro metodo di creazione degli errori che sembra funzionare correttamente e lo mantiene un po 'più ordinato. L'unica domanda è che il tipo è ora specificato come TryResult Non sono sicuro se questo avrà alcun down-side. –
Moog