Does D have object wrappers for primitives?

stunaep via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 29 21:12:45 PDT 2016


On Friday, 29 July 2016 at 20:25:16 UTC, Cauterite wrote:
> On Friday, 29 July 2016 at 20:13:34 UTC, stunaep wrote:
>> I have some java code I need to convert and at one point it 
>> uses an Object[] array to store various ints, longs, and 
>> strings. Java has built in Integer and Long classes that wrap 
>> the primitives in an object and strings are already objects.
>
> No, but with a template you could easily make your own:
>
> class Boxed(T) {
> 	T _v;
> 	alias _v this;
> 	this(in T v) immutable {_v = v;};
> };
>
> auto i = new Boxed!int(6);

Thank you. This is just what I needed. I am curious though as to 
why this doesn't work with strings. It would work if I removed 
immutable from the Boxed constructor but I thought strings were 
immutable. I get a compiler error 'not callable using a mutable 
object'. Even marking a string with the immutable keyword has the 
same result.


More information about the Digitalmars-d-learn mailing list