digitalmars.D.learn - Does D have object wrappers for primitives?
- stunaep (4/4) Jul 29 2016 I have some java code I need to convert and at one point it uses
- Cauterite (8/12) Jul 29 2016 No, but with a template you could easily make your own:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (5/17) Jul 29 2016 I was going to suggest Algebraic because it allows arrays of mixed
- Cauterite (3/7) Jul 29 2016 It could work, but keep in mind Algebraic is a structure, not an
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/15) Jul 29 2016 Also, I've later noticed that your Box was a class, so it would allow
- stunaep (7/19) Jul 29 2016 Thank you. This is just what I needed. I am curious though as to
- Cauterite (6/12) Jul 30 2016 auto s = new immutable(Boxed!string)(`foo`);
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.
Jul 29 2016
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);
Jul 29 2016
On 07/29/2016 01:25 PM, Cauterite wrote:On Friday, 29 July 2016 at 20:13:34 UTC, stunaep wrote:I was going to suggest Algebraic because it allows arrays of mixed primitive types (wrapped in Algebraic): https://dlang.org/phobos/std_variant.html#.Algebraic AliI 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);
Jul 29 2016
On Friday, 29 July 2016 at 20:26:47 UTC, Ali Çehreli wrote:I was going to suggest Algebraic because it allows arrays of mixed primitive types (wrapped in Algebraic): https://dlang.org/phobos/std_variant.html#.Algebraic AliIt could work, but keep in mind Algebraic is a structure, not an object.
Jul 29 2016
On 07/29/2016 01:40 PM, Cauterite wrote:On Friday, 29 July 2016 at 20:26:47 UTC, Ali Çehreli wrote:Also, I've later noticed that your Box was a class, so it would allow "arrays of mixed primitive types" as well. Yes, Algebraic is not a struct but Java not having structs doesn't mean that the original code really needed classes either. :) AliI was going to suggest Algebraic because it allows arrays of mixed primitive types (wrapped in Algebraic): https://dlang.org/phobos/std_variant.html#.Algebraic AliIt could work, but keep in mind Algebraic is a structure, not an object.
Jul 29 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: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.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);
Jul 29 2016
On Saturday, 30 July 2016 at 04:12:45 UTC, stunaep wrote: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.auto s = new immutable(Boxed!string)(`foo`); what it's saying is that the box itself needs to be immutable. Honestly I don't know why it was even possible to make a mutable box in the first place, when the only constructor is marked `immutable`.
Jul 30 2016