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

Bill Baxter dnewsgroup at billbaxter.com
Fri Dec 21 18:11:23 PST 2007


Daniel Keep wrote:
> 
> Simen Kjaeraas wrote:
>> On Thu, 20 Dec 2007 16:31:56 +0100, downs <default_357-line at yahoo.de>
>> wrote:
>>
>>> 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; ... }'?
>> Wouldn't it be more logical to use
>>
>>     with (auto foo = new myStruct) { bar = 23; baz = 42; ... }
> 
> Consistency demands that 'foo' only exists until the end of the while
> block.  What, then, would be the point of giving the struct a name for
> the duration of a construct whose primary purpose is to allow you to
> omit the name?

Because there's no 'this' to use if you actually need the name for 
something, like to pass it to a function.  For example in DFL code you 
might want something like:

   with(auto btn = new Button) {
      text = "Press Me";
      ...
      common_button_init(btn);
      allButtons ~= btn;
   }

> Being able to use control structures as expressions is a damn cool idea;
> I just don't get why more people don't like it.

That is a good point. The given example just wasn't a very good one 
because it could easily be replaced by something 99% as succinct.  But 
if you talk about passing the thing to a function, the expression 
version would be pretty nifty:

     create_form(
         with(new Button){
             text="foo";
         },
         with(new Checkbox) {
             text="foo";
             check = true;
         }
         ...
      )

--bb



More information about the Digitalmars-d mailing list