www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Auto-Boxing

reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
A question, I'm not sure if it's been asked:

Is Auto-Boxing a Good Thing or a Bad Thing ?
And if it's good, should be available in D ?


* With boxing, you can write stuff like:

     int i;
     Integer o = i;

And have the compiler create an object.


* Without boxing, you instead just get a type error:

"cannot implicitly convert expression i of type int to Integer"

And have to spell the object creation out for it:

     Integer o = new Integer(i);

Makes it more readily apparent that a new object is
being created, but takes a couple of extra keystrokes ?


Would it be good to have the first code map into the second ?
(so that when you assign a primitive to an object, it "boxes")

Note that D already supports auto-*un*boxing, with opCast():

     i = cast(int) o;

Trying to do same cast thing in the other direction, cast(Integer),
stuffs the address i in the reference and it goes South from there.


What do you think?
--anders


PS. The class code was:

 class Integer
 {
   public:
     this(int i) { value = i; }
     int opCast() { return value; }
   private:
     int value;
 }

Oct 15 2004
next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Anders F Björklund wrote:

 A question, I'm not sure if it's been asked:
 
 Is Auto-Boxing a Good Thing or a Bad Thing ?
 And if it's good, should be available in D ?

Before we can possibly have this, we'd need standard wrapper classes for the various types. But what do we need standard wrapper classes for? <snip>
 Note that D already supports auto-*un*boxing, with opCast():
 
     i = cast(int) o;

That isn't *auto*-unboxing. It's using the cast operator to manually unbox. Stewart.
Oct 15 2004
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Stewart Gordon wrote:

 Before we can possibly have this, we'd need standard wrapper classes for 
 the various types.  But what do we need standard wrapper classes for?

I'm not sure. The issue just arose when discussing boolean and strings? In Java, they're just used to pass around Objects to the Collection API and other such things. But I guess you could just as well just use the primitive types and instead apply templating ? Probably faster, too...
 That isn't *auto*-unboxing. It's using the cast operator to manually 
 unbox.

Right. Got my terminology a little confused there. Unboxing, not auto. So I guess using a primitive type constructor is manually boxing then? It's not that I miss all those Java objects for Strings and Arrays much. --anders
Oct 15 2004
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Anders F Björklund wrote:
<snip>
 In Java, they're just used to pass around Objects to the Collection API
 and other such things. But I guess you could just as well just use the
 primitive types and instead apply templating ? Probably faster, too...

AIUI the main reason for wrapper classes in Java was so that they can be put into containers, in a language that lacks templates. Now Java 1.5 has templates, but only classes are allowed as parameter types. I'm guessing that this is down to some implementation constraint in how the Java VM works. The wrapper classes remain both to support legacy code and for use in the new templates. If these wrapper classes weren't needed for templates, then there probably wouldn't have been any point in inventing auto-(un)boxing this late in Java's life. OTOH, Java allows primitive types to be template parameters, and so chances are we don't really need wrapper classes. <snip>
 Right. Got my terminology a little confused there. Unboxing, not auto.
 So I guess using a primitive type constructor is manually boxing then?

Primitive type constructor? You mean wrapper class constructor? Stewart.
Oct 15 2004
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Stewart Gordon wrote:

 Primitive type constructor?  You mean wrapper class constructor?

Yeah, "wrapper class constructor that takes a primitive type argument".
 class Integer
 {
   public:
     this(int i) { value = i; }

Integer o = new Integer(i); --anders
Oct 15 2004
prev sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Stewart Gordon wrote:

 OTOH, Java allows primitive types to be template parameters, and so 
 chances are we don't really need wrapper classes.

I think you meant "D" instead of Java here ? If you did, I agree :-) Wrapper classes are evil. Especially when used for things like String. If everything should be an object, there are plenty of other languages. (Smalltalk, Objective-C, Java, Ruby). No wrapper classes => No boxing. --anders
Oct 15 2004
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Correction:
 If everything should be an object, there are plenty of other languages.
 (Smalltalk, Objective-C, Java, Ruby). No wrapper classes => No boxing.

Ehrm, Objective-C is just "C and Smalltalk". I meant C# and Python :-) --anders
Oct 15 2004
prev sibling parent reply "Matthew" <admin.hat stlsoft.dot.org> writes:
"Anders F Björklund" <afb algonet.se> wrote in message
news:ckp37m$2clu$2 digitaldaemon.com...
 Stewart Gordon wrote:

 OTOH, Java allows primitive types to be template parameters, and so chances
are we don't really need wrapper classes.

I think you meant "D" instead of Java here ? If you did, I agree :-) Wrapper classes are evil. Especially when used for things like String.

IMO, Walter's single greatest point of genius vision was in the definition of slices. As with so many other things from D, I'm busy at the moment implementing slices for C++. Without garbage collection it is, as you can imagine, somewhat more difficult than it is in D. :-)
Oct 15 2004
parent reply Derek <derek psyc.ward> writes:
On Sat, 16 Oct 2004 07:12:18 +1000, Matthew wrote:

 "Anders F Björklund" <afb algonet.se> wrote in message
news:ckp37m$2clu$2 digitaldaemon.com...
 Stewart Gordon wrote:

 OTOH, Java allows primitive types to be template parameters, and so chances
are we don't really need wrapper classes.

I think you meant "D" instead of Java here ? If you did, I agree :-) Wrapper classes are evil. Especially when used for things like String.

IMO, Walter's single greatest point of genius vision was in the definition of slices. As with so many other things from D, I'm busy at the moment implementing slices for C++. Without garbage collection it is, as you can imagine, somewhat more difficult than it is in D. :-)

