syntax - Checking, if optional parameter is provided in Dart -
i'm new dart , learning basics.
the dart-homepage shows following:
it turns out dart indeed have way ask if optional parameter provided when method called. use question mark parameter syntax.
here example:
void aligndinglearm(num axis, [num rotations]) { if (?rotations) { // parameter used } }
so i've wrote simple testing script learning:
import 'dart:html'; void main() { string showline(string string, {string printbefore : "line: ", string printafter}){ // check, if parameter set manually: if(?printbefore){ // check, if parameter set null if(printbefore == null){ printbefore = ""; } } string line = printbefore + string + printafter; output.appendtext(line); output.appendhtml("<br />\n"); return line; } showline("hallo welt!",printbefore: null); }
the dart-editor marks questionmark error:
multiple markers @ line - unexpected token '?' - conditions must have static type of 'bool'
when running script in dartium, js-console shows folloing error:
internal error: 'http://localhost:8081/main.dart': error: line 7 pos 8: unexpected token '?' if(?printbefore){ ^
i know, enough check if printbefore null, want learn language.
does know reason problem? how check, if parameter set manually?
feature existed @ point in dart's development, removed again because caused more complication removed, without solving problem needed solving - forwarding of default parameters.
if have function foo([x = 42])
, want function forward it, bar([x]) => f(x);
, then, since foo
tell if x
passed or not, ended writing bar([x]) => ?x ? foo(x) : foo();
. worse had without ?_
operator.
ideas came having bar([x]) => foo(?:x)
or pased on x if present , not if absent (i no longer remember actual proposed syntax), got complicated fast, fx converting named arguments positional - bar({x,y}) => foo(?:x, ?:y);
- if y
provided , x
not. bad solution self-inflicted problem.
so, ?x
feature rolled back. optional parameters have default value passed if there no matching argument in call. if want forward optional parameter, need know default value of function forwarding to.
for function arguments, declared default value null
, internal if (arg == null) arg = defaultvalue;
statement fix it. means null
value can forwarded directly without confusion.
some arguments have non-null
default value. it's boolean arguments, there other cases too. recommend using null
except named boolean parameters (because meant named more meant optional). @ least unless there reason not - ensuring subclasses have same default value method parameter (which may reason, or not, , should used judiciosuly).
if have optional parameter can accept null
value ... consider whether should optional, or if need different function 1 more argument. or maybe can introduce different "missing argument" default value. example:
abstract class c { foo([d something]); } class _dmarker implements d { const _dmarker(); } class _actualc { foo([d = const _dmarker()]) { if (something == const _dmarker()) { // no argument passed, because user cannot create _dmarker. } else { // argument passed, may null. } } }
this big workaround, , hardly ever worth it. in general, use null
default value, it's simpler.
Comments
Post a Comment