Click here to Skip to main content
15,884,472 members

The Insider News

   

The Insider News is for breaking IT and Software development news. Post your news, your alerts and your inside scoops. This is an IT news-only forum - all off-topic, non-news posts will be removed. If you wish to ask a programming question please post it here.

Get The Daily Insider direct to your mailbox every day. Subscribe now!

 
NewsStroustrup: Why the 35-year-old C++ still dominates 'real' dev Pin
Kent Sharkey17-Aug-14 16:24
staffKent Sharkey17-Aug-14 16:24 
GeneralRe: Stroustrup: Why the 35-year-old C++ still dominates 'real' dev Pin
Super Lloyd17-Aug-14 19:41
Super Lloyd17-Aug-14 19:41 
GeneralRe: Stroustrup: Why the 35-year-old C++ still dominates 'real' dev Pin
Shao Voon Wong17-Aug-14 22:00
mvaShao Voon Wong17-Aug-14 22:00 
GeneralRe: Stroustrup: Why the 35-year-old C++ still dominates 'real' dev Pin
LloydA11117-Aug-14 23:41
LloydA11117-Aug-14 23:41 
GeneralRe: Stroustrup: Why the 35-year-old C++ still dominates 'real' dev Pin
ed welch18-Aug-14 8:45
ed welch18-Aug-14 8:45 
NewsPattern Matching in C# 6 and VB 12 Pin
Kent Sharkey17-Aug-14 16:23
staffKent Sharkey17-Aug-14 16:23 
GeneralRe: Pattern Matching in C# 6 and VB 12 Pin
Dan Neely18-Aug-14 3:58
Dan Neely18-Aug-14 3:58 
GeneralRe: Pattern Matching in C# 6 and VB 12 Pin
Rob Grainger20-Aug-14 3:53
Rob Grainger20-Aug-14 3:53 
I'll give it a go...

First, the new record class construct allows creating classes similar to types in Haskell. They are purely structural, and immutable.
C#
public record class Cartesian(double x : X, double y : Y);    // Not really sure why they have x : X here!

which is equivalent to:
C#
public class Cartesian {
    private readonly double $X;
    private readonly double $Y;
    
    public Cartesian(double x, double y) { this.$X = x; this.$Y = y; }

    public double X { get { return this.$X; }
    public double Y { get { return this.$Y; }

    // pattern-is operator
    public static bool operator is (Cartesian c, out double x, out double y) { x = c.X; y = c.Y; return true; }

    // Also overrides object.Equals, object.GetHashCode and object.ToString() - all fairly obvious implementations.
}

Unlike normal classes, record classes do not need "new":
C#
var c = Cartesian(3, 4);

The crucial part here is the "is" operator, which is used for pattern matching.
C#
if (c is Cartesian(var x, y) {
    Console.WriteLine("c.X == " + x + ", c.Y == " + y);        // Note that x and y have been matched; this is called "deconstruction" in Haskell.
}

If you're only interested in one of the fields, the other can be matched using a wildcard "*":
C#
if (c is Cartesian(var x, *) {
    Console.WriteLine("c.X == " + x);
}

Another common use (in C#) will be to robustly handle nulls:
C#
int? x;
...
if (x is int v) [
    // Use v here, confident it is not null.
}

This corresponds to use of the Maybe monad in Haskell.

More complex patten matches can be constructed. These are useful for deconstructing types such as trees.
Consider a set of record classes representing expressions:
C#
abstract class Expr;
record class X() : Expr;
record class Const(double value) : Expr;
record class Add(Expr left, Expr right) : Expr;
record class Mult(Expr left, Expr right) : Expr;
record class Negate(Expr value) : Expr;

An expression can then be represented as:
C#
var expr = Add(Const(3.0), Negate(Mult(3, 5)));

And pattern matching (this time using switch) can deconstruct it nicely:
C#
Expr Simplify(Expr e) {
    switch (e) {
        case Mult(Const(0), *): return Const(0);
        case Mult(*, Const(0)): return Const(0);
        case Mult(Const(1), var x): return Simplify(x);    // Note recursive matching going on here!
        case Mult(var x, Const(1)): return Simplify(x);
        case Mult(Const(var l), Const(var r)): return Const(l * r);
        case Add(Const(0), var x): return Simplify(x);
        case Add(var x, Const(0): return Simplify(x);
        case Add(Const(var l), Const(var r)): return Const(l + r);
        case Negate(Const(var k)): return Const(-k);
        default: return e;
    }
}

I presume that, like Haskell, the first matching expression wins.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.

GeneralRe: Pattern Matching in C# 6 and VB 12 Pin
Dan Neely21-Aug-14 2:35
Dan Neely21-Aug-14 2:35 
GeneralRe: Pattern Matching in C# 6 and VB 12 Pin
Rob Grainger23-Aug-14 6:58
Rob Grainger23-Aug-14 6:58 
GeneralRe: Pattern Matching in C# 6 and VB 12 Pin
Bernhard Hiller19-Aug-14 0:37
Bernhard Hiller19-Aug-14 0:37 
GeneralRe: Pattern Matching in C# 6 and VB 12 Pin
Dan Neely19-Aug-14 2:32
Dan Neely19-Aug-14 2:32 
GeneralRe: Pattern Matching in C# 6 and VB 12 Pin
Bernhard Hiller19-Aug-14 6:48
Bernhard Hiller19-Aug-14 6:48 
GeneralRe: Pattern Matching in C# 6 and VB 12 Pin
Rob Grainger20-Aug-14 3:54
Rob Grainger20-Aug-14 3:54 
NewsDirectX 12 - High Performance and High Power Savings Pin
Kent Sharkey17-Aug-14 16:22
staffKent Sharkey17-Aug-14 16:22 
NewsWindows as a Service gets name-checked, again, by Microsoft Pin
Kent Sharkey17-Aug-14 16:19
staffKent Sharkey17-Aug-14 16:19 
NewsMicrosoft to deliver Windows 'Threshold' tech preview around late September Pin
Kent Sharkey17-Aug-14 16:18
staffKent Sharkey17-Aug-14 16:18 
NewsMicrosoft acknowledges issues in Windows 8.1 August Update, asks users to uninstall some update files Pin
Kent Sharkey17-Aug-14 16:17
staffKent Sharkey17-Aug-14 16:17 
NewsSmartphone at 20: IBM Simon becomes museum exhibit Pin
JMK-NI16-Aug-14 9:36
professionalJMK-NI16-Aug-14 9:36 
NewsPop-up ad creator: 'I'm sorry' PinPopular
Ravi Bhavnani15-Aug-14 9:12
professionalRavi Bhavnani15-Aug-14 9:12 
AnswerRe: Pop-up ad creator: 'I'm sorry' Pin
Afzaal Ahmad Zeeshan15-Aug-14 9:56
professionalAfzaal Ahmad Zeeshan15-Aug-14 9:56 
GeneralRe: Pop-up ad creator: 'I'm sorry' Pin
Michael Gazonda15-Aug-14 10:07
professionalMichael Gazonda15-Aug-14 10:07 
GeneralRe: Pop-up ad creator: 'I'm sorry' Pin
Jeremy Falcon15-Aug-14 10:18
professionalJeremy Falcon15-Aug-14 10:18 
GeneralRe: Pop-up ad creator: 'I'm sorry' Pin
Michael Gazonda15-Aug-14 10:37
professionalMichael Gazonda15-Aug-14 10:37 
GeneralRe: Pop-up ad creator: 'I'm sorry' Pin
Jeremy Falcon15-Aug-14 10:39
professionalJeremy Falcon15-Aug-14 10:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.