GIGO: words unreadable aloud
Mishrogo Weedapeval
 

 

  Sunday 16 May 2004
Chapter 4, Exercises 6-7

Since I gave these guys class (object) names with _Dog, you'll have to run them just a little differently from previous exercises. The main point of this pair of exercises is to show a little bit of how Java overloading works. Unfortunately, Scala appears to behave the same way as Java in this instance, which I think is a small language design mistake. The overloading in c4x6_Dog is just fine by me ...

Chapter 4, Exercise 6

package x;
object c4x6_Dog {
    def bark( a: Int ) = { Console.println( "bark!" ); }
    def bark( a: Float ) = { Console.println( "arf!" ); }
    def bark( a: Double ) = { Console.println( "grr!" ); }
    def bark( a: Char ) = { Console.println( "yip!" ); }
    def bark( a: Boolean ) = { Console.println( "bugger off!" ); }
    def main ( args: Array[String] ) = {
        bark(0);
        bark(0.0f);
        bark(0.0d);
        bark('x');
        bark(false);
    }
}

Chapter 4, Exercise 7

... but the overloading in c4x7_Dog should, IMHO, result in a compile time error. There is no version of bark that takes a Float and an Int. It's easy enough (in this case, a single character!) for the programmer to write down explicitly what is meant, so I'm uneasy that there is an int-to-float conversion hidden in there. As the Zen of Python has it, "Explicit is better than implicit".

package x;
object c4x7_Dog {
    def bark( a: Int, b: Float ) = { Console.println( "bark!" ); }
    def bark( a: Float, b: Float ) = { Console.println( "arf!" ); }
    def bark( a: Double ) = { Console.println( "grr!" ); }
    def bark( a: Char ) = { Console.println( "yip!" ); }
    def bark( a: Boolean ) = { Console.println( "bugger off!" ); }
    def main ( args: Array[String] ) = {
        bark(0, 0.0f);
        bark(0.0f, 0);
        bark(0.0d);
        bark('x');
        bark(false);
    }
}

At least Scala refuses to compile the following version (as does java, with a similar java version), because it would require a float-to-int conversion:

c4x7b.scala:11: overloaded method bark of type (scala.Float,scala.Int)scala.Unit (scala.Int,scala.Int)scala.Unit (scala.Double)scala.Unit (scala.Char)scala.Unit (scala.Boolean)scala.Unit cannot be applied to (scala.Int,scala.Float) bark(0, 0.0f);

package x;
object c4x7b_Dog {
    def bark( a: Float, b: Int ) = { Console.println( "arf!" ); }
    def bark( a: Int, b: Int ) = { Console.println( "bark!" ); }
    def bark( a: Double ) = { Console.println( "grr!" ); }
    def bark( a: Char ) = { Console.println( "yip!" ); }
    def bark( a: Boolean ) = { Console.println( "bugger off!" ); }
    def main ( args: Array[String] ) = {
        bark(0.0f, 0);
        bark(0, 0.0f);
        bark(0.0d);
        bark('x');
        bark(false);
    }
}

9:31:56 PM   comment/     


Click here to visit the Radio UserLand website. Click to see the XML version of this web page. © Copyright 2007 Doug Landauer .
Last update: 07/2/6; 12:39:30 .
Click here to send an email to the editor of this weblog.

May 2004
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Apr   Jun

Previous/Next