Because of patterns. Look this example:

1
2
3
4
let printer = function
 | Two -> printfn "two"
 | Three -> printfn "three"
 | x -> printfn "other (%A)" x

The compiler knows that Two and Three are patterns (union case or active pattern), while x is a wildcard pattern (x is then defined as an identifier in the right part). Replace the x with Foo and you'll get a warning.

Laurent.

By on 8/5/2009 4:55 PM ()

Well, that's an odd way of dealing with the problem. I always thought it was just making use of the type system to determine whether or not x had already been defined prior as an identifier of a type that matches the other cases, and then binding a new symbol if not.

I guess it's this sort of weird dependence on alphabetical characters that makes Unicode difficult to get right in F#.

By on 8/5/2009 11:11 PM ()

Right, but relying on the type system could be dangerous too. Imagine you do a spelling mistake, or if you edit your sum type and forget to update the pattern matching, you'll be happy that your pattern Four doesn't suddenly become something completely different. It also improves readability: you instantaneously know which is a pattern and which is wildcard.

A solution, not dependent on alphabetical characters, would be to introduce an operator (e.g. put a "?" before the word).

Laurent.

By on 8/6/2009 12:52 AM ()

That's a mistake that happens in practically any language, F# included. I'm sure you've heard of cases where field names were confused with local variable names in C#. The exact same can still happen in F#.

Correct me if I'm wrong, but I doubt safety was the reason for this; I think it's more likely historic and a desire to make the language "look like math". If safety were the reason for it, other naming conventions would have been enforced as well, including capitalization of other types of types.

By on 8/7/2009 6:43 AM ()

Sure, this comes from historical reasons. That's the exact way OCaml behaves (actually, F# has removed several of these restrictions, as it's possible to let-define an uppercase identifier). However, "historical reasons" are not always bad.

I don't really like the compiler makes a difference of lowercase/uppercase letters, but I think removing the distinction is worse (unless an operator is introduced).

Look at the following code:

1
2
3
let f = function
 | [foo] -> true
 | _ -> false

If "foo" is not defined as a sum-case (or active pattern), then the function matches any list with a single element. Imagine someone then defines the sum type (somewhere in the file or in an open module):

1
type break_the_code = foo | bar

This simple definition, that was not intended to be connected with f, has inadvertently changed the meaning of f. This is extremely dangerous. Fortunately, this last definition is not allowed.

Laurent.

By on 8/7/2009 1:52 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