Created
May 21, 2019 08:59
-
-
Save minikin/9ed0ec3f00f9691fab14563f1d864515 to your computer and use it in GitHub Desktop.
Switch on a Set using List literals as cases in Dart.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Switch on a Set using List literals as cases in Dart. | |
// Can be really useful to avoid many if/else statements. | |
enum Direction { up, down, left, right } | |
class MapTile { | |
var directions = Set<Direction>(); | |
String render() { | |
switch (directions.toList()) { | |
case [Direction.up, Direction.down]: | |
return 'map_tile_vertical.png'; | |
case [Direction.left, Direction.right]: | |
return 'map_tile_horizontal.png'; | |
default: | |
return 'default.png'; | |
} | |
} | |
} | |
void main() { | |
final mapTile = MapTile(); | |
mapTile.directions = const {Direction.up}; | |
print(mapTile.render()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://dartpad.dartlang.org/9ed0ec3f00f9691fab14563f1d864515