www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - why is std.boxer.Box a struct and not a class?

reply derick_eddington nospam.yashmoo.com writes:
I'm wondering because I'm making a Deferred class for a D copy of Twisted, and
central to it is users supplying delegates that take variadic arguments and
return a variadic value and I need to save the variadic arguments for later to
combine with an additional variadic value and pass this all to another
user-delegate, so I'm using Box[] and Box.

But you can't have null-Box-references as a way of indicating null since it's a
struct (and I don't want struct pointers).  box(null) and
unboxable!(typeof(null))(mybox) work, but aren't as convenient.  And you have to
explicitly call Box.toString() for std.format because Box isn't an Object.  I
could easily make my own class wrapping a Box but I wanted to leverage the
standardized-ness of Box, but with Box as a struct users have to do box(null)
and unboxable!(typeof(null))(mybox) everytime to return and test for null.
Jun 02 2005
next sibling parent reply "Kris" <fu bar.com> writes:
I imagine it would be to avoid a call to new/malloc. Structs can simply be
allocated on the stack, whereas classes can't. If you need long term
persistence for Box, then one could wrap it as you suggested (or uses
pointers).

- Kris

<derick_eddington nospam.yashmoo.com> wrote in message
news:d7oc8j$2m2j$1 digitaldaemon.com...
 I'm wondering because I'm making a Deferred class for a D copy of Twisted,
and
 central to it is users supplying delegates that take variadic arguments
and
 return a variadic value and I need to save the variadic arguments for
later to
 combine with an additional variadic value and pass this all to another
 user-delegate, so I'm using Box[] and Box.

 But you can't have null-Box-references as a way of indicating null since
it's a
 struct (and I don't want struct pointers).  box(null) and
 unboxable!(typeof(null))(mybox) work, but aren't as convenient.  And you
have to
 explicitly call Box.toString() for std.format because Box isn't an Object.
I
 could easily make my own class wrapping a Box but I wanted to leverage the
 standardized-ness of Box, but with Box as a struct users have to do
box(null)
 and unboxable!(typeof(null))(mybox) everytime to return and test for null.
Jun 02 2005
next sibling parent Chris Sauls <ibisbasenji gmail.com> writes:
Is the inout attribute applicable in this case?  (As a way of avoiding 
explicit pointers.)

-- Chris Sauls

Kris wrote:
 I imagine it would be to avoid a call to new/malloc. Structs can simply be
 allocated on the stack, whereas classes can't. If you need long term
 persistence for Box, then one could wrap it as you suggested (or uses
 pointers).
 
 - Kris
 
 <derick_eddington nospam.yashmoo.com> wrote in message
 news:d7oc8j$2m2j$1 digitaldaemon.com...
 
I'm wondering because I'm making a Deferred class for a D copy of Twisted,
and
central to it is users supplying delegates that take variadic arguments
and
return a variadic value and I need to save the variadic arguments for
later to
combine with an additional variadic value and pass this all to another
user-delegate, so I'm using Box[] and Box.

But you can't have null-Box-references as a way of indicating null since
it's a
struct (and I don't want struct pointers).  box(null) and
unboxable!(typeof(null))(mybox) work, but aren't as convenient.  And you
have to
explicitly call Box.toString() for std.format because Box isn't an Object.
I
could easily make my own class wrapping a Box but I wanted to leverage the
standardized-ness of Box, but with Box as a struct users have to do
box(null)
and unboxable!(typeof(null))(mybox) everytime to return and test for null.
Jun 02 2005
prev sibling parent reply derick_eddington nospam.yashmoo.com writes:
That's what I thought, but wasn't sure because boxArray() or manually-made
Box[]s, both of which I'm assuming will be used as much if not more than single
Box's, have to 'new Box[]', so it's having to new the Box's anyways.

And I realized unboxable!(typeof(null))(mybox) won't work for what I'm doing
because the implicit casting allows all kinds of types to pass the predicate.

So if anyone cares, I've made a simple Var class wrapper around std.boxer and
it's a little more class-like and it will allow using it as variadic arguments
that want to be tested for being null.  It's attached.

In article <d7odpm$2n9d$1 digitaldaemon.com>, Kris says...
I imagine it would be to avoid a call to new/malloc. Structs can simply be
allocated on the stack, whereas classes can't. If you need long term
persistence for Box, then one could wrap it as you suggested (or uses
pointers).

- Kris

<derick_eddington nospam.yashmoo.com> wrote in message
news:d7oc8j$2m2j$1 digitaldaemon.com...
 I'm wondering because I'm making a Deferred class for a D copy of Twisted,
and
 central to it is users supplying delegates that take variadic arguments
and
 return a variadic value and I need to save the variadic arguments for
later to
 combine with an additional variadic value and pass this all to another
 user-delegate, so I'm using Box[] and Box.

 But you can't have null-Box-references as a way of indicating null since
it's a
 struct (and I don't want struct pointers).  box(null) and
 unboxable!(typeof(null))(mybox) work, but aren't as convenient.  And you
have to
 explicitly call Box.toString() for std.format because Box isn't an Object.
I
 could easily make my own class wrapping a Box but I wanted to leverage the
 standardized-ness of Box, but with Box as a struct users have to do
box(null)
 and unboxable!(typeof(null))(mybox) everytime to return and test for null.
begin 0644 var.d M85]L:7-T(&%R9W!T<BD-"GL-" ER971U<FX ;F5W(%9A<BAT>7!E+"!A<F=P M=6UE;G1S+FQE;F=T:"`]/2`Q*3L-"GT-"F)O9'D >PT*"7)E='5R;B!V87(H M87D *%1Y<&5);F9O6UT ='EP97,L('9A7VQI<W0 87)G<W!T<BD-"GL-" E6 M("AS:7IE7W0 :2P 0F]X(&([(&)O>$%R<F%Y*'1Y<&5S+"!A<F=S<'1R*2D- M" D)<F5T6VE=(#T M9W5M96YT<R`H5F%R6UT 87)G=6UE;G1S+"!O=70 5'EP94EN9F];72!T>7!E M<RP ;W5T('9A7VQI<W0 9&%T82D-"GL-" E";WA;72!B82`](&YE=R!";WA; M=`T*>PT*"61E8G5G('=R:71E9FQN*"(E<R!B96=I;B!U;FET=&5S="XN+B(L M<B B87-D9B(I.PT*"6%S<V5R="`H=BYI<U1Y<&4A*&-H87);72DI.PT*"6%S M<V5R="`H=BYG970A*&-H87);72D /3T (F%S9&8B*3L-" EV(#T =F%R*"TT M+C4W,C0Y93$R*3L-" EA<W-E<G0 *'8N:7-4>7!E(2AD;W5B;&4I*3L-" EA M<W-E<G0 *'8N9V5T(2AD;W5B;&4I(#T]("TT+C4W,C0Y93$R*3L-" T*"79A M(#T M.PT*"6%S<V5R="`H=F%;,%TN9V5T(2AI;G0I(#T](#DX-RD M("AV85LQ72YI<U1Y<&4A*&-H87);72DI.PT*"6%S<V5R="`H=F%;,5TN9V5T M(2AC:&%R6UTI(#T M=F%R07)R87E4;T%R9W5M96YT<RAV82P ='EP97,L(&%R9W,I.PT*"6%S<V5R M="`H='EP97,N;&5N9W1H(#T M='EP96ED*&EN="DI.PT*"6%S<V5R="`H=F%?87)G(2AI;G0I*&%R9W,I(#T] M(#DX-RD M" EA<W-E<G0 *'1Y<&5S6S)=(&ES('1Y<&5I9"AF;&]A="DI.PT*"6%S<V5R M="`H=F%?87)G(2AF;&]A="DH87)G<RD /3T ,"XQ,C-F*3L-" T*"61E8G5G M('=R:71E9FQN*"(E<R!U;FET=&5S="!S=6-C97-S+B(L(%]?1DE,15]?*3L- '('T-"GT-"B!I ` end
Jun 02 2005
parent reply derick_eddington nospam.yashmoo.com writes:
Of course I forgot to add .toString() and the operators Box supports; but after
adding them by just calling the wrapped Box's methods, my Var is not working as
an A.A. key but Box does works.  I.E.:

// works
assert (var(456) < var(567));
assert (var(787) == var(787));
assert (var("zzz") > var("xxx"));
assert (var("qwerty") == var("qwerty"));

// Box works
char[][Box] cbaa;
cbaa[box(45.234)] = "45.234";
Object o = new Object;
cbaa[box(o)] = "Object";
assert (cbaa[box(45.234)] == "45.234");
assert (cbaa[box(o)] == "Object");

// does NOT work
// but Var just wraps Box's operators
char[][Var] cvaa;
cvaa[var(45.234)] = "45.234";
o = new Object;
cvaa[var(o)] = "Object";
assert (cvaa[var(45.234)] == "45.234");
assert (cvaa[var(o)] == "Object");

I don't need these operators currently, but I'd like to have them work like Box.
Does anyone know why Var just wrapping its Box's methods is not working?


In article <d7olfs$2se0$1 digitaldaemon.com>,
derick_eddington nospam.yashmoo.com says...
That's what I thought, but wasn't sure because boxArray() or manually-made
Box[]s, both of which I'm assuming will be used as much if not more than single
Box's, have to 'new Box[]', so it's having to new the Box's anyways.

And I realized unboxable!(typeof(null))(mybox) won't work for what I'm doing
because the implicit casting allows all kinds of types to pass the predicate.

So if anyone cares, I've made a simple Var class wrapper around std.boxer and
it's a little more class-like and it will allow using it as variadic arguments
that want to be tested for being null.  It's attached.

In article <d7odpm$2n9d$1 digitaldaemon.com>, Kris says...
I imagine it would be to avoid a call to new/malloc. Structs can simply be
allocated on the stack, whereas classes can't. If you need long term
persistence for Box, then one could wrap it as you suggested (or uses
pointers).

- Kris
Jun 02 2005
parent derick_eddington nospam.yashmoo.com writes:
I guess I've answered my own question.  Apparently, unlike structs, Objects have
to have 'int opCmp(Object)' and not 'int opCmp(MySubclass)' even when only
comparing with other MySubclass's.

In article <d7onpm$2u3k$1 digitaldaemon.com>,
derick_eddington nospam.yashmoo.com says...
Of course I forgot to add .toString() and the operators Box supports; but after
adding them by just calling the wrapped Box's methods, my Var is not working as
an A.A. key but Box does works.  I.E.:

// works
assert (var(456) < var(567));
assert (var(787) == var(787));
assert (var("zzz") > var("xxx"));
assert (var("qwerty") == var("qwerty"));

// Box works
char[][Box] cbaa;
cbaa[box(45.234)] = "45.234";
Object o = new Object;
cbaa[box(o)] = "Object";
assert (cbaa[box(45.234)] == "45.234");
assert (cbaa[box(o)] == "Object");

// does NOT work
// but Var just wraps Box's operators
char[][Var] cvaa;
cvaa[var(45.234)] = "45.234";
o = new Object;
cvaa[var(o)] = "Object";
assert (cvaa[var(45.234)] == "45.234");
assert (cvaa[var(o)] == "Object");

I don't need these operators currently, but I'd like to have them work like Box.
Does anyone know why Var just wrapping its Box's methods is not working?
Jun 02 2005
prev sibling next sibling parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
<derick_eddington nospam.yashmoo.com> wrote in message 
news:d7oc8j$2m2j$1 digitaldaemon.com...
 I'm wondering because I'm making a Deferred class for a D copy of Twisted, 
 and
 central to it is users supplying delegates that take variadic arguments 
 and
 return a variadic value and I need to save the variadic arguments for 
 later to
 combine with an additional variadic value and pass this all to another
 user-delegate, so I'm using Box[] and Box.

 But you can't have null-Box-references as a way of indicating null since 
 it's a
 struct (and I don't want struct pointers).  box(null) and
 unboxable!(typeof(null))(mybox) work, but aren't as convenient.  And you 
 have to
 explicitly call Box.toString() for std.format because Box isn't an Object. 
 I
 could easily make my own class wrapping a Box but I wanted to leverage the
 standardized-ness of Box, but with Box as a struct users have to do 
 box(null)
 and unboxable!(typeof(null))(mybox) everytime to return and test for null.
here's a little helper: class BoxObject { Box b; this(...) { this.b = box(_arguments[0],_argptr); } char[] toString() { return b.toString(); } uint toHash() { return b.toHash(); } int opEquals(Object o) { BoxObject bo = cast(BoxObject)o; if (!bo) return 0; return b == bo.b; } int opCmp(Object o) { BoxObject bo = cast(BoxObject)o; if (!bo) throw Exception("must compare a BoxObject with a BoxObject"); return b.opCmp(bo.b); } }
Jun 02 2005
parent derick_eddington nospam.yashmoo.com writes:
Thanks.  JSYK, Box has 'float opCmp(Box)' so you have to cast(int).

In article <d7onls$2u1j$1 digitaldaemon.com>, Ben Hinkle says...
here's a little helper:
class BoxObject {
  Box b;
  this(...) { this.b = box(_arguments[0],_argptr); }
  char[] toString() { return b.toString(); }
  uint toHash() { return b.toHash(); }
  int opEquals(Object o) {
    BoxObject bo = cast(BoxObject)o;
    if (!bo)
      return 0;
    return b == bo.b;
  }
  int opCmp(Object o) {
    BoxObject bo = cast(BoxObject)o;
    if (!bo)
      throw Exception("must compare a BoxObject with a BoxObject");
    return b.opCmp(bo.b);
  }
}
Jun 02 2005
prev sibling parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
<derick_eddington nospam.yashmoo.com> wrote in message 
news:d7oc8j$2m2j$1 digitaldaemon.com...
 I'm wondering because I'm making a Deferred class for a D copy of Twisted, 
 and
 central to it is users supplying delegates that take variadic arguments 
 and
 return a variadic value and I need to save the variadic arguments for 
 later to
 combine with an additional variadic value and pass this all to another
 user-delegate, so I'm using Box[] and Box.

 But you can't have null-Box-references as a way of indicating null since 
 it's a
 struct (and I don't want struct pointers).  box(null) and
 unboxable!(typeof(null))(mybox) work, but aren't as convenient.  And you 
 have to
 explicitly call Box.toString() for std.format because Box isn't an Object. 
 I
 could easily make my own class wrapping a Box but I wanted to leverage the
 standardized-ness of Box, but with Box as a struct users have to do 
 box(null)
 and unboxable!(typeof(null))(mybox) everytime to return and test for null.
But you can't have null-Box-references as a way of indicating null since 
it's a
Just wondering: why you cannot use just empty box? Box b; assert(b.toString == "<empty box>");
Jun 03 2005
parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
But you can't have null-Box-references as a way of indicating null since
it's a
Just wondering: why you cannot use just empty box? Box b; assert(b.toString == "<empty box>");
or assert( b is Box.init );
Jun 03 2005