Function overloading by return type?

前端 未结 14 2268
终归单人心
终归单人心 2020-11-22 03:55

Why don\'t more mainstream statically typed languages support function/method overloading by return type? I can\'t think of any that do. It seems no less useful or reasona

14条回答
  •  遥遥无期
    2020-11-22 04:20

    If functions were overloaded by the return type and you had these two overloads

    int func();
    string func();
    

    there is no way the compiler could figure out which of those two functions to call upon seeing a call like this

    void main() 
    {
        func();
    }
    

    For this reason, language designers often disallow return-value overloading.

    Some languages (such as MSIL), however, do allow overloading by return type. They too face the above difficulty of course, but they have workarounds, for which you'll have to consult their documentation.

提交回复
热议问题