DUAL FEATURE REQUEST: let "with" take a struct pointer and evaluate to its parameter

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Dec 20 09:11:04 PST 2007


"downs" <default_357-line at yahoo.de> wrote in message 
news:fke1ta$2bon$3 at digitalmars.com...
> The first part of the feature request is to allow "with" to take a pointer 
> to a struct as a parameter.
>
> This is for the sake of internal consistency; since you can already call
> methods on a struct pointer as if it was dereferenced, it makes sense to
> also be able to use "with" on it.
>
> The second one arises from the following really cute idea ge0rg had in #d. 
> Quote
>
>> > <Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz = 
>> > 42; ... }'?
>
> Things this breaks:
> * None; as usual with my feature requests, it only affects behavior that 
> is currently illegal.
>
> What do you think?
> --downs

I like the idea, but I don't know why we can't just have (1) proper struct 
literals for non-static struct instances and (2) initialization of 
heap-allocated structs at allocation time.

// stack-allocated struct
auto s = MyStruct { bar: 23, baz: 42 };

// heap-allocated struct
auto s2 = new MyStruct { bar: 23, baz: 42 };

Or, hell, constructors and named params.
Instead, we have inconsistency and ugliness:

struct MyStruct
{
    int baz, bar;
}

// Look at that pretty struct initializer!
static MyStruct s = { bar: 23, baz: 42 };

// Is it a function?  Is it a call to opCall?  Why
// can't I name the members or initialize them out of
// order or leave some uninitialized?  Why is this
// so different from the static case?
auto s2 = MyStruct(42, 23);

// NOW what?!  Now I have an instance on the heap that's
// filled with default values.. how do I initialize it?
// I have to separate out the initialization into some
// method function and call it as a separate step.
auto s = new MyStruct;

// And again I can't name anything.
s.initialize(42, 23);

Dumb, dumb, dumb.  Don't even get me started on the "blessing" of static 
opCall.  Are we *trying* to make stupid special cases? 





More information about the Digitalmars-d mailing list