Thread overview | ||||||
---|---|---|---|---|---|---|
|
June 12, 2013 Compiler errors and recursive nested functions | ||||
---|---|---|---|---|
| ||||
Hello! I'm trying to make a deepMap function which operates on the deepest non-range element of an array. I think that this function should theoretically work, however there are some compiler errors that I can't quite understand. Here's the code: http://dpaste.dzfl.pl/97918311 And the compiler error because DPaste's compiler service seems to be offline: src\algorithm_util.d(26): Error: function algorithm_util.__unittestL34_5.deepMap!(__lambda4, int[]).deepMap is a nested function and cannot be accessed from algorithm_util.__unittestL34_5.deepMap!(__lambda4, int[][]).deepMap.__lambda2300!(int[]).__lambda2300DMD v2.064 DEBUG deepMap works just fine if I pass in an array that isn't nested, so this will work: auto a = [1, 2, 3]; auto b = a.deepMap!(a => a + 1)(); writeln(b); // "[2, 3, 4]" Can someone explain what is going on? Thank you, Dylan Knutson |
June 12, 2013 Re: Compiler errors and recursive nested functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dylan Knutson | On 06/12/2013 03:45 PM, Dylan Knutson wrote: > Hello! I'm trying to make a deepMap function which operates on the > deepest non-range element of an array. I think that this function should > theoretically work, however there are some compiler errors that I can't > quite understand. > > Here's the code: http://dpaste.dzfl.pl/97918311 > > And the compiler error because DPaste's compiler service seems to be > offline: > > src\algorithm_util.d(26): Error: function > algorithm_util.__unittestL34_5.deepMap!(__lambda4, int[]).deepMap is a > nested function and cannot be accessed from > algorithm_util.__unittestL34_5.deepMap!(__lambda4, > int[][]).deepMap.__lambda2300!(int[]).__lambda2300DMD > v2.064 DEBUG > > deepMap works just fine if I pass in an array that isn't nested, so this > will work: > > auto a = [1, 2, 3]; > auto b = a.deepMap!(a => a + 1)(); > writeln(b); > // "[2, 3, 4]" > > Can someone explain what is going on? > > Thank you, > Dylan Knutson Perhaps this limitation: http://d.puremagic.com/issues/show_bug.cgi?id=5710 Ali |
June 12, 2013 Re: Compiler errors and recursive nested functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dylan Knutson | DPaste has stopped responding to requests at the time of writing, so here's the code in case its still down by the time someone reads this: ---- import std.algorithm : map; import std.range : isInputRange, ElementType; // Doesn't work //template deepMap(alias pred, Range) //if(isInputRange!Range) //{ // static if(isInputRange!(ElementType!Range)) // { // alias deepMap = map!(a => deepMap!(pred, typeof(a))(a)); // } // else // { // alias deepMap = map!(pred); // } //} // Neither does this, for the same reason auto deepMap(alias pred, Range)(Range r) if(isInputRange!Range) { static if(isInputRange!(ElementType!Range)) { return r.map!( a => a.deepMap!(pred)() )(); } else { return r.map!(pred)(); } } unittest { import std.stdio; auto a = [[1, 1], [1, 1]]; auto b = a.deepMap!(a => a + 1)(); writeln(b); //assert(b == [[2, 2], [2, 2]]); } ---- |
June 12, 2013 Re: Compiler errors and recursive nested functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dylan Knutson | On 06/12/2013 03:49 PM, Dylan Knutson wrote: > DPaste has stopped responding to requests at the time of writing, Being an old ;) old-timer I never understood the need for those sites. > ---- > import std.algorithm : map; > import std.range : isInputRange, ElementType; As a workaround, make that lambda a free-standing function here: auto intermediate(T, alias pred)(T a) { return a.deepMap!(pred)(); } > // Neither does this, for the same reason > auto deepMap(alias pred, Range)(Range r) > if(isInputRange!Range) > { > static if(isInputRange!(ElementType!Range)) > { > return r.map!( a => a.deepMap!(pred)() )(); And use that free-standing instead: return r.map!(intermediate!(ElementType!Range, pred))(); Ali |
Copyright © 1999-2021 by the D Language Foundation