2013-02-27 136 views
7

我试图定义一个字符串的通用转换操作符来枚举,我想用这样的:F#类型约束

let day = asEnum<DayOfWeek>("Monday") 

但是这个实现:

let asEnum<'a, 'b when 'a: (new : unit -> 'a) and 'a : struct and 'a :> ValueType and 'a : enum<'b>> text = 
    match Enum.TryParse<'a>(text) with 
    | true, value -> Some value 
    | false, _ -> None 

我只能用这样的:

let day = asEnum<DayOfWeek,_>("Monday") 

或本:

​​

如果我从类型约束省略'a : enum<'b>干脆,我可以拥有它,因为我想,但后来如果有人不指定类型,则默认为int,我真的不喜欢,我'd喜欢它给编译时错误,就像我指定约束时一样错误

也许有任何技巧只指定一个类型参数,并让另一个类型参数被传递?有任何想法吗?

回答

2

不幸的是,为了增加它似乎你必须拼了这一切的约束: (如KVB指出,就可以避免通过添加'T : enum<int>约束的角度括号外上复制TryParse的约束)

这也适用:

let asEnum<'T 
    when 'T : enum<int> 
    and 'T : struct 
    and 'T :> ValueType 
    and 'T : (new : unit -> 'T)> text = 
    match Enum.TryParse<'T>(text) with 
    | true, value -> Some value 
    | _ -> None 

这给出了一个编译时错误,如果基础类型不int

type ByteEnum = 
    | None = 0uy 

asEnum<ByteEnum> "None" //ERROR: The type 'int' does not match the type 'byte' 
3

这个怎么样?

let asEnum s :'a option when 'a:enum<'b> = 
    match System.Enum.TryParse s with 
    | true, v -> Some v 
    | _ -> None 

// works, but warns that type params shouldn't be given explicitly 
asEnum<System.Reflection.BindingFlags,_> "DeclaredOnly"  
// also okay 
(asEnum "DeclaredOnly" : System.Reflection.BindingFlags option) 
+0

圣牛。我甚至不知道这是有效的语法。我想如果你把它改成“a:enum ”,那会给他他想要的东西。他也可以使用'let e:System.Reflection.BindingFlags option = asEnum“DeclaredOnly”'来避免警告。 – Daniel 2013-02-27 15:55:33

+0

为什么这个工作,但在'<' '>'之间放置相同的约束不? – Daniel 2013-02-27 15:57:46

+0

@丹尼尔 - 我不认为@ovastus希望'int'被迫,他希望在可能的情况下推断它(它是)。 – kvb 2013-02-27 16:13:21

0

我想另一件事是这样的:

type AsEnum = 

    static member Get<'a, 'b when 'a: (new : unit -> 'a) and 'a : struct and 'a :> ValueType and 'a : enum<int>> (text:string) = 

     match Enum.TryParse<'a>(text) with 
     | true, value -> Some value 
     | _ -> None 

    static member Get<'a, 'b when 'a: (new : unit -> 'a) and 'a : struct and 'a :> ValueType and 'a : enum<int64>> (text:string) = 

     match Enum.TryParse<'a>(text) with 
     | true, value -> Some value 
     | _ -> None 

let a = AsEnum.Get<BindingFlags>.Get "DeclaredOnly" 

尝试看看如果我能得到编译器来推断其超载打电话,但如果失败,多义性错误

0

一小更新的东西样3年后^ _^

使用此字符串扩展字符串转换为一个枚举

type System.String with 
     /// Strongly-typed shortcut for Enum.TryParse(). 
     member this.ToEnum<'a when 'a :> System.Enum and 'a : struct and 'a : (new: unit -> 'a)>() = 
      let ok, v = System.Enum.TryParse<'a>(this, true) 
      if ok then Some v else None  

请注意您的枚举声明。

type failingEnum = 
      | FirstValue 
      | SecondValue 
      | AnotherValue 

type compliantEnum = 
      | FirstValue = 0 
      | SecondValue = 1 
      | AnotherValue = 2 

然后

let x = "whatever".ToEnum<failingEnum>(); 
//- will give error failingEnum is not compatible with the type System.Enum 

let x = "whatever".ToEnum<compliantEnum>(); 
//- will succeed ! 
+1

从技术上讲,'failingEnum'是联合类型,而不是枚举。这就是它失败的原因。 – CaringDev 2016-01-27 12:50:33

+0

当然...感谢您的评论。这就是为什么我添加了评论,因为人们经常将枚举定义为union。我是那些不久前的那些人的一部分:) – 2016-01-27 15:36:49