www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - typeof map

reply "cal" <callumenator gmail.com> writes:
Is it by design that the code below does not compile?

import std.algorithm, std.range;

void main() {
    typeof(iota(10).map!(a=>a)) x = iota(10).map!(a=>a);	
}

Error message (DMD git-latest on DPaste):
Error: constructor f755.main.MapResult!(__lambda2, 
Result).MapResult.this (Result input) is not callable using 
argument types (MapResult!(__lambda4, Result))
Error: cannot implicitly convert expression (map(iota(10))) of 
type MapResult!(__lambda4, Result) to Result
Jun 24 2013
next sibling parent reply Timothee Cour <thelastmammoth gmail.com> writes:
I think it's because each lambda litteral is treated unique.
can dmd be changed to recognize identical lambda litterals as identical? Is
there any particular issue making that difficult?
it already recognizes identical string literals as identical

On Mon, Jun 24, 2013 at 7:45 PM, cal <callumenator gmail.com> wrote:

 Is it by design that the code below does not compile?

 import std.algorithm, std.range;

 void main() {
    typeof(iota(10).map!(a=>a)) x = iota(10).map!(a=>a);
 }

 Error message (DMD git-latest on DPaste):
 Error: constructor f755.main.MapResult!(__**lambda2,
 Result).MapResult.this (Result input) is not callable using argument types
 (MapResult!(__lambda4, Result))
 Error: cannot implicitly convert expression (map(iota(10))) of type
 MapResult!(__lambda4, Result) to Result
Jun 24 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 25 June 2013 at 04:26:00 UTC, Timothee Cour wrote:
 I think it's because each lambda litteral is treated unique.
 can dmd be changed to recognize identical lambda litterals as 
 identical? Is
 there any particular issue making that difficult?
 it already recognizes identical string literals as identical
I could be wrong, but the difference might be that it *can* recognize string literals as idential, but it doesn't actually guarantee it as spec. This is not an issue for strings, but... Imagine I have a module, where I declare "alias T = R!some_labda". Then in another module, I declare "alias U = R!some_labda". Then in a third module, would I be able to declare a T, and store it inside a U ? Would the compiler be able to see that both lambdas are actually the same? I honestly don't know. But it seems dangerous to me: Lambdas specifically don't have names, and are unique. If you type two of them that have the same body, that shouldn't mean they are actually the same. Also, doing this would create symbol collisions, where a user has defined two different lambdas, that have the same body, but expected different definitions. EG: alias T = R!some_lambda; alias U = R!some_same_lambda" void foo(T t); void foo(U u); //Nope, foo(U) already declared; What?
Jun 24 2013
parent "cal" <callumenator gmail.com> writes:
On Tuesday, 25 June 2013 at 06:58:33 UTC, monarch_dodra wrote:
 On Tuesday, 25 June 2013 at 04:26:00 UTC, Timothee Cour wrote:
 I think it's because each lambda litteral is treated unique.
 can dmd be changed to recognize identical lambda litterals as 
 identical? Is
 there any particular issue making that difficult?
 it already recognizes identical string literals as identical
I could be wrong, but the difference might be that it *can* recognize string literals as idential, but it doesn't actually guarantee it as spec. This is not an issue for strings, but...
I also thought it was to do with identical lambda literals being unique, and that the code should have compiled, but I've changed my mind after seeing your argument.
Jun 25 2013
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Jun 25, 2013 at 04:45:33AM +0200, cal wrote:
 Is it by design that the code below does not compile?
 
 import std.algorithm, std.range;
 
 void main() {
    typeof(iota(10).map!(a=>a)) x = iota(10).map!(a=>a);	
 }
[...] The workaround is to use 'auto', then alias its type: auto x = iota(10).map!(a=>a); alias IotaType = typeof(x); But yeah it would be nice if the original code worked. T -- Tell me and I forget. Teach me and I remember. Involve me and I understand. -- Benjamin Franklin
Jun 24 2013