Ummm... slices are not new. Euphoria has had them since 1993. -- Derek Melbourne, Australia
Oct 15 2004
next sibling parent reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Derek" <derek psyc.ward> wrote in message
news:1de5brneaifqv.1h59yjmxgfd11$.dlg 40tude.net...
 On Sat, 16 Oct 2004 07:12:18 +1000, Matthew wrote:

 "Anders F Björklund" <afb algonet.se> wrote in message


 Stewart Gordon wrote:

 OTOH, Java allows primitive types to be template parameters, and




 I think you meant "D" instead of Java here ? If you did, I agree



 Wrapper classes are evil. Especially when used for things like



 IMO, Walter's single greatest point of genius vision was in the


 As with so many other things from D, I'm busy at the moment


 is, as you can imagine, somewhat more difficult than it is in D.

 :-)

Ummm... slices are not new. Euphoria has had them since 1993.

Thanks, but I didn't say they were. :-)
Oct 16 2004
parent reply Derek <derek psyc.ward> writes:
On Sat, 16 Oct 2004 20:08:30 +1000, Matthew wrote:

 "Derek" <derek psyc.ward> wrote in message
 news:1de5brneaifqv.1h59yjmxgfd11$.dlg 40tude.net...
 On Sat, 16 Oct 2004 07:12:18 +1000, Matthew wrote:

 "Anders F Björklund" <afb algonet.se> wrote in message


 Stewart Gordon wrote:

 OTOH, Java allows primitive types to be template parameters, and




 I think you meant "D" instead of Java here ? If you did, I agree



 Wrapper classes are evil. Especially when used for things like



 IMO, Walter's single greatest point of genius vision was in the


 As with so many other things from D, I'm busy at the moment


 is, as you can imagine, somewhat more difficult than it is in D.

 :-)

Ummm... slices are not new. Euphoria has had them since 1993.

Thanks, but I didn't say they were. :-)

Sorry. I misunderstand you then. Your phrase "Walter's single greatest point of genius vision was in the definition of slices" sounds to me as if it was saying that Walter was the person the defined what slices were. -- Derek Melbourne, Australia
Oct 16 2004
parent "Matthew" <admin.hat stlsoft.dot.org> writes:
"Derek" <derek psyc.ward> wrote in message
news:1n43gmo4ws1da$.14h8ff6hyyiyp$.dlg 40tude.net...
 On Sat, 16 Oct 2004 20:08:30 +1000, Matthew wrote:

 "Derek" <derek psyc.ward> wrote in message
 news:1de5brneaifqv.1h59yjmxgfd11$.dlg 40tude.net...
 On Sat, 16 Oct 2004 07:12:18 +1000, Matthew wrote:

 "Anders F Björklund" <afb algonet.se> wrote in message


 Stewart Gordon wrote:

 OTOH, Java allows primitive types to be template parameters, and




 I think you meant "D" instead of Java here ? If you did, I agree



 Wrapper classes are evil. Especially when used for things like



 IMO, Walter's single greatest point of genius vision was in the


 As with so many other things from D, I'm busy at the moment


 is, as you can imagine, somewhat more difficult than it is in D.

 :-)

Ummm... slices are not new. Euphoria has had them since 1993.

Thanks, but I didn't say they were. :-)

Sorry. I misunderstand you then. Your phrase "Walter's single greatest point of genius vision was in the definition of slices" sounds to me as if it was saying that Walter was the person the defined what slices were.

No worries. :)
Oct 16 2004
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Derek wrote:
 On Sat, 16 Oct 2004 07:12:18 +1000, Matthew wrote:
 
 "Anders F Björklund" <afb algonet.se> wrote in message 
 news:ckp37m$2clu$2 digitaldaemon.com...
 
 Stewart Gordon wrote:
 
 OTOH, Java allows primitive types to be template parameters, 
 and so chances are we don't really need wrapper classes.

I think you meant "D" instead of Java here ? If you did, I agree :-) Wrapper classes are evil. Especially when used for things like String.



Oops ... should've caught that silly little typo before I pressed Send. <snip>
 Ummm... slices are not new. Euphoria has had them since 1993.

Fortran 90 has slices - I'm not sure about F77. Just about every BASIC ever invented has string slices at least. Stewart.
Oct 18 2004
prev sibling parent reply Andy Friesen <andy ikagames.com> writes:
Anders F Björklund wrote:
 A question, I'm not sure if it's been asked:
 
 Is Auto-Boxing a Good Thing or a Bad Thing ?
 And if it's good, should be available in D ?

What would you use it for? The only time I have ever wanted to do this is when stuffing primitive types in an untyped C#/Java collection. This leads me to believe that automatic boxing is a cludge over a horrible, crippling weakness and not a feature. :) -- andy
Oct 15 2004
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Andy Friesen wrote:

 Is Auto-Boxing a Good Thing or a Bad Thing ?


 The only time I have ever wanted to do this is when stuffing primitive 
 types in an untyped C#/Java collection.  This leads me to believe that 
 automatic boxing is a cludge over a horrible, crippling weakness and not 
 a feature. :)

Yeah, it does seem that way. So far the most opinions have been that auto-boxing creates a lot of wasteful objects, especially if you *mix* classes and primitives. And that auto-unboxing creates some troubles will null, and with checks for identity and also with assignments... So it does seem that it's more or less just a kludge ? And of course, it's a nice little marketing buzzword. --anders
Oct 15 2004