View mode: basic / threaded / horizontal-split · Log in · Help
October 16, 2012
Problem with UFCS
Hello!
  I have been using D for a while, but only recently paid 
attention to UFCS. For some reason, I can't convince my code to 
compile properly:

void main() {
	import std.range, std.stdio;
	
	auto sqrs = sequence!((a,n) => n*n)(0);
	auto fibs = recurrence!((a,n) => a[n-1]+a[n-2])(1, 1);
	foreach (v; zip(sqrs, fibs).take(10)) writeln(v);
}

The compiler complains about "undefined identifier 'take'". If I 
rewrite the last line to "take(zip(sqrts, fibs), 10)", the 
problem compiles just fine. Is this a compiler bug, or am I 
missing something obvious? I'm using dmd 2.060 on Windows 7.

Thanks,
Michael
October 16, 2012
Re: Problem with UFCS
On Tuesday, 16 October 2012 at 16:12:06 UTC, Michael wrote:
> void main() {
> 	import std.range, std.stdio;

The problem is that UFCS only works on functions in the global 
scope. The import inside a function makes them local, so it 
doesn't consider them in it.

This is apparently by design; Walter said that just a couple 
weeks ago, though I don't have the link right now and don't 
remember what he said the reason was.

But if you move those imports outside the function the rest of 
your code will work.
October 16, 2012
Re: Problem with UFCS
On Tuesday, October 16, 2012 18:28:14 Adam D. Ruppe wrote:
> On Tuesday, 16 October 2012 at 16:12:06 UTC, Michael wrote:
> > void main() {
> > 
> > import std.range, std.stdio;
> 
> The problem is that UFCS only works on functions in the global
> scope. The import inside a function makes them local, so it
> doesn't consider them in it.
> 
> This is apparently by design; Walter said that just a couple
> weeks ago, though I don't have the link right now and don't
> remember what he said the reason was.
> 
> But if you move those imports outside the function the rest of
> your code will work.

That seems like it goes against the whole "turtles all the way down" principle 
that made it so that local imports were added in the first place. I wonder what 
the reasoning was.

- Jonathan M Davis
October 19, 2012
Re: Problem with UFCS
Thank you guys. I can't believe that haven't thought of that.
I still feel that this is rather odd though. I would expect to be 
able to use UFCS in the scope that imports those libraries. 
Though I can definitely see how that might lead to obscure bugs.

On Tuesday, 16 October 2012 at 17:07:36 UTC, Jonathan M Davis 
wrote:
> On Tuesday, October 16, 2012 18:28:14 Adam D. Ruppe wrote:
>> On Tuesday, 16 October 2012 at 16:12:06 UTC, Michael wrote:
>> > void main() {
>> > 
>> > import std.range, std.stdio;
>> 
>> The problem is that UFCS only works on functions in the global
>> scope. The import inside a function makes them local, so it
>> doesn't consider them in it.
>> 
>> This is apparently by design; Walter said that just a couple
>> weeks ago, though I don't have the link right now and don't
>> remember what he said the reason was.
>> 
>> But if you move those imports outside the function the rest of
>> your code will work.
>
> That seems like it goes against the whole "turtles all the way 
> down" principle
> that made it so that local imports were added in the first 
> place. I wonder what
> the reasoning was.
>
> - Jonathan M Davis
Top | Discussion index | About this forum | D home