blue-sky
blue-sky

Reputation: 53916

Explain a scala class and objects?

Watching lecture 4.2 on Martin Odersky's scala course I fail to understand the below class, or its use :

abstract class Boolean {

  def ifThenElse[T](t: => T, e: => T): T

  def && (x: => Boolean): Boolean = ifThenElse(x, False)
  def || (x: => Boolean): Boolean = ifThenElse(True, x)
  def unary_! : Boolean = ifThenElse(False , True)

  def == (x: Boolean): Boolean = ifThenElse(x, x.unary_!)
  def != (x: Boolean): Boolean = ifThenElse(x.unary_! , x)

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

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

}

Can sample implentations / explanation be provide so I can better provide what is occuring and/or its use ?

Upvotes: 4

Views: 531

Answers (2)

pagoda_5b
pagoda_5b

Reputation: 7383

All the needed implementations are included in your example.

Boolean is an abstract superclass with 2 concrete implementations: True and False

The superclass defines common operations on booleans, delegating the differing behavior to the only specific operation that is different between the subclasses: the method ifThenElse

The subclasses are defined as objects, so there's only a single instance of them.

To understand how they work, let's make some example

/* we start with the basic values and call an if/else control
 * implementation that prints "yes"/"no" depending on the target
 */
scala> True.ifThenElse(println("yes"), println("no"))
yes

scala> False.ifThenElse(println("yes"), println("no"))
no

/* we can negate */
scala> (! True).ifThenElse(println("yes"), println("no"))
no

scala> (! False).ifThenElse(println("yes"), println("no"))
yes

/* and do some algebra */
scala> (True && False).ifThenElse(println("yes"), println("no"))
no

scala> (True || False).ifThenElse(println("yes"), println("no"))
yes

/* or some equality tests */
scala> (True && True == True).ifThenElse(println("yes"), println("no"))
yes

scala> (False || True != True).ifThenElse(println("yes"), println("no"))
no

This is just an educational approach to using call-by-name to implement boolean operations. As you can see each call just prints one value, displaying the fact that the arguments are evaluated "on demand" and not at call site.

Of course the notation is rather cumbersome to use, but it's meant to be illustrative and not practical, the goal's not that.

Upvotes: 5

Jesper
Jesper

Reputation: 207006

What exactly do you not understand about the class?

It defines an abstract class Boolean, which has one abstract method, ifThenElse. Two objects, False and True, extends class Boolean with two implementations of the method ifThenElse.

You probably made a copy and paste error, because the two implementations are the same; the version in True seems wrong, it should look like this:

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

The class Boolean contains a number of methods: &&, ||, unary_!, == and != which are all defined in terms of ifThenElse.

The method ifThenElse takes two by-name parameters, which you can see because their type in the parameter list is => T. That means that if you use the name in the implementation of the method, it will be evaluated at that point.

Upvotes: 5

Related Questions