Why don't functions have parameterized return types, since it has parameterized input?

This always forces us to return one parameter in case I need to return several, for example List and String. This restriction does not exist in function arguments.

+3
source share
6 answers

This problem is what functional languages, such as F #, haskell, etc., are trying to solve. The problem is that in hardware, the return value of the function was originally returned through the CPU register, so you can only return one value. The C syntax passed by this legacy and C ++ (and C #, Java) also inherited this.

+8

, , . , Python, . 2 : 1 int.

>>> def myFunc():
...   string_val = "techmaddy"
...   int_val = 10
...   return string_val, int_val
...
>>>
>>> s, i = myFunc()
>>>
>>> print s
techmaddy
>>>
>>> print i
10

, .

PS: , , Python . Python, , .

+4

, , .

, , .

+4

, Object []

return new Object[] { list, string};

, Pair < X, Y > < Z, Y, Z > .

+3

Javascript, fortunately for you, is a dynamic language. This means that you can build any desired object and return it. This effectively matches your requirement to have a "parameterized" return value, albeit in a rather non-standard way.

For example:

function stuff() {
    return {
        "foo": "a",
        "bar": "b"
    }
}

var theStuff = stuff();
alert(theStuff.foo + theStuff.bar); // should output "ab"
+1
source

In the method of handling complex inverse behavior, you must pass the interface that invokes the method. eg.

public interface Results<R> {
    public void processing(String stage);
    public void success(String mesg, R result);
    public void successes(String mesg, List<R> result);
    public void thrown(Throwable t);
}

public void process(Results<R> results, String text, List<String> data);
0
source

All Articles