question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enum type is lost when used in attribute

See original GitHub issue

Description Enum type is lost (casted to int32) when used in attribute named argument of type obj.

http://stackoverflow.com/questions/35732830/how-to-preserve-value-type-in-enum

Repro Steps Create attribute with named argument of type obj. Add that attribute and assign enum value to named argument.

type MyAttribute() =
    inherit Attribute()

    let mutable prop = null

    member this.Prop
        with get(): obj = prop
        and  set(value) = prop <- value


type MyEnum = 
    | A = 1
    | B = 2

[<My(Prop = MyEnum.B)>]
type MyClass = class
    end

let test () =
   let t = typeof<MyClass>
   let a = t.GetCustomAttributes(false).[0] :?> MyAttribute

   let e = a.Prop
   Convert.ToString(e, Globalization.CultureInfo.CurrentCulture)

Expected test function should return "B". In IL value should be stored as enum.

Actual test function returns 2. In IL value is stored as int32 - enum type is lost

Version: Microsoft (R) F# Compiler version 14.0.23413.0

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dsymecommented, Oct 3, 2019

Yes, it can be considered a bug, though it’s fix might need an RFC

The translation from Enums to constants is done too early. IIRC the only reason I didn’t fix it was a concern that quotations actually see the constant, not the field, and because it would mean storing more information in the TAST which we have to do very carefully w.rt. compat.

We can fix this, but it may need a new quotation node that refines “Const” - that is, the node will continue to match “BasicPatterns.Const” but there is an additional “BasicPatterns.EnumConst” or something that sees this refined information.

0reactions
cartermpcommented, Oct 3, 2019

@dsyme to comment on the labeling, I personally think it’s a bug

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - Enum type not defined at runtime
I would understand it if I had used the declare keyword, telling the compiler that something should be available at runtime, but in...
Read more >
Using Enums and Custom Attributes
The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator...
Read more >
Enumeration Types in Data Contracts - WCF
One way to use enumeration types in the data contract model is to apply the DataContractAttribute attribute to the type.
Read more >
enum — Support for enumerations
Base class for creating enumerated constants that can be combined using the bitwise operators without losing their IntFlag membership. IntFlag members are ...
Read more >
Using EnumAttr : missing or conflicting information - MLIR
There seems to be conflicting or missing information on how EnumAttr should be used successfully. I read through whatever code and write-ups ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found