The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Compilation errors

1 reply on 1 page. Most recent reply: Sep 3, 2008 10:24 PM by Bill Venners

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 1 reply on 1 page
Leif Jantzen

Posts: 2
Nickname: ljantzen
Registered: Aug, 2005

Compilation errors Posted: Aug 27, 2008 1:23 PM
Reply to this message Reply
Advertisement
Hi,

I am reading through the new (version 4) edition of the PrePrint book. I am having some trouble with a few of the code examples:

On page 161 the try expression example fails compilation ( 2.7.2.RC1 ) when I paste the code into the interpreter:


scala> val n = 3
n: Int = 3

scala> val half =
| if (n % 2 == 0)
| n / 2
half: Unit = ()

scala> else
<console>:1: error: illegal start of definition
else
^

scala> throw new RuntimeException("n must be even")


Additionally, the tail recursion example on page 191 fails to compile:


scala> def boom(x: Int): Int =
| if (x == 0) throw new Exception("boom!")
<console>:5: error: type mismatch;
found : Unit
required: Int
if (x == 0) throw new Exception("boom!")
^

scala> else boom(x - 1) + 1
<console>:1: error: illegal start of definition
else boom(x - 1) + 1
^


Am I missing something here?


Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Compilation errors Posted: Sep 3, 2008 10:24 PM
Reply to this message Reply
Hi Leif,

I think this is caused by the interpreter guessing wrongly where the end of your phrase is. If you put these things as is in a file and compile them, they will work. Or if you reformat them so the else is on the same line as the previous line, it should work in the interpreter too.

Bill

Flat View: This topic has 1 reply on 1 page
Topic: Spec trait of ScalaTest Previous Topic   Next Topic Topic: Error in a listing in Chapter 15..

Sponsored Links



Google
  Web Artima.com   

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