www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting the mutable version of a type

reply Magnus Lie Hetland <magnus hetland.org> writes:
I'm writing a template for generating data of some possibly immutable 
type -- e.g., a string. What I'm wondering is, is there some way of 
accessing the mutable version of an immutable type?

I mean, I could do something like (for strings and related immutable arrays)...

void func(T)(ref immutable(T)[] arg) {
    T[] res;
    ...
    arg = cast(immutable(T)[]) res;
}

But it would be useful to be able to write the same template for both 
mutable and immutable types, and just use something like

void func(T)(ref T arg) {
    mutable(T) res;
    ...
    arg = cast(T) res;
}

Is there something like that? (E.g., a template in Phobos or 
something.) I guess I could do a match with an is() expression to 
extract the type, perhaps.


-- 
Magnus Lie Hetland
http://hetland.org
Mar 02 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/02/2012 02:18 AM, Magnus Lie Hetland wrote:
 I'm writing a template for generating data of some possibly immutable
 type -- e.g., a string. What I'm wondering is, is there some way of
 accessing the mutable version of an immutable type?

Yes, std.traits.Unqual: http://dlang.org/phobos/std_traits.html#Unqual import std.stdio; import std.traits; void foo(T)() { Unqual!T temp; writeln(typeof(temp).stringof); } void main() { foo!(immutable int)(); } Ali
Mar 02 2012
parent Magnus Lie Hetland <magnus hetland.org> writes:
On 2012-03-02 11:23:20 +0000, Ali Çehreli said:

 On 03/02/2012 02:18 AM, Magnus Lie Hetland wrote:
 I'm writing a template for generating data of some possibly immutable
 type -- e.g., a string. What I'm wondering is, is there some way of
 accessing the mutable version of an immutable type?

Yes, std.traits.Unqual: http://dlang.org/phobos/std_traits.html#Unqual

Aah -- awesome. Thanks! -- Magnus Lie Hetland http://hetland.org
Mar 02 2012