www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Basic pattern matching in C# 6

reply "bearophile" <bearophileHUGS lycos.com> writes:


http://www.infoq.com/news/2014/08/Pattern-Matching

But it's still rather limited:

Under the current draft specification, there is no support for 
range checks. This means you cannot write patterns such as “a is 
Location( > 0, 1 to 5, <= 10)”. Nor is there support for 
matching elements in a list or enumeration.<
Bye, bearophile
Aug 15 2014
next sibling parent reply "Idan Arye" <GenericNPC gmail.com> writes:
On Friday, 15 August 2014 at 10:03:39 UTC, bearophile wrote:


 http://www.infoq.com/news/2014/08/Pattern-Matching

 But it's still rather limited:

Under the current draft specification, there is no support for 
range checks. This means you cannot write patterns such as “a 
is Location( > 0, 1 to 5, <= 10)”. Nor is there support for 
matching elements in a list or enumeration.<
Bye, bearophile
Pattern matching will always be awkward in languages where the data types were not designed to be pattern-matched. In that languages like ML and Haskell is that in the former the order of fields is an implementation detail while in the later it's an actual part of the type's interface definition. ML, when you write `datatype foo = Foo of int * int * int`, the actual representation of the type is a tuple of 3 integers - the order matters, and it defines both the constructor and the pattern matching. class Foo { private int X{get; set;} private int Z{get; set;} private int Y{get; set;} public Foo(int x, int y, int z) { this.x = X; this.y = Y; this.z = Z; } public static bool operator is(Foo foo, out int z, out int y, out int x) { x = foo.x; y = foo.y; z = foo.z; } } Debug.Assert(new Foo(1, 2, 3) is Foo(3, 2, 1); And note that both the constructor and the `is` operator are different from the order described in memory. Scala solved it nicely with case classes, which get translated to regular JVM classes but generate the structuring-destructuring code automatically and consistently, but Scala can do it because it's type declaration syntax was designed to support the style will just be awkward. classes?
Aug 15 2014
next sibling parent "Kagamin" <spam here.lot> writes:
On Friday, 15 August 2014 at 12:01:59 UTC, Idan Arye wrote:
 Pattern matching will always be awkward in languages where the 
 data types were not designed to be pattern-matched. In that 

 languages like ML and Haskell is that in the former the order 
 of fields is an implementation detail while in the later it's 
 an actual part of the type's interface definition.
Ordering works in D: http://dpaste.dzfl.pl/64b453444ecc
Aug 15 2014
prev sibling parent "Yota" <yotaxp thatGoogleMailThing.com> writes:
On Friday, 15 August 2014 at 12:01:59 UTC, Idan Arye wrote:

 classes?
The proposal includes the following syntax for class declaration: public record class Point(int x: X, int y: Y); Which creates a class with a primary constructor, properties, operator is, Equals(), GetHashCode(), etc.
Aug 15 2014
prev sibling parent "Yota" <yotaxp thatGoogleMailThing.com> writes:
On Friday, 15 August 2014 at 10:03:39 UTC, bearophile wrote:


 http://www.infoq.com/news/2014/08/Pattern-Matching

 But it's still rather limited:

Under the current draft specification, there is no support for 
range checks. This means you cannot write patterns such as “a 
is Location( > 0, 1 to 5, <= 10)”. Nor is there support for 
matching elements in a list or enumeration.<
Bye, bearophile
If I'm not mistaken, you could do this. public static class GreaterThan { public static bool operator is(int val, int test) { return val > test; } } and then... case Location(GreaterThan(0), BetweenInclusive(1, 5), LessOrEqualTo(10)): Though we should eventually be able to do something like the following: case Location(var x, var y, var z) when (x > 0 && y >= 1 && y <= 5 && z <= 10):
Aug 15 2014