Immutability and other attributes, please review

Jonathan M Davis jmdavisProg at gmx.com
Thu Jun 14 09:42:54 PDT 2012


On Thursday, June 14, 2012 18:32:18 Roman D. Boiko wrote:
> On Thursday, 14 June 2012 at 16:24:43 UTC, Jonathan M Davis wrote:
> > On Thursday, June 14, 2012 17:32:03 Roman D. Boiko wrote:
> >> I don't know how to put a variable of type float to the heap
> > 
> > auto f = new float;
> > *f = 2.1;
> > 
> > - Jonathan M Davis
> 
> Except for immutable I would need to cast when passing into a
> function. That's dangerous, given that *f might be changed later.
> But looks like Timon's suggestion from my other question should
> work.
> 
> immutable a = [2.1].ptr;

Yeah. That'll probably work. I wish that you could do

auto f = new float(2.1);

and therefore

auto f = new immutable(float)(2.1);

but you can't. You _can_ do

auto f = new float;
*f = 2.1;
immutable g = cast(immutable float*)f;

and that's safe, because there are no other references to the data, but it's 
certainly not all that desirable. The slightly more idomatic way to do it is

auto f = new float;
*f = 2.1;
immutable g = std.exception.assumeUnique(f);

But it's doing the same thing and you have to worry about the same safety 
issues.

However, if Timon's suggestion works, that's great.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list