fsprojects / FSharp.UMX

F# units of measure for primitive non-numeric types
MIT License
162 stars 10 forks source link

Using UMX in literal values #15

Open piaste opened 4 years ago

piaste commented 4 years ago

Is it possible to create a non-numeric UMX value explicitly, without going through %, so that it will be a valid constant expression?

I need them to be a literal so they can be used as arguments to an attribute, like this:

[<Measure>] type private Role

type RoleName = string<Role>

type RequireRoleAttribute([<ParamArray>] roles : RoleName[]) = 
    inherit AuthorizeAttribute(Roles = String.concat "," (Array.map (~%) roles))

module AccountRoles = 

    // syntax error
    let Admin : RoleName = "admin"<Role>

    // not a constant expression (after the operator is defined)
    let [<Literal>] Admin : RoleName = "admin" * 1<Role>

    // not a constant expression
    let [<Literal>] Admin : RoleName = ("admin" : FSharp.UMX.string<Role>)
eiriktsarpalis commented 4 years ago

I don't believe this is possible at all.