www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Minor feature request

reply Superstar64 <Hexagonalstar64 gmail.com> writes:
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
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
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
prev sibling next sibling parent Gary Willoughby <dev nomad.so> writes:
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
prev sibling next sibling parent Lodovico Giaretta <lodovico giaretart.net> writes:
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
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
On Tuesday, 28 June 2016 at 11:22:38 UTC, Steven Schveighoffer 
wrote:
 We pretty much have this with lambda syntax:

 (int arg) => arg
alias func = (int i) => i*i; ?
Jun 28 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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:
 We pretty much have this with lambda syntax:

 (int arg) => arg
alias 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. -Steve
Jun 28 2016
parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= writes:
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:
 alias 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.
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/5d35ab068c2b
Jun 28 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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:
 On 6/28/16 7:35 AM, Ola Fosheim Grøstad wrote:
 alias 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.
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/5d35ab068c2b
That's pretty cool. Unfortunately, it's still a delegate/function. I was thinking to define actual functions this way. -Steve
Jun 28 2016
parent ZombineDev <petar.p.kirov gmail.com> writes:
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:
 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:
 alias 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.
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/5d35ab068c2b
That's pretty cool. Unfortunately, it's still a delegate/function. I was thinking to define actual functions this way. -Steve
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); } }
Jun 28 2016