2 Apr 2006 14:03
Re: question about syntax
Lex Spoon <lex <at> cc.gatech.edu>
2006-04-02 12:03:41 GMT
2006-04-02 12:03:41 GMT
Ittay Dror <ittayd <at> qlusters.com> writes:
> I've been reading about Scala and I'm finding it is very interesting. However, I'm confused by the syntax
of functions:
>
> - 'def v: int = 1' defines a function that returns an int (right?),
> but the signature (for accepting it in other functions) is () =>
> int. why then not define 'def v: () => int = 1'? that way, it is
> compatible with defining a variable 'var v: int = 1'
>
> - 'def v(x: int)(y: int): int = ...' defines a function that accepts
> an int and returns a function that accepts an int and returns an
> int. why then not use the syntax 'def v(x: int) : int => int = ...',
> or 'def v: int => (int => int) = ...'
A def always gives you a type Left=>Right for some Left and Right. So
with:
def v: int = 1
Right is obviously int. Left, in this case, is the empty argument
list () .
For your def v example, you could in fact write it out longer.
def v(x: int)(y: int): int = ... /* original */
def v(x: int): int=>int = y:int => ... /* modified */
Notice that the first one has a (y: int) before the equals sign
while the second one has "y:int =>" after the equals sign.
> - constructors: how can a class have more than one constructor?
Simply define methods named "this". For example:
class Point(x: int, y: int) {
def this() = this(0,0) // by default, set x and y to 0
}
-Lex
RSS Feed