Check if additional parameter is specified in Dart

I'm new to Dart and just learning the basics.

Dart-Homepage shows the following:

It turns out that Dart does have a way to ask if an additional Parameter was supplied when calling the method. Just use the question mark parameter syntax.

Here's an example:

void alignDingleArm(num axis, [num rotations]) {
  if (?rotations) {
    // the parameter was really used
  }
}

      

So, I wrote a simple testing script for learning:

import 'dart:html';

void main() {

  String showLine(String string, {String printBefore : "Line: ", String printAfter}){
    // check, if parameter was set manually:
    if(?printBefore){
      // check, if parameter was set to null
      if(printBefore == null){
        printBefore = "";
      }
    }
    String line = printBefore + string + printAfter;
    output.appendText(line);
    output.appendHtml("<br />\n");
    return line;
  }

  showLine("Hallo Welt!",printBefore: null);

}

      

Dart-Editor already marks the question mark as Error:

Multiple markers at this line
- Unexpected token '?'
- Conditions must have a static type of 
 'bool'

      

When running the script in Dartium JS-Console shows the following error:

Internal error: 'http://localhost:8081/main.dart': error: line 7 pos 8: unexpected token '?'
if(?printBefore){
   ^

      

I know it would be enough to check if printBefore is null, but I want to learn the language.

Does anyone know the cause of this problem? How to check if a parameter is manually set?

+3


source to share


2 answers


This feature existed at some point in the development of Dart, but it was removed again because it caused more complications than elimination, without addressing the problem that really needs to be addressed - forwarding default parameters.

If you have a function foo([x = 42])

and you want the function to jump to it, bar([x]) => f(x);

then since you foo

could tell whether it passed x

or not, you actually wrote bar([x]) => ?x ? foo(x) : foo();

. It was worse than what you had to do without an operator ?_

.

Ideas came about because bar([x]) => foo(?:x)

or something that was set to x if it was present, not if it was missing (I no longer remember the actual syntax suggested), but this quickly got tricky, fx converted the named arguments in positional are bar({x,y}) => foo(?:x, ?:y);

- what if y

provided and x

not. It really was just a bad solution to the self-destructive problem.

So the function has ?x

been dropped. All optional parameters have a default value, which is passed if there is no corresponding argument in the call. If you want to redirect an optional parameter, you need to know the default value for the function you are forwarding.

Most function arguments have a declared default null

, with an internal operator if (arg == null) arg = defaultValue;

to fix it. This means that the value null

can be sent directly without any confusion.



Some arguments are non null

-default. These are mostly logical arguments, but there are other cases as well. I recommend using null

for anything but named boolean parameters (because they are really meant to be called more than they should be optional). At least, unless there is a compelling reason not to do so - for example, to ensure that all subclasses have the same default value for a method parameter (which may or may not be a compelling reason and should be used judiciously).

If you have an optional parameter that can also take null

as a value ... consider if it really should be optional, or if you just need another function with one more argument. Or maybe you can enter a different default "missing argument" value. Example:

abstract class C { foo([D something]); }
class _DMarker implements D { const _DMarker(); }
class _ActualC {
  foo([D something = const _DMarker()]) {
    if (something == const _DMarker()) {
      // No argument passed, because user cannot create a _DMarker.
    } else {
      // Argument passed, may be null.
    }
  }
}

      

This is a big workaround and hardly worth it. In general, just use it null

as the default, it's easier.

+4


source


There was support for checking if the optional parameter was actually a vendor in the early Dart days (before 1.0), but was removed because it causes some problems.



0


source







All Articles