-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow tuple syntax to be used with consts #1466
Comments
How are you expecting this to be encoded in IL? The CLR only supports a small subset of types for proper literal constants. The C# compiler also fakes supporting |
@HaloFour That's the point of this allowing only literals. It doesn't have to reach IL. It's like current |
Constant fields are encoded in IL as they can then be referenced by other code in the current (or other) projects. For reference: public const int I = 123;
public const string S = "123"; is compiled to:
and: public const decimal D = 123.4m; is compiled to:
|
Then how would you use a |
@HaloFour Ahhh, that makes sense. I can't believe I never thought about that! I was thinking that How about this: (I'm not sure this will exactly work, but I think it's progress) const (int foo, int bar) myTuple = (1, 2);
// converts to:
[TupleConstAttribute(Name="myTuple")]
// special name to avoid conflicts
const int __foo = 1;
// or instead of an attribute, maybe include the tuple name in the name
const int <bar>k__myTuple = 2; So, in IL:
Or, I'm not very familiar with IL but if it is possible, make the field edit: @svick see line 1 of this comment |
Why is being const really desirable here? I'm not a fan of const in general across public APIs as it has pretty impactful versioning constraints. Why not just use a "static readonly"? |
@CyrusNajmabadi because we can have |
If it's silly why do it? :) Basically, for something to be a language feature it has to be really useful. The system you've described above seems like a lot of work for very little gain. |
@CyrusNajmabadi I mean, in my case might be silly, but there may be useful use cases. |
@HaloFour I put together a little something. I read that generic attributes are valid in IL (edit: C# support is also coming - #124 ), so I think the 2nd option is the better one: private const (int, int) foo = (1, 2); will compile to
or
|
Maybe. That encoding could work for a To @CyrusNajmabadi point, why isn't a And yes, |
What good would a constant ValueTuple serve when ValueTuples are inherently mutable? |
@yaakov-h A |
what @jnm2 said. Indeed, it goes beyond that. A readonly value is not mutable. ValueTuple's are just a special case of that. |
@CyrusNajmabadi To nitpick unnecessarily, I might call a type mutable even if the values of its fields themselves are readonly, if it contained a reference to a mutable object. Im/mutability would then be in the deep sense of the word, as opposed to the shallow immutability of |
Fair distinction! |
OK so here's a different way of looking at this: So my proposal isn't allowing |
static class constants {
public const int foo;
} ? |
@Thaina Thanks, that proposal includes |
edit: See #144 instead.
Original post:
The text was updated successfully, but these errors were encountered: