What you suggest is not good practice though. Its not flexible and also

does not exhaust all possibilities so less type safe (in the same manner as an if without an else). Also, suppose you wished to extend expr to

be able to return more than true or false or indeed something else altogether. Or needing to do some more logic. Such a function built into the language would be so limited as to be completely useless.

this is a tiny bit shorter however:

1
let isAnd = function And _ -> true | _ -> false
By on 6/16/2009 4:26 PM ()

What you suggest is not good practice though. Its not flexible and also

does not exhaust all possibilities so less type safe (in the same manner as an if without an else). Also, suppose you wished to extend expr to

be able to return more than true or false or indeed something else altogether. Or needing to do some more logic. Such a function built into the language would be so limited as to be completely useless.

this is a tiny bit shorter however:

1
let isAnd = function And _ -> true | _ -> false

It is just as (un)safe as the catchall _ pattern. If you don't like this, you shouldn't be using _ either - which I think would be highly impractical..

By on 6/18/2009 12:50 AM ()

What you suggest is not good

practice though. Its not flexible and also

does not exhaust all possibilities so less type safe (in the same

manner as an if without an else). Also, suppose you wished to extend

expr to

be able to return more than true or false or indeed something else

altogether. Or needing to do some more logic. Such a function built

into the language would be so limited as to be completely useless.

this is a tiny bit shorter however:

1
let isAnd = function And _ -> true | _ -> false

It

is just as (un)safe as the catchall _ pattern. If you don't like this,

you shouldn't be using _ either - which I think would be highly

impractical..

I do not think it to be quite so. A wildcard pattern is used to ensure that all possible matches are exhausted. A default case. It exists so that when used correctly a match not found exception is not thrown at run time.

The function wanted by the OP is one that would automatically complete the pattern and add a false case or the default value of an inferred type I suppose. So it would be the same as I gave above but done automatically. Considering the range of types and values that may be in use or one where a default value might not make sense, implementing such a function might not be so practical.

Otherwise if a none 'And' Tag is passed to the function an exception will be thrown.

By on 6/18/2009 4:01 PM ()

I do not think it to be quite so. A wildcard pattern is used to ensure that all possible matches are exhausted. A default case. It exists so that when used correctly a match not found exception is not thrown at run time.

The function wanted by the OP is one that would automatically complete the pattern and add a false case or the default value of an inferred type I suppose. So it would be the same as I gave above but done automatically. Considering the range of types and values that may be in use or one where a default value might not make sense, implementing such a function might not be so practical.

Otherwise if a none 'And' Tag is passed to the function an exception will be thrown.

Huh? the type of isAnd is Expr -> bool, so only a value of type Expr can be given to it. If the instance is And, it returns true, and false otherwise. It can never throw an exception, and it's completely type safe. I thought you would be worried about adding or removing a union case, where you might miss some points in the program that this affects, but like I said, that is the same with the catchall pattern.

By on 6/19/2009 12:11 PM ()

We are not talking about the same thing I think because I agree with that statement. I was criticizing this syntax "What I'd like to have is: let isAnd v = v matches And". What you are highlighting is different from that and more explaining the code I offered as alternative.

Nice blog btw.

By on 6/19/2009 8:12 PM ()

The generated IL code has e.g. an "IsAnd()" member function that returns bool. But unfortunately this method is not exposed to F# code. Making this available is tracked by bug "4803: Expose union IsCaseN properties to F#" in our internal bug database. For now, boilerplate code like you have is the best alternative.

By on 6/15/2009 4:07 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