Syntactic Sugar for Virtual Constructors?

BCS BCS at pathlink.com
Thu Feb 28 08:54:24 PST 2008


Janice Caron wrote:
> On 28/02/2008, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
> 
>>what about static opCall?
> 
> 
> Ooh - I never thought of that. That's close.
> 
> In the past, what I've done is to build a standalone function (not a
> member function) called new_MyClass(), which is only an underscore
> different from the standard "new" function. Ditching the word "new"
> altogether is attractive.
> 
> But to clarify - it's the syntactic sugar of being able to say
> 
>     new AbstractBaseClass(...)
> 


how about allow this functions in abstract classes

abstract class AbstractBaseClass
{
	AbstractBaseClass this(string s, int i)
	{
		switch(s)
		{
			case "Foo": return new DerivedClass(i);
			...
			default: return null;
				// check code causes exception
		}
	}
}

the return type could be used to detect the usage.


> (and end up with a derived class) that I was asking for. I am well
> aware that we already have the functionality, but the point of the
> request /is/ the sugar. It's not that I want the syntax to be "simpler
> than it is now", or something, it's that I want it to be /identical/
> other uses of new. It's just one of those nice little touches that
> would make D seem more cool. :-)



More information about the Digitalmars-d mailing list