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

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; () }.


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.