Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to use named parameters in Dart for clarity. How should I handle them?

TL;DR: Named parameters are optional as a result of a conscious design choice. Short of having official language support, is there any way to enforce (and inform) required named arguments?


I find it extremely useful to use named parameters when defining a class. Take, for instance, an Ability in an MMORPG:

class Ability {    final name;   final effectDuration;   final recast;            // wait time until next use   // ... } 

effectDuration and recast both carry the same type of information (i.e. duration of time) and are likely represented by the same datatype. It is easy to mix up which number goes where. However, they are both information vital to the correctness of the object, so they can't be missing during instantiation.

I could just break the program via a try-catch to enforce the requirement of those parameters, but that doesn't sound like fun for someone who uses the class and has no idea (short of reading the docs and understanding intuitively what the class does) that they are required.

Is there any way to enforce the requirement of certain named parameters while managing to inform the caller of said requirement and/or help them use it correctly?

like image 702
Kafeaulait Avatar asked Apr 11 '18 06:04

Kafeaulait


People also ask

How can we use named parameter in Dart function?

Dart Masterclass Programming: iOS/Android Bible Unlike positional parameters, the parameters' name must be specified while the value is being passed. Curly brace {} can be used to specify optional named parameters.

What is named parameter in Dart?

Dart supports named parameters. Named means that when you call a function, you attach the argument to a label. Calling that function would look like this: debugger(message: 'A bug!

How are parameters passed in Dart?

Parameters are a mechanism to pass values to functions. Parameters form a part of the function's signature. The parameter values are passed to the function during its invocation. Unless explicitly specified, the number of values passed to a function must match the number of parameters defined.


1 Answers

The meta package provides a @required annotation that is supported by the DartAnalyzer.

Flutter uses this a lot and provides @required directly from import 'package:flutter/foundation.dart'

foo({@required String name}) {...}  foo(); // results in static warning 

@required doesn't check if the passed value is null or not, only that a value was actually passed on the call site. To check for null you can also use assert() to check for passed values

class Ability {   Ability(this.name, this.effectDuration, this.recast) : assert(name != null), assert(effectDuration != null), assert(recast != null);   final name;   final effectDuration;   final recast;            // wait time until next use   // ... }     
like image 103
Günter Zöchbauer Avatar answered Sep 22 '22 06:09

Günter Zöchbauer