Scala Booleans: code snippet

I use scala code taken from scala cursors on coursera:

package src.functional.week4 abstract class Boolean { def ifThenElse[T](t: => T, e: => T): T def && (x: => Boolean): Boolean = ifThenElse(x, false) } 

The line def && (x: => Boolean): Boolean = ifThenElse(x, false) gives this compile-time error:

type mismatch; found: scala.Boolean (false) required: src.functional.week4.Boolean

Here is a snippet of code from the video:

enter image description here

Do I need to change the code to compile it?

When I create a new object "false" using

  object false extends Boolean { def ifThenElse[T](t: => T, e: => t) = e } 

I get an error message:

Multiple tokens on this line - ID is expected, but "false" is found.

I define an object in the same class as the "abstract Boolean class". I cannot create a new object of type false because the Eclipse environment does not allow this.

+4
source share
2 answers

Your code (and Martin's) defines the new Boolean , although it is predefined / built into Scala.

The problem you are facing is that you did not define a new false to reinstall the built-in false , and the built-in false incompatible with your overridden Boolean .

+5
source

The code in the lecture does not compile because true and false are reserved words and cannot be overridden. Use true and false instead.

+4
source

All Articles