www.digitalmars.com         C & C++   DMDScript  

D - Suggestion: "anon" keyword prefix for no-type structs

reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
I'd like to propose this syntax:

    anon struct {
        <members>
    } <variable>;

which would declare a new (unnamed) struct type and create a single
variable instance of it.  However, I also think that array syntax should
be allowed:

    anon struct {
        <members>
    }[] <arrayVariable>;

This should be 100% clear to the parser (no abiguities like the old C++
syntax), and allows me the syntax sugar.  The only alternative I have is
to define dummy names:

    struct _valid {
        <members>
    } _valid valid;

But if you need different "valid" structs in different areas of your
type tree, then you have to prefix everything, and it starts to get
really ugly:

    struct _myType {
        struct _valid {
            bool a,b;
        } _valid valid;

        struct _a {
            struct _a_valid {
                <aMemberList>
            } _a_valid valid;
            <aMemberList>
        } _a a;

        struct _b {
            struct _b_valid {
                <bMemberList
            } _b_valid valid;
            <bMemberList>
        } _b b;
    }

This is bad enough to write by hand...but my current project is
attempting to generate code automatically, and it gets really ugly!
Just look how much nicer it would be with the "anon" syntax:

    struct _myType {
        anon struct {
            bool a,b;
        } valid;

        anon struct {
            anon struct {
                <aMemberList>
            } valid;
            <aMemberList>
        } a;

        anon struct {
            anon struct {
                <bMemberList
            } valid;
            <bMemberList>
        } b;
    }

Much nicer!

--
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))) ]
Sep 26 2002
parent Patrick Down <pat codemoon.com> writes:
Russ Lewis <spamhole-2001-07-16 deming-os.org> wrote in 
news:3D93147F.B9D2AFB1 deming-os.org:

 I'd like to propose this syntax:
 
     anon struct {
         <members>
     } <variable>;
 

I like this idea but I have a different application for it. I'm finding it nice to define callback structures. Right now because of the bug I mentioned in the news group yesterday I have to declare the structs outside the scope of the function in which they are used. struct _CalcNodeMin { TreeNode min; void TestNode(TreeNode node) { if(min == null) min = node; else { if(min.Value() > node.Value()) min = node; } } } void DoTreeCalcs() { _CalcNodeMin cnm; tree.forEachNode(&cnm.TestNode); // Do stuff with min node } Hopefully after the bug is fixed I can move the struct into the function. But it would also be nice if I did not need to give it a name. void DoTreeCalcs() { anon struct { TreeNode min; void TestNode(TreeNode node) { if(min == null) min = node; else { if(min.Value() > node.Value()) min = node; } } } cnm; tree.forEachNode(&cnm.TestNode); // Do stuff with min node }
Sep 26 2002