www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Closures not yet supported in CTFE's

reply "JS" <js.mdnq gmail.com> writes:
When I try to pass a lambda to a template, I get the subject 
error.

(string x) => { return x~"1"; }

But when I jsut do

(string x) => x~"1"

it works....

Come on!!!
Jul 27 2013
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Jul 27, 2013 at 06:47:04PM +0200, JS wrote:
 When I try to pass a lambda to a template, I get the subject error.
 
 (string x) => { return x~"1"; }
Drop the "=>", that syntax doesn't do what you think it does.
 But when I jsut do
 
 (string x) => x~"1"
 
 it works....
 
 Come on!!!
Y'know, it would really help if you (1) show the full code that's failing, and (2) calm down before posting to the forum. I understand that compiler bugs/limitations can be very aggravating, but you're much more likely to get a helpful reply that way. T -- He who does not appreciate the beauty of language is not worthy to bemoan its flaws.
Jul 27 2013
parent reply "JS" <js.mdnq gmail.com> writes:
On Saturday, 27 July 2013 at 17:18:29 UTC, H. S. Teoh wrote:
 On Sat, Jul 27, 2013 at 06:47:04PM +0200, JS wrote:
 When I try to pass a lambda to a template, I get the subject 
 error.
 
 (string x) => { return x~"1"; }
Drop the "=>", that syntax doesn't do what you think it does.
 But when I jsut do
 
 (string x) => x~"1"
 
 it works....
 
 Come on!!!
Y'know, it would really help if you (1) show the full code that's failing, and (2) calm down before posting to the forum. I understand that compiler bugs/limitations can be very aggravating, but you're much more likely to get a helpful reply that way.
The code wouldn't help because that is not where the error is at. It is because I thought => was used for lambda's as it is in C#. I don't know why there is a special syntax for a direct return. (string x) => { return x; } is trying to return a function that returns x using closure because of x is outside the inside function. (string x) { return x; } is what I was trying to do. I knew that (string x) => ...; was simple notation for return ... but didn't think I was returning a function in any way.
Jul 27 2013
next sibling parent reply "Meta" <jared771 gmail.com> writes:
On Saturday, 27 July 2013 at 23:54:28 UTC, JS wrote:
 The code wouldn't help because that is not where the error is 
 at. It is because I thought => was used for lambda's as it is 
 in C#. I don't know why there is a special syntax for a direct 
 return.

 (string x) => { return x; } is trying to return a function that 
 returns x using closure because of x is outside the inside 
 function.

 (string x) { return x; } is what I was trying to do. I knew 
 that (string x) => ...; was simple notation for return ... but 
 didn't think I was returning a function in any way.
It was a source of confusion for me at first as well, as I expected `a => { ... }` to behave as it does in C#. That's not the case, unfortunately, as { ... } is a delegate literal in D, so you have to use the full `(a) { ... }` syntax. It's annoying, but somewhat more bearable if you've worked with Javascript or LUA before, since they have pretty much the same syntax for function literals.
Jul 27 2013
parent "anonymous" <anonymous example.com> writes:
On Sunday, 28 July 2013 at 01:17:00 UTC, Meta wrote:
[...]
 It was a source of confusion for me at first as well, as I 
 expected `a => { ... }` to behave as it does in C#. That's not 
 the case, unfortunately, as { ... } is a delegate literal in D, 
 so you have to use the full `(a) { ... }` syntax.
That "full" syntax is (a tiny tad) shorter than C#'s though: (a) { ... } a => { ... }
Jul 27 2013
prev sibling parent "anonymous" <anonymous example.com> writes:
On Saturday, 27 July 2013 at 23:54:28 UTC, JS wrote:
 On Saturday, 27 July 2013 at 17:18:29 UTC, H. S. Teoh wrote:
[...]
 Y'know, it would really help if you (1) show the full code 
 that's
 failing,
[...]
 The code wouldn't help because that is not where the error is 
 at.
It's not so much about providing the full source of your project, but more about a reduced test case, a little snippet that compiles (or should compile in your opinion). It lowers the barrier for helpers quite a bit.
 It is because I thought => was used for lambda's as it is in C#.
I don't know C#. So I read a little MSDN[1] to find out what (foo) => {bar} means in C#. Apparently, it's a delegate that takes foo and does bar. In D: (foo) {bar} However, D's "=>" syntax works the same as C#'s Expression Lambdas.
 I don't know why there is a special syntax for a direct return.
Not sure, I'm understanding this right. You mean, why do we need the arrow syntax when we can do the same thing with the braces syntax? Well, because it's shorter. C# does it too, doesn't it (Expression Lambdas vs Statement Lambdas)? It's just that D has a different syntax for Statement Lambdas than C#.
 (string x) => { return x; } is trying to return a function that 
 returns x using closure because of x is outside the inside 
 function.

 (string x) { return x; } is what I was trying to do. I knew 
 that (string x) => ...; was simple notation for return ... but 
 didn't think I was returning a function in any way.
I feel like you may have missed this: In D (string x) => x is equivalent to (string x) { return x; } [1] http://msdn.microsoft.com/en-US/library/vstudio/bb397687.aspx
Jul 27 2013