Function overloading by return type?

前端 未结 14 2398
终归单人心
终归单人心 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:38

    if you want to overload methods with different return types, just add a dummy parameter with default value to allow the overload execution, but don't forget the parameter type should be different so the overload logic works next is an e.g on delphi:

    type    
        myclass = class
        public
          function Funct1(dummy: string = EmptyStr): String; overload;
          function Funct1(dummy: Integer = -1): Integer; overload;
        end;
    

    use it like this

    procedure tester;
    var yourobject : myclass;
      iValue: integer;
      sValue: string;
    begin
      yourobject:= myclass.create;
      iValue:= yourobject.Funct1(); //this will call the func with integer result
      sValue:= yourobject.Funct1(); //this will call the func with string result
    end;
    

提交回复
热议问题