simplified decl+assign syntax? (was * bogus codegen with static opAssign() usage *)

kris foo at bar.com
Mon Feb 19 12:49:16 PST 2007


Walter Bright wrote:
> kris wrote:
> 
>> The syntax, however, is very clean. Importantly, it supports the 
>> unification or /centralization/ of all those 'new' invocations. I'd go 
>> so far as to say such a syntax could represent a bridge between OO and 
>> scripting:
>>
>> ----
>> String s = "mystring";
>> ----
>>
>> ----
>> File f = "/foo/bar.d";
>> ----
>>
>> ----
>> Regex r = "^(.*)$";
>> ----
>>
>> There's a fairly wide range of simple applicability for this kinda' 
>> thing. Would be great if static opAssign() could support this, or some 
>> other operator were enabled?
>>
>> How about it?
> 
> 
> I don't get it. Exactly what transformation are you looking for?

Something that combines the decl with an assignment, with a more concise 
syntax than 'new', that works for both struct and class, and with the 
side-benefit of isolating the code required to instantiate an object 
instance (e.g. the codegen required for "x = new Blah" is located in one 
spot rather than being repeated multiple times).

For example, this type of function might be added to a class (for want 
of a better name):

static File opDecl (char[] foo)
{
   return new File (foo);
}

And this one might be added to a struct:

static MyStruct opDecl (int[] x)
{
   MyStruct s;
   s.list = x;
   return s;
}

In both cases, assignment to a decl would invoke the opDecl() and assign 
the result:

File f = "myfilePath";

MyStruct s = [1, 2, 3];

It's a shorthand notation in both cases, and happens to reduce the 
distinction between class & struct also? In the class case, it 
eliminates the repeated codegen associated with "new" too. There should 
be no conflict with custom allocators either, since the opDecl() is just 
a convenience wrapper around the existing mechanisms; right?

In short, this is a shorthand ctor, that could work for both class and 
struct?







More information about the Digitalmars-d mailing list