Discussion:
[dart-misc] When Expression/Statement
Yissachar Radcliffe
2015-10-09 06:05:21 UTC
Permalink
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?
--
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.
Jan Mostert
2015-10-09 06:20:36 UTC
Permalink
What would be the purpose of "when" other than replacing if-else-chains ?
Post by Yissachar Radcliffe
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
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
--
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.
Günter Zöchbauer
2015-10-09 07:28:56 UTC
Permalink
It would also be a more powerful/less restricted switch.
Post by Jan Mostert
What would be the purpose of "when" other than replacing if-else-chains ?
Post by Yissachar Radcliffe
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
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
--
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.
Jan Mostert
2015-10-09 07:33:29 UTC
Permalink
Wouldn't adapting a switch statement be cleaner than introducing a when
keyword?

switch(obj, nofallthrough:true){
case is String => print("string");
case is List => print("list");
default => print("object");
}
Post by Günter Zöchbauer
It would also be a more powerful/less restricted switch.
Post by Jan Mostert
What would be the purpose of "when" other than replacing if-else-chains ?
Post by Yissachar Radcliffe
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
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
--
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
--
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.
Günter Zöchbauer
2015-10-09 08:41:48 UTC
Permalink
It's not only about fallthrough, switch only allows to compare integer,
string, or compile-time constants that don't have custom == implementation.
See also http://japhr.blogspot.co.at/2012/03/dart-switch.html
Post by Jan Mostert
Wouldn't adapting a switch statement be cleaner than introducing a when
keyword?
switch(obj, nofallthrough:true){
case is String => print("string");
case is List => print("list");
default => print("object");
}
Post by Günter Zöchbauer
It would also be a more powerful/less restricted switch.
Post by Jan Mostert
What would be the purpose of "when" other than replacing if-else-chains ?
Post by Yissachar Radcliffe
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
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
--
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
--
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.
Jan Mostert
2015-10-09 09:12:10 UTC
Permalink
Having the ability to set flags on how a switch statement runs would be
useful, the nofallthrough:true would probalby the most useful one.
In the cases where you're doing switch(object) or something that's not
integer / string / constant, it would be nice if dart can treat it as if
it's an if-else statement.
Post by Günter Zöchbauer
It's not only about fallthrough, switch only allows to compare integer,
string, or compile-time constants that don't have custom == implementation.
See also http://japhr.blogspot.co.at/2012/03/dart-switch.html
Post by Jan Mostert
Wouldn't adapting a switch statement be cleaner than introducing a when
keyword?
switch(obj, nofallthrough:true){
case is String => print("string");
case is List => print("list");
default => print("object");
}
Post by Günter Zöchbauer
It would also be a more powerful/less restricted switch.
Post by Jan Mostert
What would be the purpose of "when" other than replacing if-else-chains ?
Post by Yissachar Radcliffe
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
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
--
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
--
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
--
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.
Joel Trottier-Hébert
2015-10-09 13:29:12 UTC
Permalink
Pattern matching was asked several times in the past in feature requests (I
asked it too) and it was just refused.

Hopefully someone asks it again formally and they might consider it.
Post by Jan Mostert
Having the ability to set flags on how a switch statement runs would be
useful, the nofallthrough:true would probalby the most useful one.
In the cases where you're doing switch(object) or something that's not
integer / string / constant, it would be nice if dart can treat it as if
it's an if-else statement.
Post by Günter Zöchbauer
It's not only about fallthrough, switch only allows to compare integer,
string, or compile-time constants that don't have custom == implementation.
See also http://japhr.blogspot.co.at/2012/03/dart-switch.html
Post by Jan Mostert
Wouldn't adapting a switch statement be cleaner than introducing a when
keyword?
switch(obj, nofallthrough:true){
case is String => print("string");
case is List => print("list");
default => print("object");
}
Post by Günter Zöchbauer
It would also be a more powerful/less restricted switch.
Post by Jan Mostert
What would be the purpose of "when" other than replacing
if-else-chains ?
Post by Yissachar Radcliffe
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
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,
--
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
--
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
--
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
--
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.
'Paul Brauner' via Dart Misc
2015-10-09 13:36:14 UTC
Permalink
I also would like to see pattern matching in dart but not that this request
is about something much simpler: this is about bringing something akin
to scheme's
cond <http://docs.racket-lang.org/guide/conditionals.html#%28part._cond%29>
to Dart. This is simpler than pattern matching because clauses don't
introduce new bindings, and because you don't have to define what it means
to deconstruct an object (which might break encapsulation if done wrong).

