On 2011-07-15 17:16, Andrei Alexandrescu wrote:
On 7/15/11 7:58 AM, Jacob Carlborg wrote:
I'm not very familiar with Scala but I found this:
http://blog.darevay.com/2009/01/remedial-scala-interpreting-scala-from-scala/


Interesting. What are the features that do make a DSL better looking in
Scala than in D?


Thanks,

Andrei

This is just what I think (I've thrown in Ruby and CoffeeScript as well)

Scala:

* Delegate syntax - Scala allows passing a delegate to a syntax like this:

loop {
   // code
}

This allows to create, what look likes, new statements. I don't recall how the "loop" function should be implemented but it was quite complex and had something do with partial functions.

Scala also has this nice delegate syntax:

foo(a => a + a)

* Infix operators - Allows to call any method that takes one argument without parentheses and without the dot:

object.func("asd")

Can be called like this:

object func "asd"

* Naming methods - In Scala you can name a method "+", "-", "*" and similar. You are not limited to A-Za-z_0-9. Together with infix operators this is how Scala implements operator overloading. This also allows to add new operators.

* No semicolons
* Is in general very good at inferring types, including return types
* Almost everything is an expression
* The constructor is built-in the class declaration:

class Foo
{
    // this is the constructor
    def foo = "foo"
}

Ruby:

* Methods can be called without parentheses
* Class bodies can execute code
* Not limited to top level declarations
* No semicolons
* Delegate/block syntax - In Ruby you passes blocks to methods after the regular arguments:

foo(3, 4) do |a|
   # do something with a
end

Or the one-line syntax:

foo(3, 4) { |a| }


* Simplified hash literal syntax - The standard hash syntax in Ruby is:

a = { key => value }

But if you call a method with hash literal you can most of the times omit the braces:

foo(key => value)

* Almost everything is an expression - This is possible

b = if a == 3
    4
else
    5
end

* Trailing if statements

foo(3) if a == 4

CoffeeScript:

* Methods can be called without parentheses (if they take at least on argument)
* Almost everything is an expression
* Trailing if statements
* No semicolons
* Class bodies can execute code
* Not limited to top level declarations
* Keywords for "or", "and", "yes", "no" and "not".
* Delegate/functoin syntax - Example:

foo (a, b) -> a + b

* Hash/object syntax - Example:

hash =
    foo: 1
    bar: 2

hash = foo: 1, bar: 2

CoffeeScript: http://jashkenas.github.com/coffee-script/

--
/Jacob Carlborg

Reply via email to