I have some old C++ code I'm porting to D, in which I did a lot of stuff
like this:
struct foo {
struct {
bool element1;
bool element2;
} valid;
bar element1;
bar element2;
};
So you could access the members "valid.element1" and "valid.element2".
This is no longer valid code, I have to name the "valid" struct and
create an instance of the structure. Is there any more elegant way to
do it? I always like the C++ way...creating a struct just to use one
instance of it seems very hacky to me.

I have some old C++ code I'm porting to D, in which I did a lot of stuff
like this:
struct foo {
struct {
bool element1;
bool element2;
} valid;
bar element1;
bar element2;
};
So you could access the members "valid.element1" and "valid.element2".
This is no longer valid code, I have to name the "valid" struct and
create an instance of the structure. Is there any more elegant way to
do it? I always like the C++ way...creating a struct just to use one
instance of it seems very hacky to me.

I have some old C++ code I'm porting to D, in which I did a lot of stuff
like this:
struct foo {
struct {
bool element1;
bool element2;
} valid;
bar element1;
bar element2;
};
So you could access the members "valid.element1" and "valid.element2".
This is no longer valid code, I have to name the "valid" struct and
create an instance of the structure. Is there any more elegant way to
do it? I always like the C++ way...creating a struct just to use one
instance of it seems very hacky to me.

In this case I see no reason to do this. You can just put xelement1 and
xelement2 right into struct foo.
Can you define named types inside a struct? Such as:
struct foo {
struct valid_str {
bool element1;
bool element2;
} valid;
bar element1;
bar element2;
};

Sure, I know that's possible. But that's no better than just defining
the x elements in the main struct. I would really prefer to access the
elements as "valid.element1". (Actually, I'd like to make "valid" a
boolean property of both the element1 and element2 variable...but
perhaps that's asking too much.
Then again, you could conceivably use this sort of syntax:
struct foo {
bar element1
properties {
bool valid;
};
bar element2
properties {
bool valid;
};
}
The compiler would have to store the properties in the struct, but they
would be accessible using the property syntax;
foo.element1 // refers to the variable, of type "bar"
foo.element1.valid // referes to the property

The "element1" and "valid.element1" values are tied to each other. I don't
really want to clutter up the namespace with similar names. It's particularly
problematic in my circumstance because the D code we're referring to is
automatically generated from user definitions..so what if he defined both an
"element1" and "xelement1" value for his purposes? Then the user's
"xelement1" conflicts with the automatically generated "xelement1", which is
just a property of the user's "element1".
--
The Villagers are Online! http://villagersonline.com
.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]

In C++ you can do:
struct Foo
{
struct Bar { int x; }b;
int x;
};
(Walter: That's what I refered to as "named type", as compared to declaring
an *anonymous* struct type inside an other struct.)
and in C++ you can also do:
struct Foo
{
struct { int x; }b;
int x;
};
I still don't understand the reason for disallowing these constructs. What's
the problem with them? Too complex for humans?
Sandor

problematic in my circumstance because the D code we're referring to is
automatically generated from user definitions..so what if he defined both

"element1" and "xelement1" value for his purposes? Then the user's
"xelement1" conflicts with the automatically generated "xelement1", which

just a property of the user's "element1".

Ok, I see. The way to do it is to create a unique prefix for your generated
names, like _Foo_. Disallow the user from using _ in the names he types in,
or disallow leading _.

Yeah, I do it like that for some other things, but don't really like it :(
--
The Villagers are Online! http://villagersonline.com
.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]

Now that sounds cool.
Decorating member variables with little extra functions. It's be nice to be
able to decorate types that way too. Especially other classes.
Sean
"Russell Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3D80CF9E.5070201 deming-os.org...

Sure, I know that's possible. But that's no better than just defining
the x elements in the main struct. I would really prefer to access the
elements as "valid.element1". (Actually, I'd like to make "valid" a
boolean property of both the element1 and element2 variable...but
perhaps that's asking too much.
Then again, you could conceivably use this sort of syntax:
struct foo {
bar element1
properties {
bool valid;
};
bar element2
properties {
bool valid;
};
}
The compiler would have to store the properties in the struct, but they
would be accessible using the property syntax;
foo.element1 // refers to the variable, of type "bar"
foo.element1.valid // referes to the property

Decorating member variables with little extra functions. It's be nice to

able to decorate types that way too. Especially other classes.

Well we already have properties for classes. The invention here is that they
behave as having a value of a different (actually built-in) type. And that
is *cast overloading*. If you have cast overloading, then - for example -
you can declare a class whose instances can be directly assigned to and from
integers, and still have your new properties, or boolean fields, or
whatever.
You can already do this in C++.

Sure, I know that's possible. But that's no better than just defining
the x elements in the main struct. I would really prefer to access the
elements as "valid.element1". (Actually, I'd like to make "valid" a
boolean property of both the element1 and element2 variable...but
perhaps that's asking too much.
Then again, you could conceivably use this sort of syntax:
struct foo {
bar element1
properties {
bool valid;
};
bar element2
properties {
bool valid;
};
}
The compiler would have to store the properties in the struct, but they
would be accessible using the property syntax;
foo.element1 // refers to the variable, of type "bar"
foo.element1.valid // referes to the property