That said, if this were added to the language, it would be good to do it in
such a way that a possible future addition of pattern matching would be
compatible.

Paul
Post by Joel Trottier-Hébert
Pattern matching was asked several times in the past in feature requests
(I asked it too) and it was just refused.
Hopefully someone asks it again formally and they might consider it.
Post by Jan Mostert
Having the ability to set flags on how a switch statement runs would be
useful, the nofallthrough:true would probalby the most useful one.
In the cases where you're doing switch(object) or something that's not
integer / string / constant, it would be nice if dart can treat it as if
it's an if-else statement.
Post by Günter Zöchbauer
It's not only about fallthrough, switch only allows to compare integer,
string, or compile-time constants that don't have custom == implementation.
See also http://japhr.blogspot.co.at/2012/03/dart-switch.html
Post by Jan Mostert
Wouldn't adapting a switch statement be cleaner than introducing a when
keyword?
switch(obj, nofallthrough:true){
case is String => print("string");
case is List => print("list");
default => print("object");
}
Post by Günter Zöchbauer
It would also be a more powerful/less restricted switch.
Post by Jan Mostert
What would be the purpose of "when" other than replacing
if-else-chains ?
Post by Yissachar Radcliffe
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
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,
--
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
--
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
--
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
--
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
--
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.
kc
2015-10-09 14:36:52 UTC
Permalink
Right - you could imagine a new cond/match/switch 'form' in Dart 2.0 -
which leaves the door open to further enhancements - like patterns - in a
backward compatible manner.

K.
Post by 'Paul Brauner' via Dart Misc
I also would like to see pattern matching in dart but not that this
request is about something much simpler: this is about bringing something
akin to scheme's cond
<http://docs.racket-lang.org/guide/conditionals.html#%28part._cond%29> to
Dart. This is simpler than pattern matching because clauses don't introduce
new bindings, and because you don't have to define what it means to
deconstruct an object (which might break encapsulation if done wrong).
That said, if this were added to the language, it would be good to do it
in such a way that a possible future addition of pattern matching would be
compatible.
Paul
Post by Joel Trottier-Hébert
Pattern matching was asked several times in the past in feature requests
(I asked it too) and it was just refused.
Hopefully someone asks it again formally and they might consider it.
Post by Jan Mostert
Having the ability to set flags on how a switch statement runs would be
useful, the nofallthrough:true would probalby the most useful one.
In the cases where you're doing switch(object) or something that's not
integer / string / constant, it would be nice if dart can treat it as if
it's an if-else statement.
Post by Günter Zöchbauer
It's not only about fallthrough, switch only allows to compare
integer, string, or compile-time constants that don't have custom ==
implementation.
See also http://japhr.blogspot.co.at/2012/03/dart-switch.html
Post by Jan Mostert
Wouldn't adapting a switch statement be cleaner than introducing a
when keyword?
switch(obj, nofallthrough:true){
case is String => print("string");
case is List => print("list");
default => print("object");
}
Post by Günter Zöchbauer
It would also be a more powerful/less restricted switch.
Post by Jan Mostert
What would be the purpose of "when" other than replacing
if-else-chains ?
Post by Yissachar Radcliffe
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
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,
--
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,
--
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
--
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
--
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
--
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.
kc
2015-10-09 14:32:26 UTC
Permalink
Post by Günter Zöchbauer
It's not only about fallthrough, switch only allows to compare integer,
string, or compile-time constants that don't have custom == implementation.
See also http://japhr.blogspot.co.at/2012/03/dart-switch.htm
<http://japhr.blogspot.co.at/2012/03/dart-switch.html>
Developers checking out Dart are probably looking for an alternative to the
weirdness/trickiness of JS and restrictiveness of Java.

But a Dart core form - 'switch' - is both weird/tricky and restrictive.

Dart 2.0 - fix or an alternative form.

