
Yan Cui
I help clients go faster for less using serverless technologies.
Whilst Dart doesn’t have support for enum types but it turns out you can do a pretty good job of emulating it using Dart’s constant constructors (which allows you to create compile-time constants with your class, which in .Net you’re restricted to numbers, booleans, strings and null).
Using the technique from this SO answer you will start with a base class for your enum types:
abstract class Enum<T> { | |
final T value; | |
const Enum(this.value); | |
} |
From here, you can create other specialized enum types, such as:
import 'enum.dart'; | |
class Meter<int> extends Enum<int> { | |
const Meter(int val) : super(val); | |
static const Meter HIGH = const Meter(100); | |
static const Meter MIDDLE = const Meter(50); | |
static const Meter LOW = const Meter(10); | |
} |
Thank you, mr Sergy Akopkokhyants for this elegant little solution.
Whenever you’re ready, here are 3 ways I can help you:
- Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
- I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
- Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.
Pingback: Introduce raven_dart, a Dart client for Sentry | theburningmonk.com
Pingback: Year in Review, 2014 | theburningmonk.com