www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.boxed

reply bobef <bobef_member pathlink.com> writes:
So what is the use of these boxes? I can not undestand? Can someone help me...
maybe a little example and explanation why it is needed? Thank you...
May 19 2005
next sibling parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
Boxing is basically wannabe loose typing.  It's for use when you want to 
pass something around without the baton holders having to know the type 
of it.

It's like a void*, except with type information.  Or, so I understand.

-[Unknown]


 So what is the use of these boxes? I can not undestand? Can someone help me...
 maybe a little example and explanation why it is needed? Thank you...
 
 

May 19 2005
prev sibling parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
 So what is the use of these boxes? I can not undestand? Can someone help 
 me...
 maybe a little example and explanation why it is needed? Thank you...

Synonims: VARIANT, discriminated union. Box is a structure which stores polymorphic data (not known at compile time) Typically implemented as struct box { byte type; // type of data in the box union { int intVal; double dblVal; void* ptrVal; } data; int getIntData() { if(type == INT) return data.intVal; .... } double getDblData() { .... } } Almost all scripting languages use kind of boxes as a representation of values. Andrew. "bobef" <bobef_member pathlink.com> wrote in message news:d6is5q$2gt9$1 digitaldaemon.com...
 So what is the use of these boxes? I can not undestand? Can someone help 
 me...
 maybe a little example and explanation why it is needed? Thank you...

 

May 19 2005
next sibling parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Andrew Fedoniouk" <news terrainformatica.com> wrote in message 
news:d6jglj$2v9l$1 digitaldaemon.com...
 So what is the use of these boxes? I can not undestand? Can someone help 
 me...
 maybe a little example and explanation why it is needed? Thank you...

Synonims: VARIANT, discriminated union. Box is a structure which stores polymorphic data (not known at compile time) Typically implemented as struct box { byte type; // type of data in the box union { int intVal; double dblVal; void* ptrVal; } data; int getIntData() { if(type == INT) return data.intVal; .... } double getDblData() { .... } } Almost all scripting languages use kind of boxes as a representation of values. Andrew.

I agree Box is like discriminated unions in that they store data whose type is only known at run time but I wouldn't say it's a synonym since as you pointed out in a previous thread unions wrap a small number of known types and Boxes wrap all types. Maybe I'm nit-picking...
May 19 2005
parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
 I agree Box is like discriminated unions in that they store data whose 
 type is only known at run time but I wouldn't say it's a synonym since as 
 you pointed out in a previous thread unions wrap a small number of known 
 types and Boxes wrap all types. Maybe I'm nit-picking...

:) Well, well, well, back to roots.... ... let me to take my place on the rostrum... ok... (who the hell left this chalk here?) .... khm-khm.... Dear students! ... Discriminated union is such a type (especially an algebraic data type) might be described as a discriminated union if it is a sum type whose objects consist of a tag to say which part of the union they belong to and a value of the corresponding type. ... (Hooh, too long statement... not for use in MSDN...) To be serious: Andrew's Alexandrescu discriminated unions implementation based on type lists is just a particular case of them. The Box is another implementation of DU limited by type system of D language. :-P Andrew. "Ben Hinkle" <ben.hinkle gmail.com> wrote in message news:d6jiik$306n$1 digitaldaemon.com...
 "Andrew Fedoniouk" <news terrainformatica.com> wrote in message 
 news:d6jglj$2v9l$1 digitaldaemon.com...
 So what is the use of these boxes? I can not undestand? Can someone help 
 me...
 maybe a little example and explanation why it is needed? Thank you...

Synonims: VARIANT, discriminated union. Box is a structure which stores polymorphic data (not known at compile time) Typically implemented as struct box { byte type; // type of data in the box union { int intVal; double dblVal; void* ptrVal; } data; int getIntData() { if(type == INT) return data.intVal; .... } double getDblData() { .... } } Almost all scripting languages use kind of boxes as a representation of values. Andrew.

I agree Box is like discriminated unions in that they store data whose type is only known at run time but I wouldn't say it's a synonym since as you pointed out in a previous thread unions wrap a small number of known types and Boxes wrap all types. Maybe I'm nit-picking...

May 19 2005
prev sibling parent reply bobef <bobef_member pathlink.com> writes:
So why don't we just use void*-s? Their content is also know at run time. I mean
we know what we put in there so we know what we expect to put out. Or at least
most of the cases. Anyway I think I understand it better now but I still can't
think of any use.. I don't say it is useless I just can't think of one in this
moment...


In article <d6jglj$2v9l$1 digitaldaemon.com>, Andrew Fedoniouk says...
 So what is the use of these boxes? I can not undestand? Can someone help 
 me...
 maybe a little example and explanation why it is needed? Thank you...

Synonims: VARIANT, discriminated union. Box is a structure which stores polymorphic data (not known at compile time) Typically implemented as struct box { byte type; // type of data in the box union { int intVal; double dblVal; void* ptrVal; } data; int getIntData() { if(type == INT) return data.intVal; .... } double getDblData() { .... } } Almost all scripting languages use kind of boxes as a representation of values. Andrew. "bobef" <bobef_member pathlink.com> wrote in message news:d6is5q$2gt9$1 digitaldaemon.com...
 So what is the use of these boxes? I can not undestand? Can someone help 
 me...
 maybe a little example and explanation why it is needed? Thank you...

 


May 20 2005
next sibling parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
The idea is simply to have a class to put both a void* in and a 
TypeInfo, essentially.

The use might be for something like this:

Box coolfunction(int mode)
{
	if (mode == 1)
		return box(5);
	elseif (mode == 2)
		return box("testing");
	else
		return box(false);
}

Or similar.  For class members too.

-[Unknown]


 So why don't we just use void*-s? Their content is also know at run time. I
mean
 we know what we put in there so we know what we expect to put out. Or at least
 most of the cases. Anyway I think I understand it better now but I still can't
 think of any use.. I don't say it is useless I just can't think of one in this
 moment...

May 20 2005
next sibling parent bobef <bobef_member pathlink.com> writes:
Thanks. I understand now. It is cool. I like it.

In article <d6k5fl$fda$1 digitaldaemon.com>, Unknown W. Brackets says...
The idea is simply to have a class to put both a void* in and a 
TypeInfo, essentially.

The use might be for something like this:

Box coolfunction(int mode)
{
	if (mode == 1)
		return box(5);
	elseif (mode == 2)
		return box("testing");
	else
		return box(false);
}

Or similar.  For class members too.

-[Unknown]


 So why don't we just use void*-s? Their content is also know at run time. I
mean
 we know what we put in there so we know what we expect to put out. Or at least
 most of the cases. Anyway I think I understand it better now but I still can't
 think of any use.. I don't say it is useless I just can't think of one in this
 moment...


May 20 2005
prev sibling parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Unknown W. Brackets" <unknown simplemachines.org> wrote in message 
news:d6k5fl$fda$1 digitaldaemon.com...
 The idea is simply to have a class to put both a void* in and a TypeInfo, 
 essentially.

A small clarification: an important feature is that integer types like ints, longs and doubles and array-references (ptr + length) are stored "inline" meaning what is stored is not a void* pointing to an int but the actual int is stored. So it is very efficient to pass around boxes of small types like ints.
May 20 2005
prev sibling parent Paul Bonser <misterpib gmail.com> writes:
bobef wrote:
 So why don't we just use void*-s? Their content is also know at run time. I
mean
 we know what we put in there so we know what we expect to put out. Or at least
 most of the cases. Anyway I think I understand it better now but I still can't
 think of any use.. I don't say it is useless I just can't think of one in this
 moment...
 
 
 In article <d6jglj$2v9l$1 digitaldaemon.com>, Andrew Fedoniouk says...
 
So what is the use of these boxes? I can not undestand? Can someone help 
me...
maybe a little example and explanation why it is needed? Thank you...

Synonims: VARIANT, discriminated union. Box is a structure which stores polymorphic data (not known at compile time) Typically implemented as struct box { byte type; // type of data in the box union { int intVal; double dblVal; void* ptrVal; } data; int getIntData() { if(type == INT) return data.intVal; .... } double getDblData() { .... } } Almost all scripting languages use kind of boxes as a representation of values. Andrew. "bobef" <bobef_member pathlink.com> wrote in message news:d6is5q$2gt9$1 digitaldaemon.com...
So what is the use of these boxes? I can not undestand? Can someone help 
me...
maybe a little example and explanation why it is needed? Thank you...



Dang, I'm gone for a while, and a neat feature like this gets added... This'll be great for implementing a scripting language in my game engine.. :) -- -PIB -- "C++ also supports the notion of *friends*: cooperative classes that are permitted to see each other's private parts." - Grady Booch
May 24 2005