Anonymous structs
Era Scarecrow
rtcvb32 at yahoo.com
Wed Feb 13 00:45:59 PST 2013
On Wednesday, 13 February 2013 at 07:28:14 UTC, Jacob Carlborg
wrote:
> On 2013-02-12 21:30, Era Scarecrow wrote:
>
>> Seems I did misread what you had, however having it creating
>> dozens of misc/anonymous types doesn't seem like a wise idea.
>> The entire block as it was defined is more like a scope/code
>> block rather than a struct declaration; Then is it a delegate
>> instead? (without return type or input type possibly)
>
> I don't know what you're talking about. Where did "delegate"
> come from?
Then let's step back. You can make a scope block without having
'if' or any other statment that separates it.
unittest {
int x;
{
x++;//code block is valid
}
Now if you attach that to a variable it's effectively a
delegate, function, or predicate; depending on syntax of how it's
called.
auto y = delegate void(){ x++; };
auto y = (){ x++; }; //shortened to
auto y = { x++; }; //if no calling variables gets shortened
to..??
Now if there's only type declarations and no instructions, it
can be an anonymous struct (probably), but what if it has code?
Is it a code block? The code gets defaulted to a function inside
it? Illegal to do period? (at which point it breaks regular
compatibility most likely).
auto z = {int x,y,color;}; //types only, could be struct...
auto z = { //which is it?
int x,y,color;
x++; y++; color = 0xffffff;
};
This can either be
1) POD struct, instructions are illegal
2) instructions called right away, still a POD struct otherwise
auto z = {
int x,y,color;
};
z.x++; z.y++; z.color = 0xffffff;
3) delegate/function/predicate
z(); //call is legal, or passable to template function
4) structs legal and instructions are effectively postblit (or
opCall or something)
struct anonymous_int_x_y_color {
int x,y,color;
this(this) {
x++; y++; color = 0xffffff;
}
}
anonymous_int_x_y_color z;
}
5) none of it is legal, leaves you having to specify what it is
which is probably safer than assumption or ambiguity.
It can be easy to forget a simple set of parenthesis (or
semicolon, or equal sign) sometimes when you're programming,
having it make assumptions of code 'this is a struct' vs 'this is
a delegate' could be quite the annoyance, perhaps with very
confusing error messages. IMO the way you're suggest having
anonymous structs seems unneeded.
More information about the Digitalmars-d
mailing list