The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Scala is not friendly with space, and if-without-else

4 replies on 1 page. Most recent reply: Dec 20, 2008 1:36 PM by Seth Tisue

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Haixu Huang

Posts: 6
Nickname: regular
Registered: Oct, 2008

Scala is not friendly with space, and if-without-else Posted: Dec 15, 2008 9:33 PM
Reply to this message Reply
Advertisement
For example, if I defined

class A(int u) {
val unary_=u
def +(A a) = {new A(unary_+a.unary_)}
def unary_+(A a) = { a }
}

OK, I confess it's a very badly format. But a language should permit a genius or a dull fish write this kind of code to do some useless thing.

So I think maybe use some magic format to define the operator should be acceptable. For example,

class A(int u) {
val unary_=u // unary_ is a member of class A
def {_+_}(A a) = {new A(unary_+a.unary_)}
// code above defined a plus operator
def {+_}(A a) = { a }
// code above defined a prefix unary + operator
private def factorial(int i) =
{ if (i == 1) 1 else i * factorial(i - 1) }
def {_!}(A a) = {new A(factorial(unary_))}
// code above defined a suffix unary ! operator
}

Also, version 6 of Book omited the discuss of difference between

if (true | false) value

and

if (true | false) value1 else value2


Does it mean anything? In Scala v 2.7.2, the first one still produce Unit no matter the expression is true or false, will it be changed in next version? Waiting for the answer here.


Alex Cruise

Posts: 3
Nickname: arrgh
Registered: Dec, 2007

Re: Scala is not friendly with space, and if-without-else Posted: Dec 16, 2008 10:57 AM
Reply to this message Reply
> Also, version 6 of Book omited the discuss of difference
> between
> if (true | false) value
> and
> if (true | false) value1 else value2
>
> Does it mean anything? In Scala v 2.7.2, the first one
> still produce Unit no matter the expression is true or
> false, will it be changed in next version? Waiting for the
> answer here.

If you use if as an expression that returns a value, the inferred type of the expression is the least upper bound of the "then" and "else" clauses. Since the else clause doesn't return a value, Unit is the LUB. If you use an if imperatively (e.g. if (condition) mutateMyState()) everything will work as you'd expect because you don't care about the return value anyway.

Alex Cruise

Posts: 3
Nickname: arrgh
Registered: Dec, 2007

Re: Scala is not friendly with space, and if-without-else Posted: Dec 16, 2008 10:59 AM
Reply to this message Reply
Note that some would argue, with some justification, thatval foo = if (cond) value (i.e. without an else clause) should throw an exception.

Haixu Huang

Posts: 6
Nickname: regular
Registered: Oct, 2008

Re: Scala is not friendly with space, and if-without-else Posted: Dec 16, 2008 8:49 PM
Reply to this message Reply
> > Also, version 6 of Book omited the discuss of
> difference
> > between
> > if (true | false) value
> > and
> > if (true | false) value1 else value2
> >
> > Does it mean anything? In Scala v 2.7.2, the first one
> > still produce Unit no matter the expression is true or
> > false, will it be changed in next version? Waiting for
> the
> > answer here.
>
> If you use if as an expression that returns a value, the
> inferred type of the expression is the least upper bound
> of the "then" and "else" clauses. Since the else clause
> doesn't return a value, Unit is the LUB. If you use an if
> imperatively (e.g. if (condition) mutateMyState())
> everything will work as you'd expect because you don't
> care about the return value anyway.

Don't think so.
try this following code in interpretor:

val a = if (true) "a" else ()

the shell will reply:

a: Any = a

I think the only thing needed is "else ()", or maybe "else new Exception", but should be implicited.

Seth Tisue

Posts: 2
Nickname: sethtisue
Registered: Feb, 2008

Re: Scala is not friendly with space, and if-without-else Posted: Dec 20, 2008 1:36 PM
Reply to this message Reply
I don't believe this is a mistake or flaw in the language and I hope it won't be "fixed".

Flat View: This topic has 4 replies on 1 page
Topic: Chapter 15 Example Issue Previous Topic   Next Topic Topic: Partially applied function (8.6, page 182)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use