C# should support "const ref"

  • Thread starter Thread starter not_a_commie
  • Start date Start date
N

not_a_commie

A method parameter declared as "const ref" would allow for passing
large structs quickly and enforce that the struct does not get
reassigned. I know there was concern before about the inability of
such a device to enforce that the members don't get set. I don't care
about that. Let them assign the members (all of which I've declared
readonly.) I've started making most of my structs immutable. They're
just so nice to use that way. I just wish that I could pass them into
methods without such a hard hit and at the same time avoid the fear
that my coworkers might reassign them without my permission.
 
Not a bad idea. Have you submitted it to the Product Feedback site? A
link to your entry would be helpful, so people can vote on it.

Don't be surprized if there is resistance (from inside MS) to this, as
this would require a CLR change (I can't think of a way to do it through
"compiler magic") and history has shown that they are resistant to that (the
CLR hasn't changed since .NET 2.0).
 
not_a_commie said:
A method parameter declared as "const ref" would allow for passing
large structs quickly and enforce that the struct does not get
reassigned. I know there was concern before about the inability of
such a device to enforce that the members don't get set. I don't care
about that. Let them assign the members (all of which I've declared
readonly.) I've started making most of my structs immutable. They're
just so nice to use that way. I just wish that I could pass them into
methods without such a hard hit and at the same time avoid the fear
that my coworkers might reassign them without my permission.

Do you really need such large structs in the first place? Just make
them classes (keeping them immutable) and you'll be fine.
 
not_a_commie said:
A method parameter declared as "const ref" would allow for passing
large structs quickly and enforce that the struct does not get
reassigned. I know there was concern before about the inability of
such a device to enforce that the members don't get set. I don't care
about that. Let them assign the members (all of which I've declared
readonly.) I've started making most of my structs immutable. They're
just so nice to use that way. I just wish that I could pass them into
methods without such a hard hit and at the same time avoid the fear
that my coworkers might reassign them without my permission.

How would you implement the compiler? It's hard, even before you
consider unmanaged code. To construct a pretty hard example:

public void Problem2(const ref MyStruct s)
{
Modifier(__makeref(s));
}

private void Modifier(TypedReference tr)
{
__refvalue(tr, MyStruct) = new MyStruct();
}

Alun Harford
 
not_a_commie said:
A method parameter declared as "const ref" would allow for passing
large structs quickly and enforce that the struct does not get
reassigned. I know there was concern before about the inability of
such a device to enforce that the members don't get set. I don't care
about that. Let them assign the members (all of which I've declared
readonly.) I've started making most of my structs immutable. They're
just so nice to use that way. I just wish that I could pass them into
methods without such a hard hit and at the same time avoid the fear
that my coworkers might reassign them without my permission.

I think the thought process was that const being static would conflict with
ref's variable change.
 
Chizl said:
I think the thought process was that const being static would conflict with
ref's variable change.

Well, that's just a matter of naming. It could easily have been
"readonly" instead of "const".
 
I think the thought process was that const being static would conflict with
Well, that's just a matter of naming. It could easily have been
"readonly" instead of "const".

I like readonly as well. C++ uses const so I thought we might as well
go with something familiar.
 
not_a_commie said:
I stuck this on MS Feedback: #322420

Sadly, not likely to happen.

All the C++ developers are already pissed at having const-correctness
broken, and Microsoft already knows it quite well (some of their own
developers blogged about it IIRC). But const-correctness is something you
can't retrofit.
 
Back
Top