Patterns always bind 'fresh' names. There is a very limited set of things allowed on the left-hand-side of an assignment operator (see 6.5.3, below). This little example seems pretty innocuous, but I think it would require a substantial and confusing change to the language to enable 'patterns with mutables' to be assignable.

[link:research.microsoft.com]

By on 12/3/2008 7:14 PM ()

While we're on the subject of what can appear on the left hand side of a pattern, why can't I do something like this?

1
2
3
4
let f x y = 
    match x with
       (y, _) -> "fst x = y"
       | _ -> "fst x != y"

I can understand not allowing it for lists, records, or basically anything non-primitive, but it would be rather useful, better than the more verbose when (x=y). I guess it would only be feasible for primitive types (strings, ints, floats, doubles, etc), but nothing else, but is the reason this isn't supported merely because of the incosistency it would introduce with other types?

By on 12/3/2008 8:15 PM ()

Because match patterns like that are for binding values to names, not comparing them for equality.

Since match patterns are syntactically equivalent to function patterns, this concept becomes more apparent when your function is written this way:

1
2
3
4
5
6
let f = function

    | (y, _) -> fun y -> "fst x = y"

    | _ -> fun y -> "fst x != y"
By on 12/4/2008 7:04 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper