Re: How to hand in a closure variable

2014-04-07 Thread Steve Teale
On Friday, 4 April 2014 at 15:15:55 UTC, bearophile wrote: Bienlein: What I was actually looking for was how to get this to work: immutable int b = if(1 == 1) { return 123; } else { return 456; }; immutable b = (1 == 1) ? 123 : 456; Bye, bearophile You said you did not like ternary expre

Re: How to hand in a closure variable

2014-04-07 Thread Bienlein
On Friday, 4 April 2014 at 19:56:14 UTC, Jesse Phillips wrote: On Friday, 4 April 2014 at 15:13:25 UTC, Bienlein wrote: What I was actually looking for was how to get this to work: immutable int b = if(1 == 1) { return 123; } else { return 456; }; But I'm happy enough with the solution throu

Re: How to hand in a closure variable

2014-04-04 Thread Jesse Phillips
On Friday, 4 April 2014 at 15:13:25 UTC, Bienlein wrote: What I was actually looking for was how to get this to work: immutable int b = if(1 == 1) { return 123; } else { return 456; }; But I'm happy enough with the solution through a delegate. What bearophile said, or: immutable int b = {i

Re: How to hand in a closure variable

2014-04-04 Thread bearophile
Bienlein: What I was actually looking for was how to get this to work: immutable int b = if(1 == 1) { return 123; } else { return 456; }; immutable b = (1 == 1) ? 123 : 456; Bye, bearophile

Re: How to hand in a closure variable

2014-04-04 Thread Bienlein
On Friday, 4 April 2014 at 13:53:33 UTC, bearophile wrote: If your D function has one argument, you have to give it one argument, even if it doesn't have a visible name and it's unused. Ah! Admittedly, I though it's the return type .. So this works now: immutable int b = () { if(1 ==

Re: How to hand in a closure variable

2014-04-04 Thread bearophile
Bienlein: Whereas this does not compile: immutable int b = (int) { if(1 == 1) { return 123; } else { return 456; } }(); // line x However, this does compile and displays correctly 123: immutable int b = (int) { if(1 == 1) {

Re: How to hand in a closure variable

2014-04-04 Thread Bienlein
Thanks so far. I have another one, though. Not trying to tease people, I really don't know ;-). This compiles and runs: immutable int a = (int val) { if(1 == 1) { return val; } else { return 456; } }(123); writeln(a); Whereas th

Re: How to hand in a closure variable

2014-03-24 Thread Matej Nanut
Hello! You just missed the syntax a little. Instead of: > int delegate(int) dg = { value => return value + a + 3; }; You can write auto dg = (int value) { return value + a + 3; }; // Omitted return type, but had to specify type of value. or auto dg = (int value) => value + a + 3; //

Re: How to hand in a closure variable

2014-03-24 Thread Dicebot
On Monday, 24 March 2014 at 16:40:55 UTC, Bienlein wrote: Now I want the closure (aka delegate) to have a closure variable: int a = 7; int delegate(int) dg = { value => return value + a + 3; }; auto result = dg(123); Unhappily, the code above doesn't compile. Tried various things, looked for

How to hand in a closure variable

2014-03-24 Thread Bienlein
Hello, I have some piece of code that compiles and runs fine: void main(string[] args) { int a = 7; int delegate() dg = { return a + 3; }; auto result = dg(); writeln(result); } Now I want the closure (aka delegate) to have a closure variable: int a = 7;