www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - UFCS should work with local symbols

--047d7b33c94cba8dcc04f4741be0
Content-Type: text/plain; charset=ISO-8859-1

I'd like to have UFCS work with local symbols, it would be more orhogonal
and make the following work:
void main(){
  void fun(string a){}
  "".fun;//CT error currently
}

unittest{
  void fun(string a){}
  "".fun;//CT error currently
}

in http://dlang.org/function.html there is some explanation why this isn't
allowed however I don't find it convincing (see my added comments with
//EDIT):

int front(int[] arr) { return arr[0]; }

void main() {
  int[] a = [1,2,3];
  auto x = a.front();   // call .front by UFCS

  auto front = x;       // front is now a variable
  auto y = a.front();   // Error, front is not a function

//EDIT: error (same behavior as if we had outside : import std.array:front;
auto front=1;)

//EDIT: another example:
  auto front(int[]b){return b;}
  auto y = a.front();   // since a isn't a class instance that has a front
method, call front(a), which is the nested function front.
}

class C {
  int[] arr;
  int front() {
    return arr.front(); // Error, C.front is not callable
                        // using argument types (int[])
    //EDIT: why not error: front is ambiguous or even accept the code as
calling the front() defined outside based on usual disambiguation rules
based on types
  }
}
}

--047d7b33c94cba8dcc04f4741be0
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>I&#39;d like to have UFCS work with local symbols, it=
 would be more orhogonal and make the following work:</div><div>void main()=
{<br></div><div>=A0 void fun(string a){}</div><div>=A0 &quot;&quot;.fun;//C=
T error currently</div>
<div>}</div><div><br></div><div>unittest{</div><div>=A0 void fun(string a){=
}</div><div>=A0 &quot;&quot;.fun;//CT error=A0currently</div><div>}</div><d=
iv><br></div><div>in=A0<a href=3D"http://dlang.org/function.html">http://dl=
ang.org/function.html</a> there is some explanation why this isn&#39;t allo=
wed however I don&#39;t find it convincing (see my added comments with //ED=
IT):<br>
</div><div><br></div><div><div>int front(int[] arr) { return arr[0]; }</div=
<div><br></div><div>void main() {</div><div>=A0 int[] a =3D [1,2,3];</div>=

iv><div> =A0 auto front =3D x; =A0 =A0 =A0 // front is now a variable</div><div>=A0 = auto y =3D a.front(); =A0 // Error, front is not a function</div><div><br><= /div><div>//EDIT: error (same behavior as if we had outside : import std.ar= ray:front; auto front=3D1;)</div> <div><br></div><div>//EDIT: another example:</div><div><div>=A0 auto front(= int[]b){return b;}</div></div><div><div>=A0 auto y =3D a.front(); =A0 // si= nce a isn&#39;t a class instance that has a front method, call front(a), wh= ich is the nested function front.</div> </div><div>}<br></div><div><br></div><div>class C {</div><div>=A0 int[] arr= ;</div><div>=A0 int front() {</div><div>=A0 =A0 return arr.front(); // Erro= r, C.front is not callable</div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 // using argument types (int[])</div> <div>=A0 =A0 //EDIT: why not error: front is ambiguous or even accept the c= ode as calling the front() defined outside based on usual disambiguation ru= les based on types</div><div>=A0 }</div><div>}</div><div>}</div></div><div>= <br> </div><div><br></div></div> --047d7b33c94cba8dcc04f4741be0--
Mar 12 2014