oop - Why is method overloading not defined for different return types? -


In Scala, you can overload the method by sharing a common name, but who have different stages Or different parameters are of type. I was wondering why this was not extended even for the return type of a method? Consider the following code:

  class C {def m: int = 42 def m: string = "forty two"} val c = new c val I: int = cm val: string =  

Thanks,

Vincent.

You can definitely overload the methods that return Is different from the type, not just for the methods which only have different return types, for example it is alright:

  def foo (s: string ): String = s + "hello" def foo (i: int): int = i + 1  

On one hand, the answer to your question was clearly a design decision: Return The type of method is part of the signature as Oi is also is AbstractMethodError could experience can tell you.

However, the permission of such overloading can work with sub-typing:

  class A {def foo: int = 1} val a: A = // ..lookup aa val b = a.foo  

This is definitely a valid code and the javac method will resolve the call in a specific way. But if I sub-code A is as follows:

  extends class B {def foo: String = "Hello"}  
< P> For this reason the original code resolution is said to be broken. What should be b ? I have broken some existing code by logically some existing code by sub-type, even if I have not changed that code or that class either.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -