Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can a method returning Unit be overridden with method returning String when return types are not explicitly given?

I was working through the code examples from the chapter on Traits in Programming in Scala Edition1 https://www.artima.com/pins1ed/traits.html

and came across a weird behavior because of my typo. While overriding method of a trait below code snippet doesn't give any compile error although the return types of the overridden method is different Unit vs String. But upon calling the method on an object it returns Unit but doesn't print anything.

trait Philosophical {
    def philosophize = println("I consume memory, therefore I am!")
}

class Frog extends Philosophical {
  override def toString = "green"
  override def philosophize = "It aint easy to be " + toString + "!"
}

val frog = new Frog
//frog: Frog = green

frog.philosophize
// no message printed on console

val f = frog.philosophize
//f: Unit = ()

But when I give the explicit return type in the overridden method , it gives a compile error:

class Frog extends Philosophical {
  override def toString = "green"
  override def philosophize: String = "It aint easy to be " + toString + "!"
}
         override def philosophize: String = "It aint easy to be " + toString +
                      ^
On line 3: error: incompatible type in overriding
       def philosophize: Unit (defined in trait Philosophical);
        found   : => String
        required: => Unit

Can anyone help explain why no compile error in the first case.

like image 214
Shanil Avatar asked Dec 02 '19 13:12

Shanil


People also ask

Is it possible to override a return type of a method?

This means an overridden method may have a more specific return type. That is, as long as the new return type is assignable to the return type of the method you are overriding, it's allowed.

Can a method in a subclass return an object of another class?

In another words, a method in a subclass can return an object whose type is a subclass of the type returned by the method with the same signature in the superclass. Show activity on this post.

What are the limitations of overridden methods?

But the limitations are that the overridden method must have a return type that is more specific type of the return type of the actual method. All the answers have given examples of the overridden method to have a return type which is a subclass of the return type of the actual method.

Why can’t a method be overridden in a derived class?

Because a method is overridden in the derived class from the base class. A non-virtual or a static method can’t be overridden. Both the override method and the virtual method must have the same access level modifier. Writing code in comment? Please use ide.geeksforgeeks.org , generate link and share the link here.


2 Answers

When the expected type is Unit, any value can be accepted:

Value Discarding

If e has some value type and the expected type is Unit, e is converted to the expected type by embedding it in the term { e; () }.

like image 195
Alexey Romanov Avatar answered Nov 15 '22 22:11

Alexey Romanov


my question is why it got through the compiler in the 1st case

When you did not specify the return type explicitly it was inferred by the type it needs to have for the override to work.

That turned out to be Unit.

Since String values (the value of the expression making up the function body) can be assigned to Unit, the compiler is happy.

like image 32
Thilo Avatar answered Nov 15 '22 23:11

Thilo