Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code equivalent of out or reference parameters in Dart

Tags:

dart

In Dart, how would I best code the equivalent of an (immutable/value/non-object) out or reference parameter?

For example in C#-ish I might code:

function void example()
{
  int result = 0;
  if (tryFindResult(anObject, ref result))
    processResult(result);
  else
    processForNoResult();
}

function bool tryFindResult(Object obj, ref int result)
{
  if (obj.Contains("what I'm looking for"))
  {
    result = aValue;
    return true;
  }
  return false;  
}
like image 595
Will Rubin Avatar asked Aug 22 '12 19:08

Will Rubin


1 Answers

This is not possible in Dart. Support for struct value types, ref or val keywords were discussed on the Dart mailing list just like week. Here is a link to the discussion where you should let your desire be known:

https://groups.google.com/a/dartlang.org/d/topic/misc/iP5TiJMW1F8/discussion

The Dart-way would be:

void example() {
  List result = tryFindResult(anObject);
  if (result[0]) {
    processResult(result[1]);
  } else {
    processForNoResult();
  }
}

List tryFindResult(Object obj) {
  if (obj.contains("What I'm looking for")) {
    return [true, aValue];
  }
  return [false, null];
}
like image 190
Cutch Avatar answered Oct 03 '22 11:10

Cutch