6

Say I have:

Function(int x, int y) func = (int x, int y) {
  return (1, 2); // error
};

How to actually return (1, 2) from above function?

3
  • What is return (1, 2);? (1, 2) looks like parameters for some method but you are not calling any method. Also, what is Function(int x, int y) result = func(1, 2);? It looks like you don't want to return only a function but also its parameters? Commented May 29, 2020 at 11:10
  • A variable can point to a function which you then later can call with arguments to run the function. But you cannot point to a function with arguments for to execute it later somewhere else. Also, you cannot make unnamed recursive functions (e.g. function calling itself without a name of the function it should call). Commented May 29, 2020 at 11:19
  • This question is similar to: Return multiple values from function. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented May 23 at 10:31

2 Answers 2

8

Methods in Dart can only return one value. So if you need to return multiple values you need to pack them inside another object which could e.g. be your own defined class, a list, a map or something else.

In your case with x and y you could consider using the Point class from dart:math:

import 'dart:math';

Point<int> func(int x, int y) => Point(x, y);

Support for returning multiple values in Dart are a ongoing discussion here: https://github.com/dart-lang/language/issues/68

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry about that. I think you need to add some more clarification in your question with a small code example of what your want to achieve. Right now I am confused about what you goal is even after your edit.
Ok, so you want to have a method which returns multiple arguments which can be used by other method. That is no possible in Dart. You should use one of the solutions from: stackoverflow.com/a/62083743/1953515
From my point of view you should just delete the question since everything is a mess here and it already very much the same as your previous question: stackoverflow.com/q/62083562/12483095
Well, just delete it. It is just a few points and I don't care about the points. :)
I edited the post to reflect your edited version to give this question some life. I also request you to delete all the comments to clear everything. Boooom!!!
4

Now you can return multiple values from function in Dart 3.0 with Record feature.

 (int, int) testFunc(int x, int y) {
     return (1, 2);
   }

Destructures using a record pattern

 var (x, y) = testFunc(2,3);
 print(x); // Prints 1
 print(y); // Prints 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.