Permit usage of attribute constructors with nullable versions of literal-able structs #63304
Replies: 4 comments 9 replies
-
Your understanding of how The rules for CA encoding could probably be expanded if need be, but as they are now, you can't encode |
Beta Was this translation helpful? Give feedback.
-
see dotnet/csharplang#4814 dotnet/csharplang#2878 dotnet/csharplang#688 |
Beta Was this translation helpful? Give feedback.
-
I think the current scenario around nullable value types and Attributes are a mess. There is no compiler warning or error (or analyzer) for putting nullable value type properties in an Attribute. It will just silently fail until such time when users go to use the attribute (since such properties cannot be assigned to). We produce a library for others to consume that includes attributes and have been bitten by this repeatedly. Compounding the issue is we usually can't even fix such bugs easily, because changing the type of attribute after the fact is a binary compatibility change. Nullable value types have been in C# since 2.0. They are a fundamental part of C# and work seamlessly everywhere else in the language. They absolutely should be expressible in Attributes. |
Beta Was this translation helpful? Give feedback.
-
Shouldn't this be moved to the runtime repository? |
Beta Was this translation helpful? Give feedback.
-
Currently, using an attribute constructor with
int?
or similar is prohibited. Specifically, for all the available constant value types, the compiler will prohibit their usage (byte
,sbyte
,short
,ushort
,int
,uint
,long
,ulong
,float
,double
,bool
,char
, and any enum type).Since the nullable versions of the structs that support constant literals are also literals, this could be enabled as a feature.
From this answer on StackOverflow:
This justifies the current restriction, however it could be worked around. Since the metadata supports holding instances of the types
object
,string
,Type
and arrays of any other allowed types, a reference to the constant value could also be held to support those types as well. I'm assuming that those reference types are stored as pointers to the addresses that the values are held, in which case any other reference type could be permitted, for long as it would support a constant expression representing it.Side-effects
decimal
could also see a bright day.const int? a = 4;
is currently unavailable).Beta Was this translation helpful? Give feedback.
All reactions