Yissachar Radcliffe
2015-10-09 06:05:21 UTC
Kotlin has a nice when expression that can be used for control flow:
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
Consider the following if-else:
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
This can be more cleanly expressed as:
when (obj) {
is String -> print("string");
is List -> print("list");
else -> print("object");
}
I find when to be much easier to read then if-else chains, especially for
one-liners.
This can be implemented in Dart as either an expression or a statement,
though making it an expression would touch on the same issues with making
if statements expressions (e.g. implicit returns).
Thoughts on whether this would be a nice addition to Dart?
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
Consider the following if-else:
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
This can be more cleanly expressed as:
when (obj) {
is String -> print("string");
is List -> print("list");
else -> print("object");
}
I find when to be much easier to read then if-else chains, especially for
one-liners.
This can be implemented in Dart as either an expression or a statement,
though making it an expression would touch on the same issues with making
if statements expressions (e.g. implicit returns).
Thoughts on whether this would be a nice addition to Dart?
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.