Calls to struct methods and immutable
Kenji Hara
k.hara.pg at gmail.com
Thu Nov 15 19:43:24 PST 2012
On Thursday, 15 November 2012 at 23:40:16 UTC, Joseph Rushton
Wakeling wrote:
> On 11/15/2012 07:48 PM, Ali Çehreli wrote:
>> Do you have a reference type in your struct?
>
> Assuming I do, what can I do to ensure the struct instance is
> immutable? Is cast(immutable) now the only option?
This code works with dmd git head (and might work with dmd 2.060).
struct Foo
{
int a, b;
string[string] aa;
}
immutable(Foo) makeFoo() pure
{
Foo foo;
foo.aa["a"] = "hello";
foo.aa["b"] = "world";
return foo;
// compiler allows this implicit casting from Foo to
immutable(Foo),
// because compiler can guarantee the instance 'foo' doesn't
have
// mutable indirections to any global data so makeFoo is a
pure function.
}
void main()
{
immutable ifoo = makeFoo();
}
Kenji Hara
More information about the Digitalmars-d-learn
mailing list