Elegant ways to return multiple values from a function

后端 未结 14 859
梦谈多话
梦谈多话 2020-12-15 03:49

It seems like in most mainstream programming languages, returning multiple values from a function is an extremely awkward thing.

The typical soluti

相关标签:
14条回答
  • 2020-12-15 04:42

    Both Lua and CLU (by Barbara Liskov's group at MIT) have multiple return values for all functions---it is always the default. I believe the designers of Lua were inspired by CLU. I like having multiple values be the default for calls and returns; I think it is a very elegant way of thinking about things. In Lua and CLU this mechanism is completely independent of tuples and/or records.

    The portable assembly language C-- supports multiple return values for its native calling convention but not for the C calling convention. There the idea is to enable a compiler writer to return multiple values in individual hardware registers rather than having to put the values in a clump in memory and return a pointer to that clump (or as in C, have the caller pass a pointer to an empty clump).

    Like any mechanism, multiple return values can be abused, but I don't think it is any more unreasonable to have a function return multiple values than it is to have a struct contain multiple values.

    0 讨论(0)
  • 2020-12-15 04:43

    Here was my Ruby idea to return a special value that looks and acts like a scalar but actually has hidden attributes on it:

    https://gist.github.com/2305169

    0 讨论(0)
  • 2020-12-15 04:44

    c#

    public struct tStruct
    {
        int x;
        int y;
        string text;
        public tStruct(int nx, int ny, string stext)
        {
             this.x = nx;
             this.y = ny;
             this.text = stext;
        }
    }
    
    public tStruct YourFunction()
    {
        return new tStruct(50, 100, "hello world");
    }
    
    public void YourPrintFunction(string sMessage)
    {
        Console.WriteLine(sMessage);
    }
    

    in Lua you call

    MyVar = YourFunction();
    YourPrintfFunction(MyVar.x);
    YourPrintfFunction(MyVar.y);
    YourPrintfFunction(MyVar.text);
    

    Output: 50 100 hello world

    Paul

    0 讨论(0)
  • 2020-12-15 04:46

    Even if you ignore the wonderful newer destructuring assignment Moss Collum mentioned, JavaScript is pretty nice on returning multiple results.

    function give7and5() {
      return {x:7,y:5};
    }
    
    a=give7and5();
    console.log(a.x,a.y);
    
    7  5
    
    0 讨论(0)
  • 2020-12-15 04:51

    If a function returns multiple values, that is a sign you might be witnessing the "Data Clump" code smell. Often data clumps are primitive values that nobody thinks to turn into an object, but interesting stuff happens as you begin to look for behavior to move into the new objects.

    Writing tiny helper classes might be extra typing, but it provides clear names and strong type checking. Developers maintaining your code will appreciate it. And useful small classes often grow up to be used in other code.

    Also, if a function returns multiple values, then it might be doing too much work. Could the function be refactored into two (or more) small functions?

    0 讨论(0)
  • 2020-12-15 04:51

    Often in php I'll use a referenced input:

    public function Validates(&$errors=array()) {
       if($failstest) {
          $errors[] = "It failed";
          return false;
       } else {
          return true;
       }
    }
    

    That gives me the error messages I want without polluting the bool return, so I can still do:

    if($this->Validates()) ...
    
    0 讨论(0)
提交回复
热议问题