The Artima Developer Community
Sponsored Link

Programming in Scala Forum
page 241, compile error (pre-print version of the 2nd edition)

2 replies on 1 page. Most recent reply: Dec 8, 2010 7:16 AM by Jim Balter

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 2 replies on 1 page
Peter B

Posts: 2
Nickname: 74230
Registered: Dec, 2010

page 241, compile error (pre-print version of the 2nd edition) Posted: Dec 8, 2010 6:35 AM
Reply to this message Reply
Advertisement
Hi,

I'm trying out code in section 10.12, namely adding a new method called "above" to the class Element. Running this in REPL, it gives me the following compile error:

<console>:14: error: type mismatch;
found : ArrayElement
required: Element
new ArrayElement(this.contents ++ that.contents)
^


However, if I move this method into class "ArrayElement" instead, it works fine. I suspect it has something to do with class Element not being aware of existence of ArrayElement. Here is the source code:

abstract class Element {
def contents: Array[String]
def height: Int = contents.length
def width: Int = if (height == 0) 0 else contents(0).length

def above(that: Element): Element =
new ArrayElement(this.contents ++ that.contents) // ERROR HERE
}

class ArrayElement(
val contents: Array[String]
) extends Element


I know that later in the text the code gets refactored and "above" is moved into a companion object, but the fact that this intermediate version doesn't compile is a bit misleading to the reader.

What is the right version of the source code?

Besides this little glitch, I'm highly enjoying the book so far.

Thanks.

- Peter


Peter B

Posts: 2
Nickname: 74230
Registered: Dec, 2010

Re: page 241, compile error (pre-print version of the 2nd edition) Posted: Dec 8, 2010 7:11 AM
Reply to this message Reply
Actually, I just tried compiling it with scalac, and that worked.

So, this code DOES compile with scalac, but DOES NOT compile in REPL.

Jim Balter

Posts: 13
Nickname: jibal
Registered: Dec, 2010

Re: page 241, compile error (pre-print version of the 2nd edition) Posted: Dec 8, 2010 7:16 AM
Reply to this message Reply
The compiler compiles whole files, whereas the REPL compiles top level expressions as it sees them. When it sees new ArrayElement(...), it hasn't yet seen its definition so it doesn't know that it's a subclass of Element. If you're going to hand code fragments to the REPL, you have to get the order right, or you have to include them inside a single top level construct. e.g.,

scala> scala> class Element
defined class Element

scala> val a: Element = new ArrayElement
<console>:6: error: not found: type ArrayElement
val a: Element = new ArrayElement
^

scala> class ArrayElement extends Element
defined class ArrayElement


but


scala> object Foo {
| class Element
| val a: Element = new ArrayElement
| class ArrayElement extends Element
| }
defined module Foo

Flat View: This topic has 2 replies on 1 page
Topic: Why parameterize Array? Previous Topic   Next Topic Topic: decimals

Sponsored Links



Google
  Web Artima.com   

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