Issue
I want to create a custom type in Dart like I would do in typescript. This type should be a subtype of String, accepting only some values.
For example, in Typescript I would do:
type myType = 'HELLO' | 'WORLD' | '!'
How can I do the same stuff in Dart?
Solution
This isn't possible at the language level in Dart - There are a couple alternatives though.
You could simply define an enum along with a method to derive a string from your enum:
enum MyType {
hello,
world,
exclamationPoint,
}
String myTypeToString(MyType value) {
switch (value) {
case MyType.hello:
return 'HELLO';
case MyType.world:
return 'WORLD';
case MyType.exclamationPoint:
return '!';
}
}
Or you could define a class with three named constructors, and override the toString method:
class MyType {
final String _value;
MyType.hello(): _value = 'HELLO';
MyType.world(): _value = 'WORLD';
MyType.exclamationPoint(): _value = '!';
@override
String toString() {
return _value;
}
}
// Usage:
void main() {
final hello = MyType.hello();
final world = MyType.world();
final punctuation = MyType.exclamationPoint();
// Prints "HELLO, WORLD!"
print("$hello, $world$punctuation");
}
Answered By - Michael Horn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.