UFCS and constructors

monarch_dodra monarchdodra at gmail.com
Tue Jul 2 00:35:38 PDT 2013


Coming back from learn here. There was an example where somebody 
"accidentally" called a constructor via UFCS. I am kind of 
surprised that it worked. I thought UFCS was for functions only, 
and that constructors (specifically) were off limits.

Am I mistaken? Is UFCS explicitly allowed for constructors? Or 
did we kind of forget to take it into account?

One of the "big" problems with allowing UFCS and constructors is 
that a "." which meant "scope" can now mean "function call". If I 
remember correctly, that is the reason why qualified calls (eg 
'a'.std.uni.toLower()) aren't allowed in UFCS (Am I correct?), 
but with constructors, you get the same problem:

Allow me to demonstrate:

--------
import std.stdio;

struct Bar
{
     struct S
     {
         int i;
     }
     enum j = 1;
}

struct S
{
     int[10] i;
}

void main()
{
     writeln(Bar.S());   (1)
     writeln(Bar.j.S()); (2)
}
--------

(1) is a standard scope call: instantiate a Bar.S. This prints 
"S(0)".
(2) is actually: "get the value j from Bar, and then UFCS 
construct an S using that J". This prints:  "S([1, 1, 1, 1, 1, 1, 
1, 1, 1, 1])"

Furthermore, I find UFCS construction confusing on the grounds 
that there is no actual "constructor function" eg: "this(...)" 
call: This is just aggregate initialization, which looks *very* 
confusing when written that way.

------------------------------------

So to sum up the question: Was UFCS + constructors are really 
desired feature? Was it taken into account? Do we want to keep it?

In particular, the "standard" workaround of "free function 
constructor" (EG "Take" vs "take") would serve much better here.


More information about the Digitalmars-d mailing list