digitalmars.D.learn - Problem about lambda expressions
- Tongzhou Li (9/9) Mar 27 2012 Hello again! I'm learning D, and I encountered a problem.
- Tongzhou Li (8/8) Mar 27 2012 Oh, I also tried:
- dennis luehring (4/12) Mar 27 2012 just a question:
- Tongzhou Li (3/17) Mar 28 2012 Well...
- Artur Skawina (12/17) Mar 27 2012 I'm not sure what exactly you're trying to do, but maybe this will help:
- Tongzhou Li (2/23) Mar 28 2012 Yes, this is what I want to do. Thanks!
- Kenji Hara (7/17) Mar 27 2012 (obj x, int a0, int a1) => { x.setxxx(a0); x.setyyy(a1); }
- Tongzhou Li (3/20) Mar 28 2012 I understood. But why it compiles instead of giving an error?
- Tongzhou Li (4/6) Mar 28 2012 Oh, I mean if I write the wrong code, what objectcode does the
- Timon Gehr (4/10) Mar 28 2012 (int a) => {return a;}
- Tongzhou Li (4/17) Mar 28 2012 I understand.
- Timon Gehr (2/20) Mar 28 2012 Yes.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/15) Mar 27 2012 printed.
Hello again! I'm learning D, and I encountered a problem. I tried this code: http://ideone.com/hkpT6 It works well. (Have no idea why codepad.org failed to compile it) I tried to write a lambda instead of function f, but I got nothing printed. Did I make something wrong? Compiler used: DMD32 D Compiler v2.058 (Win7 SP1 x64) Sorry for my poor English :)
Mar 27 2012
Oh, I also tried: void seq_apply(Params..., Args...)(void delegate(Params) func, Args args) But I got a error: variadic template parameter must be last Does it mean that there can only be one variadic template parameter? How to fix it? Thanks
Mar 27 2012
Am 27.03.2012 15:52, schrieb Tongzhou Li:Oh, I also tried: void seq_apply(Params..., Args...)(void delegate(Params) func, Args args) But I got a error: variadic template parameter must be last Does it mean that there can only be one variadic template parameter? How to fix it? Thanksjust a question: how on earth should the compiler seperate your 2 variadic parameters??? t( a,b,c,x,y,z ) -> where Params Start/End, where Args???? magic?
Mar 27 2012
On Tuesday, 27 March 2012 at 14:12:38 UTC, dennis luehring wrote:Am 27.03.2012 15:52, schrieb Tongzhou Li:Well... It's a question...Oh, I also tried: void seq_apply(Params..., Args...)(void delegate(Params) func, Args args) But I got a error: variadic template parameter must be last Does it mean that there can only be one variadic template parameter? How to fix it? Thanksjust a question: how on earth should the compiler seperate your 2 variadic parameters??? t( a,b,c,x,y,z ) -> where Params Start/End, where Args???? magic?
Mar 28 2012
On 03/27/12 15:52, Tongzhou Li wrote:Oh, I also tried: void seq_apply(Params..., Args...)(void delegate(Params) func, Args args) But I got a error: variadic template parameter must be last Does it mean that there can only be one variadic template parameter? How to fix it?I'm not sure what exactly you're trying to do, but maybe this will help: void seq_apply(Func, Args...)(Func func, Args args) { import std.traits; alias ParameterTypeTuple!Func Params; enum ArgNum = Params.length-1; func(args[0], args[1 .. ArgNum + 1]); static if (args.length > ArgNum + 1) { seq_apply(func, args[ArgNum + 1 .. args.length]); } } artur
Mar 27 2012
On Tuesday, 27 March 2012 at 14:54:11 UTC, Artur Skawina wrote:On 03/27/12 15:52, Tongzhou Li wrote:Yes, this is what I want to do. Thanks!Oh, I also tried: void seq_apply(Params..., Args...)(void delegate(Params) func, Args args) But I got a error: variadic template parameter must be last Does it mean that there can only be one variadic template parameter? How to fix it?I'm not sure what exactly you're trying to do, but maybe this will help: void seq_apply(Func, Args...)(Func func, Args args) { import std.traits; alias ParameterTypeTuple!Func Params; enum ArgNum = Params.length-1; func(args[0], args[1 .. ArgNum + 1]); static if (args.length > ArgNum + 1) { seq_apply(func, args[ArgNum + 1 .. args.length]); } } artur
Mar 28 2012
On Tuesday, 27 March 2012 at 13:42:30 UTC, Tongzhou Li wrote:Hello again! I'm learning D, and I encountered a problem. I tried this code: http://ideone.com/hkpT6 It works well. (Have no idea why codepad.org failed to compile it) I tried to write a lambda instead of function f, but I got nothing printed. Did I make something wrong? Compiler used: DMD32 D Compiler v2.058 (Win7 SP1 x64) Sorry for my poor English :)(obj x, int a0, int a1) => { x.setxxx(a0); x.setyyy(a1); } This lambda expression returns *a delegate has no parameter*. Instead: (obj x, int a0, int a1) => (x.setxxx(a0), x.setyyy(a1)) or: (obj x, int a0, int a1){ x.setxxx(a0); x.setyyy(a1); }
Mar 27 2012
On Tuesday, 27 March 2012 at 15:21:57 UTC, Kenji Hara wrote:On Tuesday, 27 March 2012 at 13:42:30 UTC, Tongzhou Li wrote:I understood. But why it compiles instead of giving an error? What does the complier do when I write the wrong code?Hello again! I'm learning D, and I encountered a problem. I tried this code: http://ideone.com/hkpT6 It works well. (Have no idea why codepad.org failed to compile it) I tried to write a lambda instead of function f, but I got nothing printed. Did I make something wrong? Compiler used: DMD32 D Compiler v2.058 (Win7 SP1 x64) Sorry for my poor English :)(obj x, int a0, int a1) => { x.setxxx(a0); x.setyyy(a1); } This lambda expression returns *a delegate has no parameter*. Instead: (obj x, int a0, int a1) => (x.setxxx(a0), x.setyyy(a1)) or: (obj x, int a0, int a1){ x.setxxx(a0); x.setyyy(a1); }
Mar 28 2012
On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:I understood. But why it compiles instead of giving an error? What does the complier do when I write the wrong code?Oh, I mean if I write the wrong code, what objectcode does the compiler generate? My English is not good, sorry.
Mar 28 2012
On 03/28/2012 10:28 AM, Tongzhou Li wrote:On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:(int a) => {return a;} Is the same thing as (int a){return ()=>a;}I understood. But why it compiles instead of giving an error? What does the complier do when I write the wrong code?Oh, I mean if I write the wrong code, what objectcode does the compiler generate? My English is not good, sorry.
Mar 28 2012
On Wednesday, 28 March 2012 at 10:17:15 UTC, Timon Gehr wrote:On 03/28/2012 10:28 AM, Tongzhou Li wrote:I understand. { return a; } is a function which requires no params right?On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:(int a) => {return a;} Is the same thing as (int a){return ()=>a;}I understood. But why it compiles instead of giving an error? What does the complier do when I write the wrong code?Oh, I mean if I write the wrong code, what objectcode does the compiler generate? My English is not good, sorry.
Mar 28 2012
On 03/28/2012 01:13 PM, Tongzhou Li wrote:On Wednesday, 28 March 2012 at 10:17:15 UTC, Timon Gehr wrote:Yes.On 03/28/2012 10:28 AM, Tongzhou Li wrote:I understand. { return a; } is a function which requires no params right?On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:(int a) => {return a;} Is the same thing as (int a){return ()=>a;}I understood. But why it compiles instead of giving an error? What does the complier do when I write the wrong code?Oh, I mean if I write the wrong code, what objectcode does the compiler generate? My English is not good, sorry.
Mar 28 2012
On 03/27/2012 06:42 AM, Tongzhou Li wrote:Hello again! I'm learning D, and I encountered a problem. I tried this code: http://ideone.com/hkpT6 It works well. (Have no idea why codepad.org failed to compile it) I tried to write a lambda instead of function f, but I got nothingprinted. The lambda syntax (=>) is only for when there is a single return statement in the function literal: http://dlang.org/expression.html#Lambda As Kenji Hara has shown, you can have more than one expression in the return statement by taking advantage of the comma operator but personally I would use something else instead: the longer syntax, a local function, etc. Ali
Mar 27 2012