optional (), what is done elsewhere

Jacob Carlborg doob at me.com
Mon Feb 4 02:06:36 PST 2013


On 2013-02-04 04:34, deadalnix wrote:
> I finally did my homework. Many languages have been presented as example
> of optional parenthesis, and not posing ambiguity problems. As I'm not a
> specialist of such languages, I wanted to do some homework and check
> what is the reality.
>
> See for instance
> http://forum.dlang.org/thread/doeutcaammauwgaskawl@forum.dlang.org?page=2#post-ju0d0o:241bvh:241:40digitalmars.com
> for example of such claim.
>
> 1/ Ruby.
>
> In Ruby, method are called without (). This is not ambiguous as fields
> are always private.

I can elaborate a bit about Ruby. In Ruby it's possible to call a method 
with parentheses regardless if it takes arguments or not.

It's correct that fields are always private. Inside a class fields 
always start with an at sign, so there's conflict there. But there is a 
chance of conflict for methods called without parentheses and local 
variables. Example:

class Bar
   def foo; end

   def bar
     foo = 3
     a = foo # local variable
   end
end

In the above example, if a local variable is declared with the same name 
as a method it will always refer to the local variable, just as in D.

Callable objects. There is no conflict with callable objects since those 
use a different syntax to invoke the object:

foo = lambda { }
foo.call


> 4/ Coffescript
>
> Example given on that page http://coffeescript.org/ tends to show that
> the behavior is similar to scala's. Note function square that is stored
> into a structure as a function, and not evaluated.

In CoffeeScript it's only legal to call a function without parentheses 
if it takes at least one argument. Example:

foo = -> console.log "asd"

bar = foo # refers to the "foo" function
bar = foo() # calls the "foo" function

x = (y) -> y * 2

bar = x # refers to the "x" function
x 3 # calls the "x" function with argument 3
x(3) # calls the "x" function with argument 3

So with CoffeeScript there's never a conflict.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list