【发布时间】:2015-07-29 18:06:37
【问题描述】:
我有一个受歧视工会的层次结构。
type SpecificNoun =
| Noun
| NounPhrase
| Pronoun
| PosesivePronoun
type SpecificModifier =
| Adverb //slowly, quickly, verb + ly (90% of the time)
| Preposition //off, on, together, behind, before, between, above, with, below
type SpecificVerb =
| ActionVerb
| BeingVerb
| PossesiveVerb
| TransitiveVerb
type PartsOfSpeech =
| Noun of SpecificNoun
| Verb of SpecificVerb
| Adjective
| Punctuation
| Modifier of SpecificModifier
我需要将一个字符串翻译成其中之一,但它必须是 PartOfSpeech 以便我可以在匹配案例中使用它。以下代码无法编译。
let StringToPartOfSpeech (part:string) =
match part with
| "Noun" -> SpecificNoun.Noun
| "NounPhrase" -> SpecificNoun.NounPhrase
| "Pronoun" -> SpecificNoun.Pronoun
| "PossessivePronoun" -> SpecificNoun.PosesivePronoun
| "Adverb" -> SpecificModifier.Adverb
这是一个与此相关的问题:F# - Can I return a discriminated union from a function 然而,就我而言,一切都只是直接歧视的工会
【问题讨论】:
-
我建议更改名称,以便“名词”不会在不同类型中使用两次。阅读起来可能会令人困惑,并且还会混淆类型推断。如果案例名称是唯一的,则不需要
SpecificNoun.限定符。
标签: f#