Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
August 17, 2005 Std.boxer toString() w/ Objects or Structs. | ||||
---|---|---|---|---|
| ||||
Hi, I'm using boxes with relative ease and everything seems to be working properly. I have a problem with the Box.toString() method, though. Is there a way to make it work with objects or structs? Thanks, --AJG. |
August 17, 2005 Re: Std.boxer toString() w/ Objects or Structs. | ||||
---|---|---|---|---|
| ||||
Posted in reply to AJG | "AJG" <AJG_member@pathlink.com> wrote in message news:ddubsm$1llp$1@digitaldaemon.com... > Hi, > > I'm using boxes with relative ease and everything seems to be working > properly. > I have a problem with the Box.toString() method, though. Is there a way to > make > it work with objects or structs? > > Thanks, > --AJG. > > I can't reproduce any problem with objects - perhaps you mean interfaces? With structs there's no default toString. Note when boxing an interface I think you need to explicitly cast it to Object before boxing but I'm not sure. |
August 17, 2005 Re: Std.boxer toString() w/ Objects or Structs. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Hi, So for example, what do you get here: # class Test { # real m_Real; # this(real r) { m_Real = r; } # char[] toString() { # return (std.string.toString(m_Real)); # } # } # import std.boxer; # # void main() { # Box b = box(new Test(4.2)); # std.stdio.writefln("Test=" ~ b.toString()); # } I get: Test=nan Or something like that. (Don't have a compiler available; please excuse any typos in there). I would like to get: Test=4.2 ------ Also, what does casting a struct to an Object do? Seems kinda weird, going from value to reference? Thanks, --AJG. In article <ddviv4$2u8f$1@digitaldaemon.com>, Ben Hinkle says... >"AJG" <AJG_member@pathlink.com> wrote in message news:ddubsm$1llp$1@digitaldaemon.com... >> Hi, >> >> I'm using boxes with relative ease and everything seems to be working >> properly. >> I have a problem with the Box.toString() method, though. Is there a way to >> make >> it work with objects or structs? >> >> Thanks, >> --AJG. >> >> > >I can't reproduce any problem with objects - perhaps you mean interfaces? With structs there's no default toString. Note when boxing an interface I think you need to explicitly cast it to Object before boxing but I'm not sure. > > |
August 17, 2005 Re: Std.boxer toString() w/ Objects or Structs. | ||||
---|---|---|---|---|
| ||||
Posted in reply to AJG | > Or something like that. (Don't have a compiler available; please excuse > any > typos in there). > > I would like to get: > Test=4.2 I get Test=4.2 on Windows using dmd.129 > ------ > > Also, what does casting a struct to an Object do? Seems kinda weird, going > from > value to reference? If you are referring to my reply I said cast an interface to Object - not struct to Object. You can't cast a struct to Object as you probably guessed. |
August 17, 2005 Re: Std.boxer toString() w/ Objects or Structs. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Hi, >> Or something like that. (Don't have a compiler available; please excuse >> any typos in there). >> I would like to get: >> Test=4.2 >I get Test=4.2 on Windows using dmd.129 Will have to try that again soon. Thanks. >> Also, what does casting a struct to an Object do? Seems kinda weird, going >> from >> value to reference? > >If you are referring to my reply I said cast an interface to Object - not struct to Object. You can't cast a struct to Object as you probably guessed. Whoops. Misread that sentence a little bit ;) Though I was kind of interested in the struct to object casting, hypothetically. Could be a way to automagically pass from the stack to the GC heap. Ok, anyway, then what is the expected result when you toString() a box with a struct? Is there a defined behaviour? Thanks again, --AJG. |
August 17, 2005 Re: Std.boxer toString() w/ Objects or Structs. | ||||
---|---|---|---|---|
| ||||
Posted in reply to AJG | > Though I was kind of interested in the struct to object casting,
> hypothetically.
> Could be a way to automagically pass from the stack to the GC heap.
>
> Ok, anyway, then what is the expected result when you toString() a box
> with a
> struct? Is there a defined behaviour?
I think it does whatever std.format.doFormat does with a struct - probably throw a FormatError exception.
ps - I wish Walter would do something about the Error vs Exception naming "convention"...
|
August 18, 2005 Re: Std.boxer toString() w/ Objects or Structs. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Hi, First- you were right about Objects. There was a ridiculously diabolical bug in my code. >> Ok, anyway, then what is the expected result when you toString() a box >> with a >> struct? Is there a defined behaviour? > >I think it does whatever std.format.doFormat does with a struct - probably throw a FormatError exception. Hm... I even tried a toString() method in my struct and it still crashed and burnt. It would seem logical to me to call this method for a struct too. >ps - I wish Walter would do something about the Error vs Exception naming "convention"... Yes. This is entirely misleading. Thanks, --AJG. |
Copyright © 1999-2021 by the D Language Foundation