Function definition

Ivan Čukić ivan.cukic at kde.org
Fri Jul 25 16:11:02 UTC 2014


Hi all,

This is one of the things I was thinking about. (I completely understand why 
C++ committee ended up with a not so terse syntax for lambdas :) )

Full syntax:

fun functionName(arg1, arg2, ...) -> Type
    when arg1 > 0 && arg2 < 0,
    tailrec, receiver, test, other_tags_or_annotations
{
    :::
}

Starting comments:

 - Return type is optional and easily parsed. -> is from C++ (and others), it 
could be a colon (:D) or similar.

 - Argument types: because of fun/let and similar, I think that right-side 
types should be preferred. (if Java devs are able to switch to Scala's 
number: Int, I guess c++ people could as well). So:
    let i: Int = 0  // type would be deduced, if not specified (it would not 
be specified most of the time)
    fun f(i: Int, s: String) -> String
A possible alternative would be (I'm just not used to it)
   let Int i = 0
   fun String f(Int i, String s)

 - If we had a special syntax for tags and annotations - something like 
C++11's [[something, something]] or Java's @something, @something, we could 
allow the them in the front as well as after the function head without much 
parsing problems. Something like:
    [[receiver, when a>0]] fun f(a)
as well as
    fun f(a) [[receiver, when a>0]]
    [[receiver]] f(a) [[when a>0]]
but this might be an overkill.

 - Lambdas / Anonymous functions - just skip the functionName (in the 
following examples as well)

 - If the body is a single expression (might be more complicated, but limit to 
this for the time being), and there isn't a type nor tags, we could omit 
brackets like this:
    fun functionName(a,b,c) a + b + c // though this does look strange
    fun (a,b,c) a + b + c // but bear with me

 - If using jokers, the arguments can be omitted as well:
    fun _1 + _2 + _3 // from C++ - std::placeholders
    fun _ + _ + _ // equivalent to the above
    fun _1 + _2 + _1 // if one needs to repeat an argument

 - If we can deduce that a required type at some point is a function, we might 
allow one to skip the 'fun' keyword:
    let product = list.foldl(1, _ * _)
instead of:
    let product = list.foldl(1, fun(acc, i) acc * i)
or, instead of this, if we skip all the terseness modifications:
    let product = list.foldl(1, fun(acc, i) { acc * i; })


Cheerio,
Ivan


KDE, ivan.cukic at kde.org, http://ivan.fomentgroup.org/ 
gpg key id: 850B6F76, keyserver.pgp.com


More information about the Funq-devel mailing list