Why can ValueTuple not be const?
Asked Answered
M

1

7

ValueTuple types, declared as fields, can be mutable:

class Foo {
  (int, int) bar = (0, 1);
}

or readonly:

class Foo {
  readonly (int, int) bar = (0, 1);
}

and this (im)mutability applies to each member. I would expect it to be stretched to const declarations as well:

class Foo {
  const (int, int) bar = (0, 1);
}

But this statement does not compile. Is there a certain case where it is an undesirable feature, or is it just something not implemented?

EDIT OK, I understand now. My question was based on the assumption that the C# compiler treats ValueTuples differently than other types, regarding the keyword readonly. So, if it is already an exception, why not make another exception for consts. But, in fact, this logic seems to be applied to all structs. So this will not work:

class Foo {
  readonly ExampleStruct field;
  void Method() {
    field.structField = 2;
  }
}
struct ExampleStruct {
  public int structField;
}
Mackinaw answered 21/4, 2019 at 20:10 Comment(4)
Const relies on a specific feature of the CLR called literal fields. These are only supported for certain primitive types, int, double, string, etc. Note that one of these is NOT decimal, however C# allows decimal constants. So it must be possible to do on some level, but my guess it is not worth the work to implement since much of the same functionality is offered through static readonly fields (however these behave differently in some cases).Kunin
Decimals are handled using attributes. I think the attribute(s) supported is hardcoded into the compiler so that "trick" cannot be used by anything other than decimals.Francoise
This topic was the discussion of issue #1034 on dotnet/csharplang.Maffick
@MikeZboray I don't think in this case any support from CLR would be required (for sure, if we only use allowed types in valuetuple). And comparison with other structs is not really relevant, because we know exactly how valuetuple created, unlike some custom struct. Actually, I shouldn't have mentioned this in the question.Mackinaw
M
6

There is a limited set of types, which could be used as const in c# (more detailed here).

Value tuple is simply not among them.

Motivity answered 21/4, 2019 at 20:16 Comment(5)
Your answer is correct, however, originally I meant something different, I explaned it in my edit.Mackinaw
@AlexButenko The code you posted in the update doesn't even use const. Was there a purpose to this question at all? Or is this more like "Well, see, I originally meant to ask why apples are round but then accidentally asked why aliens seems to be portrayed as bipeds in most movies"?Francoise
@LasseVågsætherKarlsen I think my edit explains everything. My question was based on wrong understanding of something. When I found it, I realized that the whole question does not make same sense as I originally meant. And in this sense this answer would be valid. I apologize for asking questions here. What do you want me to post in the edit? This is what I discovered, I didn't know this, and I could find this even in C# specification. I thought it can be useful for someone who didn't know this, too. And it explains my motivation to as the question.Mackinaw
I'm sorry, I tried to post a humorous comment to your comment. I simply did not understand why you asked about const when your code clearly did not use const in any way. I still don't know what your question is, though, so if you're still wondering why your code (that you posted in the edit) doesn't behave or compile the way you expected, I'm sure there is a good question in there, somewhere. If you're still wondering, perhaps you should try to reword your question and post it? I'm sure you'd get an answer to it.Francoise
@LasseVågsætherKarlsen No, the edit is not part of the question. It is kind of answer.Mackinaw

© 2022 - 2024 — McMap. All rights reserved.