The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Problems with Element examples

4 replies on 1 page. Most recent reply: Oct 29, 2008 9:50 AM by Bruce Gunderson

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
Bruce Gunderson

Posts: 4
Nickname: 62808
Registered: Oct, 2008

Problems with Element examples Posted: Oct 28, 2008 10:13 AM
Reply to this message Reply
Advertisement
I have PrePrint Edition Version 4 and am trying the examples with Scala 2.7.2.RC3 and am having lots of problems with the Element subclasses in Chapter 10.

1. LineElement does not compile:
E:\Data\Scala\ProgInScala\Ch10>fsc *.scala
E:\Data\Scala\ProgInScala\Ch10\LineElement.scala:2: error: error overriding value width in class Element of type Int;
method width needs to be an immutable value
override def width = s.length
^
E:\Data\Scala\ProgInScala\Ch10\LineElement.scala:3: error: error overriding value height in class Element of type Int;
method height needs to be an immutable value
override def height = 1
^
two errors found

To make progress, I changed the "def"s to "val"s and the compiler was happy.

2. Instantiating a UniformElement throws a NullPointer Exception:
scala> val e3: Element = new UniformElement('x', 2, 3)
java.lang.NullPointerException
at Element.<init>(Element.scala:5)
at UniformElement.<init>(UniformElement.scala:5)
at .<init>(<console>:4)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:3)
at RequestResult$.<clinit>(<console>)
at RequestResult$result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.i...

Investigating, I found that
"private val line = ch.toString * width"
gives line == null which leads to contents == null.
Changing the "val" to "def" makes it work.

I find two problems related to val and def somehow suspicious. I have the same problems with Scala.2.7.1.final and with the Scala plug-in for eclipse.

I use Windows XP Profesional SP3 with all updates and Java 1.6.0_07.


Bruce Gunderson

Posts: 4
Nickname: 62808
Registered: Oct, 2008

Re: Problems with Element examples Posted: Oct 29, 2008 4:03 AM
Reply to this message Reply
I am sorry. When trying this, I accidentally substituted the hypothetical definition of Element at the bottom of page 213 for the real definition in Listing 10.2 at the top of the page. This the cause of problem 1.

Bruce Gunderson

Posts: 4
Nickname: 62808
Registered: Oct, 2008

Re: Problems with Element examples Posted: Oct 29, 2008 4:12 AM
Reply to this message Reply
Concerning problem 2, the decompiled code of UniformElement

public UniformElement(char, int, int);
Code:
0: aload_0
1: iload_2
2: putfield #15; //Field width:I
5: aload_0
6: iload_3
7: putfield #17; //Field height:I
10: aload_0
11: invokespecial #22; //Method Element."<init>":()V
14: aload_0
15: getstatic #28; //Field scala/Predef$.MODULE$:Lscala/Predef$;
18: iload_1
19: invokestatic #34; //Method scala/runtime/BoxesRunTime.boxToCharacter:(C)Ljava/lang/Character;
22: invokevirtual #40; //Method java/lang/Object.toString:()Ljava/lang/String;
25: invokevirtual #44; //Method scala/Predef$.stringWrapper:(Ljava/lang/String;)Lscala/runtime/RichString;
28: iload_2
29: invokevirtual #50; //Method scala/runtime/RichString.$times:(I)Ljava/lang/String;
32: putfield #52; //Field line:Ljava/lang/String;
35: return

shows that Element.<init> is invoked at 11 before line is initialized at 14-32.

I do not understand how this can ever work without a change to the code.

George Berger

Posts: 24
Nickname: gcb
Registered: Jul, 2007

Re: Problems with Element examples Posted: Oct 29, 2008 5:59 AM
Reply to this message Reply
I tried pasting listings 10.13 and 10.12 into a file and found it compiled okay with 2.7.2.RC3. Have you tried that?

Bruce Gunderson

Posts: 4
Nickname: 62808
Registered: Oct, 2008

Re: Problems with Element examples Posted: Oct 29, 2008 9:50 AM
Reply to this message Reply
The solution to problem 2 is the same as to problem 1.
The definition of Element with def's for width and height works because Element.<init> does not reference contents.
The definition of Element with val's for width and height has a problem with the subclass UniformElement because Element.<init> need contents which references UniformElement.line, which is not initialized until after the call to Element.<init>.

It was all a stupid mistake. But, I learned a lot.

Flat View: This topic has 4 replies on 1 page
Topic: Coding conventions : Listing 8.1 Previous Topic   Next Topic Topic: Noob: Using -> Operator with Map factory method

Sponsored Links



Google
  Web Artima.com   

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