Return multiple values from function

前端 未结 4 416
抹茶落季
抹茶落季 2021-01-01 08:28

Is there a way to return several values in a function return statement (other than returning an object) like we can do in Go (or some other languages)?

For example,

相关标签:
4条回答
  • 2021-01-01 08:57

    I'd like to add that one of the main use-cases for multiple return values in Go is error handling which Dart handle's in its own way with Exceptions and failed promises.

    Of course this leaves a few other use-cases, so let's see how code looks when using explicit tuples:

    import 'package:tuple/tuple.dart';
    
    Tuple2<int, String> demo() {
      return new Tuple2(42, "life is good");
    }
    
    void main() {
      final result = demo();
      if (result.item1 > 20) {
        print(result.item2);
      }
    }
    

    Not quite as concise, but it's clean and expressive code. What I like most about it is that it doesn't need to change much once your quick experimental project really takes off and you start adding features and need to add more structure to keep on top of things.

    class FormatResult {
      bool changed;
      String result;
      FormatResult(this.changed, this.result);
    }
    
    FormatResult powerFormatter(String text) {
      bool changed = false;
      String result = text;
      // secret implementation magic
      // ...
      return new FormatResult(changed, result);
    }
    
    void main() {
      String draftCode = "print('Hello World.');";
      final reformatted = powerFormatter(draftCode);
      if (reformatted.changed) {
        // some expensive operation involving servers in the cloud.
      }
    }
    

    So, yes, it's not much of an improvement over Java, but it works, it is clear, and reasonably efficient for building UIs. And I really like how I can quickly hack things together (sometimes starting on DartPad in a break at work) and then add structure later when I know that the project will live on and grow.

    0 讨论(0)
  • 2021-01-01 09:11

    In this type of situation in Dart, an easy solution could return a list then accessing the returned list as per your requirement. You can access the specific value by the index or the whole list by a simple for loop.

    List func() {
      return [false, 30, "Ashraful"];
    }
    
    void main() {
      final list = func();
      
      // to access specific list item
      var item = list[2];
      
      // to check runtime type
      print(item.runtimeType);
      
      // to access the whole list
      for(int i=0; i<list.length; i++) {
        print(list[i]);
      }
    }
    
    0 讨论(0)
  • 2021-01-01 09:21

    Create a class:

    import 'dart:core';
    
    class Tuple<T1, T2> {
      final T1 item1;
      final T2 item2;
    
      Tuple({
        this.item1,
        this.item2,
      });
    
      factory Tuple.fromJson(Map<String, dynamic> json) {
        return Tuple(
          item1: json['item1'],
          item2: json['item2'],
        );
      }
    }
    

    Call it however you want!

    Tuple<double, double>(i1, i2);
    or
    Tuple<double, double>.fromJson(jsonData);
    
    0 讨论(0)
  • 2021-01-01 09:22

    Dart doesn't support multiple return values.

    You can return an array,

    List foo() {
      return [42, "foobar"];
    }
    

    or if you want the values be typed use a Tuple class like the package https://pub.dartlang.org/packages/tuple provides.

    See also either for a way to return a value or an error.

    0 讨论(0)
提交回复
热议问题