Feature request: one-statement functions without brackets

eao197 eao197 at intervale.ru
Mon Apr 2 05:39:21 PDT 2007


On Mon, 02 Apr 2007 14:25:27 +0400, Downs <default_357-line at yahoo.de>  
wrote:

> Feature request: Allow functions of the form function-declaration  
> statement;
> Example: int test() return 5;
>
> This would make the function syntax closer to the way if/else, while,  
> for and similar
> statements' blocks are handled now, potentially making parsing easier.

An interesting example of language syntax simplification can be seen from  
Scala language (http://www.scala-lang.org). In version 1.* Scala used  
semicolons as an expression terminator. That restruction had been removed  
in version 2.*. There is a quote from document 'Changes between Scala  
Version 1.0 and 2.0' (unfortunately this document is not available from  
Scala site now):

<quote>
2 Newlines as Statement Separators

Newlines can now be used as statement separators in place of semicolons. A  
newline is significant as a statement separator if the following three  
conditions are fulfilled:

1. The token immediately preceding the newline can terminate a statement.
2. The token immediately following the newline can begin a statement.
3. The token appears in a region where multiple statements are allowed.

Multiple statements are allowed for the program as a whole and between  
{ ... } braces; they are disallowed between (...) parentheses and between  
[...] brackets.

For instance, a semicolon is now no longer necessary after the println  
call in the function below:

def square(x: int) = {
   println("square called")
   x * x
}

This new convention makes some previously allowed multi-line expressions  
illegal. An example is:

def inAllowedRange(x: int) =
   x >= loBound && // newline significant here!
   x <= hiBound

In these cases it suffices to enclose themulti-line expression in  
parentheses to suppress the generation of a newline token:

def inAllowedRange(x: int) = (
   x >= loBound && // no newline is inserted
   x <= hiBound
)
</quote>

This improvement has made Scala a very short-spoken language. In  
conjunction with other things (like: every expression has a value, type  
inference and optional {} in function body definition) Scala programs have  
less syntax noise than in С++/D or Java. For example, simple getter method  
can be written in Scala as:

def getX = x // return type is infered from expression 'x'.

IMHO, it will be great if D allows semicolons and 'return' keyword  
skipping. Like this:

int getX() { x }

instead of

int getX() { return x; }

-- 
Regards,
Yauheni Akhotnikau



More information about the Digitalmars-d mailing list