K.
--
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.
kc
2015-10-09 14:48:12 UTC
Permalink
Post by Jan Mostert
What would be the purpose of "when" other than replacing if-else-chains ?
It's expressive. The lack of expressiveness compared to other langs was a
core reason for the lack of take up of Dart 1.x imo.

The trick is for Dart 2.x to add new forms/syntax/semantics while
maintaining a core simplicity.

The sweet spot for Dart 2.0 is something both more lighter/expressive then
C#/Java and structured/saner than JS/ES.

Dart doesn't have the restriction of a public byte code VM (C#/Java) or
backward compatibility with the bizarre (JS/ES).

K.
Post by Jan Mostert
Post by Yissachar Radcliffe
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
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
--
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.
'Bob Nystrom' via Dart Misc
2015-10-09 16:58:06 UTC
Permalink
I'd like to see pattern matching too. I actually wrote up a rough internal
proposal for it *years* ago before we even launched publicly.

In particular, I think it would help our inference story in places where
the current type promotion rules currently fall down. Consider code like
this:

class Box {
final num value;
Box(this.value);
}

main() {
Box boxed = new Box(12.34);

if (boxed.value is int) {
print(boxed.value.isEven);
}
}


You would hope there is no warning inside the body of the if. But analysis
has no way of know the .value getter returns the same value every time you
call it, so the previous is check on the result tells it nothing about the
later usage of it. You end up having to write redundant code like:

if (boxed.value is int) {
print((boxed.value as int).isEven);
}


If we had pattern matching—a way to do a type test and bind a new scoped
variable in one step—you'd have a cleaner way to express this. This is why
almost every language that does inference also has some kind of
type-test-and-bind syntax or eventually gets one.

– bob
Post by kc
Post by Jan Mostert
What would be the purpose of "when" other than replacing if-else-chains ?
It's expressive. The lack of expressiveness compared to other langs was a
core reason for the lack of take up of Dart 1.x imo.
The trick is for Dart 2.x to add new forms/syntax/semantics while
maintaining a core simplicity.
The sweet spot for Dart 2.0 is something both more lighter/expressive
then C#/Java and structured/saner than JS/ES.
Dart doesn't have the restriction of a public byte code VM (C#/Java) or
backward compatibility with the bizarre (JS/ES).
K.
Post by Jan Mostert
Post by Yissachar Radcliffe
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
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
--
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
--
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.
Yissachar Radcliffe
2015-10-09 22:19:23 UTC
Permalink
Paul is correct that this feature request is not dependent on
pattern-matching, though that would be great to add.
Post by 'Bob Nystrom' via Dart Misc
I'd like to see pattern matching too. I actually wrote up a rough internal
proposal for it *years* ago before we even launched publicly.
In particular, I think it would help our inference story in places where
the current type promotion rules currently fall down. Consider code like
class Box {
final num value;
Box(this.value);
}
main() {
Box boxed = new Box(12.34);
if (boxed.value is int) {
print(boxed.value.isEven);
}
}
You would hope there is no warning inside the body of the if. But analysis
has no way of know the .value getter returns the same value every time you
call it, so the previous is check on the result tells it nothing about the
if (boxed.value is int) {
print((boxed.value as int).isEven);
}
If we had pattern matching—a way to do a type test and bind a new scoped
variable in one step—you'd have a cleaner way to express this. This is why
almost every language that does inference also has some kind of
type-test-and-bind syntax or eventually gets one.
– bob
Post by kc
Post by Jan Mostert
What would be the purpose of "when" other than replacing if-else-chains ?
It's expressive. The lack of expressiveness compared to other langs was a
core reason for the lack of take up of Dart 1.x imo.
The trick is for Dart 2.x to add new forms/syntax/semantics while
maintaining a core simplicity.
The sweet spot for Dart 2.0 is something both more lighter/expressive
then C#/Java and structured/saner than JS/ES.
Dart doesn't have the restriction of a public byte code VM (C#/Java) or
backward compatibility with the bizarre (JS/ES).
K.
Post by Jan Mostert
Post by Yissachar Radcliffe
http://kotlinlang.org/docs/reference/control-flow.html#when-expression
if (obj is String) {
print("string");
} else if (obj is List) {
print("list");
} else {
print("object");
}
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
--
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
--
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.
Continue reading on narkive:
Loading...