Using a range in case clause

79 views
Skip to first unread message

RedDevil

unread,
Jul 31, 2013, 3:50:27 PM7/31/13
to clay-l...@googlegroups.com
Hi, I am looking into the switch statement and would like to know if a case clause can have a range of values without having to specity all.
e.g is there a way to do something like case(129 to 191)?

Joe Groff

unread,
Jul 31, 2013, 10:44:09 PM7/31/13
to clay-l...@googlegroups.com
On Wednesday, July 31, 2013, RedDevil wrote:
Hi, I am looking into the switch statement and would like to know if a case clause can have a range of values without having to specity all.
e.g is there a way to do something like case(129 to 191)?

The switch statement desugars into calls to the case? function, which is implemented in the library to compare the switch value against every case value using the == operator. You could overload case? to accept a range type, as in:

record Range[T](lo:T, hi:T);

[T] overload case?(x:T, range:Range[T]) : Bool = lo < x && x < hi;

-Joe
Reply all
Reply to author
Forward
0 new messages