[nem-en] Initonly modifier for immutable fields
Kamil Skalski
kamil.skalski at gmail.com
Sun Jul 9 21:07:46 CEST 2006
Yap, I agree. The only difference for structs should be the way they
are passed to other functions. But the real life is a bit harder and
structs in .NET are painful in a few aspects, like in connection with
generics:
http://www.interact-sw.co.uk/iangblog/2005/01/27/modifyinsitu
Anyway, our problem here is quite specific and is connected with the
fact that PEVerify forbids getting ANY address of initonly valutype
field. I don't really understand this restriction, because it should
be easy to verify that I only use this address to call methods /
access inner fields.
On 7/9/06, Snaury <snaury at gmail.com> wrote:
> On 7/9/06, Michal Moskal <michal.moskal at gmail.com> wrote:
> > The solution here is to avoid mutator methods on structs. It is the
> > best for structs to be immutable. This is also the advice given by the
> > C# folks, that I've read somewhere. If you're using mutable structs,
> > you deserve punishment.
>
> Sorry for getting in, but here is where you might be crossing the line
> and strip features from CLI. The difference between class and struct
> is that class is passed by reference, and struct is passed by value.
> There is nothing bad in struct being mutable, I myself often use
> structs in place of classes when I know for sure that I won't need to
> pass it somewhere that will change it (this is already guaranted by
> the fact it is passed by value, it some function changes it, it
> changes it local copy). The methods on structs, however, still take
> the reference to struct and should be able to change it, it is
> especially useful when you don't expose fields and provide properties
> instead. One of the most important field is, for example, P/Invoke.
> The might be dozens of fields in a struct which you don't want to init
> via constructor. Or the might be just a hidden uint value where you
> expose its parts as properties (I did that when I was writing wrapper
> for Uniscribe). It is to important for structs to be able to be
> mutable, so I don't want this to be just stripped.
>
> _______________________________________________
> https://nemerle.org/mailman/listinfo/devel-en
>
--
Kamil Skalski
http://nazgul.omega.pl
More information about the devel-en
mailing list