Function overloading by return type?

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

    In .NET, sometimes we use one parameter to indicate the desired output from a generic result, and then made a conversion to get what we expect.

    C#

    public enum FooReturnType{
            IntType,
            StringType,
            WeaType
        }
    
        class Wea { 
            public override string ToString()
            {
                return "Wea class";
            }
        }
    
        public static object Foo(FooReturnType type){
            object result = null;
            if (type == FooReturnType.IntType) 
            {
                /*Int related actions*/
                result = 1;
            }
            else if (type == FooReturnType.StringType)
            {
                /*String related actions*/
                result = "Some important text";
            }
            else if (type == FooReturnType.WeaType)
            {
                /*Wea related actions*/
                result = new Wea();
            }
            return result;
        }
    
        static void Main(string[] args)
        {
            Console.WriteLine("Expecting Int from Foo: " + Foo(FooReturnType.IntType));
            Console.WriteLine("Expecting String from Foo: " + Foo(FooReturnType.StringType));
            Console.WriteLine("Expecting Wea from Foo: " + Foo(FooReturnType.WeaType));
            Console.Read();
        }
    

    Maybe this example could help too:

    C++

        #include 
    
    enum class FooReturnType{ //Only C++11
        IntType,
        StringType,
        WeaType
    }_FooReturnType;
    
    class Wea{
    public:
        const char* ToString(){
            return "Wea class";
        }
    };
    
    void* Foo(FooReturnType type){
        void* result = 0;
        if (type == FooReturnType::IntType) //Only C++11
        {
            /*Int related actions*/
            result = (void*)1;
        }
        else if (type == FooReturnType::StringType) //Only C++11
        {
            /*String related actions*/
            result = (void*)"Some important text";
        }
        else if (type == FooReturnType::WeaType) //Only C++11
        {
            /*Wea related actions*/
            result = (void*)new Wea();
        }
        return result;
    }
    
    int main(int argc, char* argv[])
    {
        int intReturn = (int)Foo(FooReturnType::IntType);
        const char* stringReturn = (const char*)Foo(FooReturnType::StringType);
        Wea *someWea = static_cast(Foo(FooReturnType::WeaType));
        std::cout << "Expecting Int from Foo: " << intReturn << std::endl;
        std::cout << "Expecting String from Foo: " << stringReturn << std::endl;
        std::cout << "Expecting Wea from Foo: " << someWea->ToString() << std::endl;
        delete someWea; // Don't leak oil!
        return 0;
    }
    

提交回复
热议问题