hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

F# and enum types in .net

Last post 03-12-2008, 1:47 by ivan.eryshov. 7 replies.
Sort Posts: Previous Next
  •  07-17-2006, 2:56 428

    F# and enum types in .net

    How to combine values from enumeration types in F#? I playing around with programming Windows Forms in F#, and I need to set control anchor styles. In C# or C++ it is done like this:

    button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);

    How to set this up in F#? Is there an operator defined to combine enum values?
  •  07-17-2006, 3:50 430 in reply to 428

    Re: F# and enum types in .net

    Use Enum.combine, e.g. fom a different example exercising the .NET reflection library:

    open System.Reflection
    let callStaticMethod (ty:Type) name args =
      ty.InvokeMember(name,
        Enum.combine
         [ BindingFlags.InvokeMethod;
           BindingFlags.Static;
           BindingFlags.Public;
           BindingFlags.NonPublic ], null, null, args)

    Cheers!

    Don

  •  07-17-2006, 3:50 431 in reply to 428

    Re: F# and enum types in .net

    What you need is "Enum.combine", see below link for more details:

    http://research.microsoft.com/fsharp/manual/mllib/Microsoft.FSharp.MLLib.Enum.html


    Robert Pickering, MVP
    http://strangelights.com
  •  03-10-2008, 12:13 5261 in reply to 431

    Re: F# and enum types in .net

    And how about such code:

    enum RuleTarget {

       Class = 0x0001, Delegate = 0x0002, Enum = 0x0004, Struct = 0x0008, 

       AllTypes = Class | Delegate | Enum | Struct,

    }

    ?

    Nemerle provides such ability in that way:

    enum RuleTarget {

       | Class = 0x0001 | Delegate = 0x0002 | Enum = 0x0004 | Struct = 0x0008

       | AllTypes = Class %| Delegate %| Enum %| Struct

    }

  •  03-11-2008, 9:07 5282 in reply to 5261

    Re: F# and enum types in .net

    In F# enums can be create using the following syntax (note you must explicitly give the members a value to distinguish it from a union type):

    [<System.Flags>]
    type RuleTarget =
       | Class = 0x0001
       | Delegate = 0x0002
       | Enum = 0x0004
       | Struct = 0x0008

    It's seems for the moment you can't reference enum members from there definition so you can't create a value "AllTypes" as a member (unless you work out its value). As a work around you can create a sperate value for all types:

    let AllTypes = RuleTarget.Class ||| RuleTarget.Delegate ||| RuleTarget.Enum ||| RuleTarget.Struct

    If this is really important to you send a feature request to the F# team, they've generally been quite good about small feature requests like this.


    Robert Pickering, MVP
    http://strangelights.com
  •  03-11-2008, 10:56 5287 in reply to 5282

    Re: F# and enum types in .net

    Robert:

    It's seems for the moment you can't reference enum members from there definition so you can't create a value "AllTypes" as a member (unless you work out its value). As a work around you can create a sperate value for all types:

    let AllTypes = RuleTarget.Class ||| RuleTarget.Delegate ||| RuleTarget.Enum ||| RuleTarget.Struct

    I think you'll agree that this is not the same. Actually, I don't see any value of AllTypes when it is not an enum's member.

    Robert:

    If this is really important to you send a feature request to the F# team, they've generally been quite good about small feature requests like this.

    Important - is a wrong word for this. But, it is hell of a useful. I'll follow your advice and send a request ;)

  •  03-12-2008, 1:20 5295 in reply to 5287

    Re: F# and enum types in .net

    ivan.eryshov:
    Robert:

    It's seems for the moment you can't reference enum members from there definition so you can't create a value "AllTypes" as a member (unless you work out its value). As a work around you can create a sperate value for all types:

    let AllTypes = RuleTarget.Class ||| RuleTarget.Delegate ||| RuleTarget.Enum ||| RuleTarget.Struct

    I think you'll agree that this is not the same. Actually, I don't see any value of AllTypes when it is not an enum's member.

    I don't think I said it was the samething, I said it was a work arround and okay not a very good one ;). A better one is maybe to use F# binary litterals which make it a doodle to work flags:

    [<System.Flags>]
    type RuleTarget =
       | Class =    0b00000001
       | Delegate = 0b00000010
       | Enum =     0b00000100
       | Struct =   0b00001000
       | AllTypes = 0b00001111

     


    Robert Pickering, MVP
    http://strangelights.com
  •  03-12-2008, 1:47 5296 in reply to 5295

    Re: F# and enum types in .net

    Robert:
    A better one is maybe to use F# binary litterals which make it a doodle to work flags:


    Yep! That what I'm doing. Thank you Robert!
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems