2
私は一般的には新しいプログラマーであり、F#も同様です。私はこの問題に何度もぶつかってきましたが、私の意見ではまだ効率的に解決していません。いくつかのオプションタイプをチェックしてからタイプに変換してください
私はこれらの例の種類があります:ここに問題がある
type Retail1 = | Fashion | Auto | Sports
type Wholesale1 = | Fashion | Auto | Sports
type Events1 = | Wedding | Birthday
type Product =
| Retail of Retail1 | Wholesale of Wholesale1 | Events of Events1
| NoProduct
私は機能を経由して製品タイプに最初の3種類の可能性を変換したい:
let convertToProduct (retail: Retail1 option)
(wholesale: Wholesale1 option) (events: Events1 option) =
// convert to Product here
if retail.IsSome then Retail retail
elif wholesale.IsSome then Wholsale wholseale
elif events.IsSome then Events events
else NoProduct
方法私がパスでそれを処理したのは、それぞれの条件をチェックして最終的なタイプのProductを返すためにelifステートメントを一緒に連鎖させることですが、これは正しいとは思わないか、F#では少なくとも慣用的です。この問題に対する推奨されるアプローチは何でしょうか?
'convertToProduct(Some(Retail1.Fashion))None(Some(Wedding))'のような入力をすることは可能でしょうか?そうであれば、あなたのタイプを再考する必要があります。 –
興味深い:[機能タイプについての推論](http://tomasp.net/blog/types-and-math.aspx/) –