digitalmars.D - Minor feature request
- Superstar64 (20/20) Jun 27 2016 Right now, D functions expect a Block Statement
- rikki cattermole (4/24) Jun 27 2016 The only one I'm ok with is with, since it is the most similar to what
- Gary Willoughby (2/17) Jun 28 2016 Please no! All of these are awful.
- Lodovico Giaretta (8/28) Jun 28 2016 Incidentally, yesterday I stumbled upon a very long C++ function
- Steven Schveighoffer (8/19) Jun 28 2016 We pretty much have this with lambda syntax:
- Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= (4/6) Jun 28 2016 alias func = (int i) => i*i;
- Steven Schveighoffer (4/10) Jun 28 2016 Is that valid in the compiler, or are you proposing it? I haven't used
- Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= (12/20) Jun 28 2016 It does work:
- Steven Schveighoffer (4/24) Jun 28 2016 That's pretty cool. Unfortunately, it's still a delegate/function.
- ZombineDev (23/54) Jun 28 2016 It was added with DMD 2.070
Right now, D functions expect a Block Statement (https://dlang.org/spec/function.html#FunctionBody) as their function body. Changing that to allow any statement (https://dlang.org/spec/statement.html#Statement) would provide a few new syntactic sugars like: --- auto func(MyObj obj) with(obj) { //... } auto func(int arg) return arg; auto func() try { //... } finally { return //... } ---
Jun 27 2016
On 28/06/2016 6:13 PM, Superstar64 wrote:Right now, D functions expect a Block Statement (https://dlang.org/spec/function.html#FunctionBody) as their function body. Changing that to allow any statement (https://dlang.org/spec/statement.html#Statement) would provide a few new syntactic sugars like: --- auto func(MyObj obj) with(obj) { //... } auto func(int arg) return arg; auto func() try { //... } finally { return //... } ---The only one I'm ok with is with, since it is the most similar to what we already have. The others, no thanks. Two extra characters won't hurt you.
Jun 27 2016
On Tuesday, 28 June 2016 at 06:13:44 UTC, Superstar64 wrote:--- auto func(MyObj obj) with(obj) { //... } auto func(int arg) return arg; auto func() try { //... } finally { return //... } ---Please no! All of these are awful.
Jun 28 2016
On Tuesday, 28 June 2016 at 06:13:44 UTC, Superstar64 wrote:Right now, D functions expect a Block Statement (https://dlang.org/spec/function.html#FunctionBody) as their function body. Changing that to allow any statement (https://dlang.org/spec/statement.html#Statement) would provide a few new syntactic sugars like: --- auto func(MyObj obj) with(obj) { //... } auto func(int arg) return arg; auto func() try { //... } finally { return //... } ---Incidentally, yesterday I stumbled upon a very long C++ function whose body was a try/catch statement instead of a block statement. It took me some time to figure out what was going on, and, although I'm usually ok with these kind of language "features", I have to say that this can really harm readability, because flow control is no longer indented with respect to the declaration.
Jun 28 2016
On 6/28/16 2:13 AM, Superstar64 wrote:Right now, D functions expect a Block Statement (https://dlang.org/spec/function.html#FunctionBody) as their function body. Changing that to allow any statement (https://dlang.org/spec/statement.html#Statement) would provide a few new syntactic sugars like: --- auto func(MyObj obj) with(obj) { //... } auto func(int arg) return arg;We pretty much have this with lambda syntax: (int arg) => arg Would be nice to be able to use this to define functions... However, I don't want to change the definition of functions to allow non-blocks. It harms readability. The lambda syntax is different enough and restricted enough to stand out. -Steve
Jun 28 2016
On Tuesday, 28 June 2016 at 11:22:38 UTC, Steven Schveighoffer wrote:We pretty much have this with lambda syntax: (int arg) => argalias func = (int i) => i*i; ?
Jun 28 2016
On 6/28/16 7:35 AM, Ola Fosheim Grøstad wrote:On Tuesday, 28 June 2016 at 11:22:38 UTC, Steven Schveighoffer wrote:Is that valid in the compiler, or are you proposing it? I haven't used or seen such a thing. -SteveWe pretty much have this with lambda syntax: (int arg) => argalias func = (int i) => i*i; ?
Jun 28 2016
On Tuesday, 28 June 2016 at 13:50:42 UTC, Steven Schveighoffer wrote:On 6/28/16 7:35 AM, Ola Fosheim Grøstad wrote:It does work: ---- import std.stdio; alias func1 = (int i) => i*i; alias func2 = function int (int i){ return i+i;}; void main(){ writeln(func1(3), " ", func2(4), " ", func1); } ---- https://dpaste.dzfl.pl/5d35ab068c2balias func = (int i) => i*i; ?Is that valid in the compiler, or are you proposing it? I haven't used or seen such a thing.
Jun 28 2016
On 6/28/16 10:07 AM, Ola Fosheim Grøstad wrote:On Tuesday, 28 June 2016 at 13:50:42 UTC, Steven Schveighoffer wrote:That's pretty cool. Unfortunately, it's still a delegate/function. I was thinking to define actual functions this way. -SteveOn 6/28/16 7:35 AM, Ola Fosheim Grøstad wrote:It does work: ---- import std.stdio; alias func1 = (int i) => i*i; alias func2 = function int (int i){ return i+i;}; void main(){ writeln(func1(3), " ", func2(4), " ", func1); } ---- https://dpaste.dzfl.pl/5d35ab068c2balias func = (int i) => i*i; ?Is that valid in the compiler, or are you proposing it? I haven't used or seen such a thing.
Jun 28 2016
On Tuesday, 28 June 2016 at 15:03:01 UTC, Steven Schveighoffer wrote:On 6/28/16 10:07 AM, Ola Fosheim Grøstad wrote:It was added with DMD 2.070 (http://dlang.org/changelog/2.070.0.html#alias-funclit) and helps unify normal aliases with alias template parameters. However similar to member delegates, it does not have access to the enclosing aggregate's members. struct S { int x; // Doesn't work: auto inc = () => x++; // Neither does this: alias add = (amount) => x += x = cast(int)(x + amount); void memberFun() // But this is ok: { alias add = (amount) => x = cast(int)(x + amount); add('1'); add(2UL); add(3.5f); add(4.0); } }On Tuesday, 28 June 2016 at 13:50:42 UTC, Steven Schveighoffer wrote:That's pretty cool. Unfortunately, it's still a delegate/function. I was thinking to define actual functions this way. -SteveOn 6/28/16 7:35 AM, Ola Fosheim Grøstad wrote:It does work: ---- import std.stdio; alias func1 = (int i) => i*i; alias func2 = function int (int i){ return i+i;}; void main(){ writeln(func1(3), " ", func2(4), " ", func1); } ---- https://dpaste.dzfl.pl/5d35ab068c2balias func = (int i) => i*i; ?Is that valid in the compiler, or are you proposing it? I haven't used or seen such a thing.
Jun 28 2016