UFCS and constructors

Jonathan M Davis jmdavisProg at gmx.com
Tue Jul 2 19:12:09 PDT 2013


On Wednesday, July 03, 2013 04:00:45 bearophile wrote:
> deadalnix:
> > The whole point of UFCS is to be able to provide additional
> > custom "methods" to a object (class or struct). Constructor
> > UFCS don't fulfill that use case.
> > 
> > Nothing is removed from the language as factories method can be
> > introduced anyway.
> 
> This frames the topic in a wrong way. Constructors are not normal
> functions, they are special, but functional languages show us
> that's it's a very good idea to see them as functions.
> 
> And the original point of UFCS doesn't matter much. What matters
> is what are the practical disadvantages of allowing UFCSyntax for
> constructors (like the original post in this thread), and what
> are their practical advantages/uses (like a handy usage in UFCS
> chains). Then we take a look at what's the resulting balance and
> we decide. And such decisions should then become the written
> specifics of this part of the D design.

The primary benefit of UFCS is generic code, because if uses UFCS, generic code 
doesn't have to care whether a function is a member function or a free 
function (particularly when that can vary drastically depending on what type 
is used to instantiate the template). Construction is not and cannot be 
generic. The closest that you could get would be a factory function, which is 
quite different. But constructors themselves cannot be generic. As such, using 
constructors with UFCS in generic code just doesn't work, meaning that the 
only gain that you're getting from using UFCS with constructors is that you 
get a slightly different syntax that you might like better for one reason or 
another. But I see no technical reason why it could add any benefit over simply 
calling the constructor normally. And as such, I think that allowing UFCS to 
work with constructors is definitely an anti-feature.

- Jonathan M Davis


More information about the Digitalmars-d mailing list