Why dont languages allow overloading of methods by return value?

前端 未结 8 673
[愿得一人]
[愿得一人] 2021-01-02 08:38

c, java and many other languages do not pay attention to return values.

int   i = func()
float f = func()
int   func() { return 5 }
float func() { return 1.3         


        
8条回答
  •  北荒
    北荒 (楼主)
    2021-01-02 09:07

    To avoid ambiguity.

    a( int )
    a( bool )
    int b()
    bool b()
    
    a( b() ) -- Which b?
    

    Here type inference has a cyclic dependency. Which b depends on which a, but which a depends on which b, so it gets stuck.

    Disallowing overloading of return types ensures that type inference is acyclic. The return type can always be determined by the parameter types, but the parameter types cannot be determined by the return type, since they may in turn be determined by the parameter types you are trying to find.

提交回复
热议问题