Question about D's quantum-esque type system
Sean Kelly
sean at f4.ca
Wed Aug 16 11:30:05 PDT 2006
nobody wrote:
> It is my understanding that only structs guarantee you an order of the
> fields you define while not allowing constructors. Which means whether
> you want to initialize the fields via static struct building functions
> or member functions you have no assurance the struct fields hold
> meaningful data unless by happenstance you have a situation in which the
> default values of the fields can be creatively used to get a freshly
> initialized struct to be meaningful.
For what it's worth, you can specify a default value for struct members,
you just can't supply a this() function:
struct S
{
int x = 5;
char y = 'a';
}
void main()
{
S s;
printf( "%d\n%c\n", s.x, s.y );
}
prints:
5
a
> However, classes allow a constructor but at the loss of any guarantee of
> the order of the fields. So you can be sure your initialized class
> starts out with meaningful values but you cannot map the class instance
> directly onto some data structure in memory nor can a class instance
> imitate a data structure.
I'll admit I'm a bit disappointed that my .isizeof proposal was ignored,
as it would have been nice to be able to construct class instances into
static buffers. I would be surprised if member order were not
guaranteed (though a more compact layout may be possible if the compiler
could rearrange data), but offset is obviously affected by data in any
parent classes.
Sean
More information about the Digitalmars-d-learn
mailing list