www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7176] New: Lambda => syntax for function and methods too

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7176

           Summary: Lambda => syntax for function and methods too
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2011-12-28 05:11:07 PST ---
(From an idea of Timon Gehr)

I think extending the applicability of the new lambda syntax to free
functions/methods is a nice idea, to shorten tiny functions/methods, that are
common enough:


struct C {
    int x;
    int getX() => x;
    int sqrX() => x ^^ 2;
}


Some examples from other languages:

A function to compute the arithmetic mean in Scala 2.7:

def mean(s: Seq[Int]) = s.foldLeft(0)(_+_) / s.size


From the Ada 2012 changes:
http://www.disca.upv.es/jorge/ae2010/slides/05-3_Language_Tech_Schonberg_Towards_Ada_2012.pdf

 To simplify the writing of pre/postconditions and predicates, allow
parametrized expressions (aka function bodies in package specs):

function Cube (X : integer) is (X ** 3); -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 28 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7176



--- Comment #1 from bearophile_hugs eml.cc 2012-01-03 16:07:57 PST ---
There are 3 votes now. But this feature doesn't add a lot to D. This feature
looks nice, but I don't feel a need for it in my code.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 03 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7176


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg gmx.com


--- Comment #2 from Jonathan M Davis <jmdavisProg gmx.com> 2012-01-03 16:28:36
PST ---
It would be too much of a departure from the normal syntax to enable the new
lambda syntax in general IMHO. It's useful for lambdas simply because without
it they risk being very verbose in what is already fairly dense code. Function
declarations don't really have that problem. Yes, the syntax is a bit verbose
if all you're doing is returning a value, but most functions do more than that,
and most functions are not declared in the midst of dense code like you
typically get with lambdas. This enhancement request is such a drastic
departure from the normal C-based syntax that I think that it would cause far
more harm than good.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 03 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7176


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr gmx.ch


--- Comment #3 from timon.gehr gmx.ch 2012-01-03 16:40:47 PST ---
It is the same 'departure' as the one caused by the introduction of the new
lambda literals and therefore I cannot see how it can possibly cause any harm.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 03 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7176



--- Comment #4 from Jonathan M Davis <jmdavisProg gmx.com> 2012-01-03 16:48:12
PST ---
It's very different IMHO to introduce it in lambdas which are already part of
an expression and where the number of characters definitely matters than it is
to introduce it in normal function declarations. With declarations, they're on
their own instead of part of a larger expression. They just don't present the
same kind of gain and therefore don't merit the cost of the large departure in
syntax IMHO.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 03 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7176



--- Comment #5 from bearophile_hugs eml.cc 2012-01-03 16:54:10 PST ---
(In reply to comment #2)

Currently I am neutral toward this feature. I see it used in Scala and it looks
nice, but I don't think it will improve my D programs a lot.


 This enhancement request is such a drastic
 departure from the normal C-based syntax that I think that it would cause far
 more harm than good.

What kind of harm are you referring to? I think it's not significantly bug-prone, and being already present in the language (as lambda syntax) doesn't add a lot of complexity for the person that has to learn D. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 03 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7176



--- Comment #6 from Jonathan M Davis <jmdavisProg gmx.com> 2012-01-03 19:10:35
PST ---
 What kind of harm are you referring to?

It doesn't fit with the rest of the language. The syntax is very different from other declarations. This reduces readability and increases how much the programmer has to deal with. The verboseness of lambda expressions is a definite problem for readability, so the syntax is arguably worth it for lambda expressions. But to then also use it in declarations which don't have the same readibility problem is incurring that cost where it's not worth it IMHO. Obviously, this is perfectly valid as an enhancement request, but I hope that the request is denied. I think that the lambda syntax is too different from typical C-based syntax to be reasonable in normal function declarations. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 03 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7176


Jacob Carlborg <doob me.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |doob me.com


--- Comment #7 from Jacob Carlborg <doob me.com> 2012-01-03 23:30:48 PST ---
I could really have a use for this. I have a lot of methods that just returns a
single expression.

Another idea would be to allow optional braces for methods and functions, just
as for if-statements. This could be extended to all language features where
braces are used to make it more consistent.

In addition to the above we could make implicit returns possible to all
functions and methods.

I don't know which of these two ideas are farthest away from the normal C-based
syntax.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 03 2012