Inherited mutability, freeze, thaw and more in Rust

bearophile bearophileHUGS at lycos.com
Thu Jul 26 07:54:58 PDT 2012


Another post from the Rust language blog:

http://smallcultfollowing.com/babysteps/blog/2012/07/24/generalizing-inherited-mutability/

 From the post:

>This is a powerful idea but quite beyond Rust’s type system, 
>and I am interested in exploring solutions that lead to similar 
>expressiveness while avoiding the quagmire of dependent types.<

Adding dependent types to Rust increases the language complexity, 
but it also offers very good capabilities that only very few 
languages like ATS have.


A first part, "More background: inherited mutability" is about a 
problem that I have discussed a little in this newsgroup. So it 
seems it wasn't a so naive and useless topic.

Rust immutability seems better thought out/designed than D 
immutability, but Rust design is not finished yet. Rust type 
system is much more powerful than D type system, but maybe it's 
also a little harder to understand.

In Rust even if a struct (record) field is not mutable, you are 
allowed to replace the contents of a mutable variable that 
contains one of such struct instances.

It means it accepts code similar to this one, because inherits 
mutability:

struct Foo {
     immutable int x;
     int y;
}
void main() {
     Foo f = Foo(5, 10);
     f = Foo(20, 30);
}


This is quite handy, because in D once a struct has an immutable 
field, you can't do a lot with it, you can't reassign, etc. A D 
class instance with one immutable field doesn't have such 
problems:

Doing this is of course not accepted in Rust:

void main() {
     immutable Foo f = Foo(5, 10);
     f = Foo(20, 30);
}

Bye,
bearophile


More information about the Digitalmars-d mailing list