Version française
Home     About     Download     Resources     Contact us    
Browse thread
Option functions (or lack thereof) + operator for composition
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: bluestorm <bluestorm.dylc@g...>
Subject: Re: [Caml-list] Option functions (or lack thereof) + operator for composition
On Tue, Nov 16, 2010 at 12:27 PM, Serge Le Huitouze <
serge.lehuitouze@gmail.com> wrote:

> It seems that there is no predefined function to test an "'a option" for
> being
> specifically "None" or "Some _". This seems to be confirmed by the very
> existence of:
> http://ocaml-lib.sourceforge.net/doc/Option.html
> which defines such functions ("is_none" and "is_some").
> I found it weird to be forced to use "match" expressions in my code for
> doing that, e.g.:
> *  let curSelectedRow = ref None in
> *  let updateButtonsStatus () =
> *      button_remove#misc#set_sensitive
> *              (match !curSelectedRow with None -> false | _ -> true)
> *  in
> *  ...
>

Though useless in this case (just use ((<>) None)), there is a very nice
syntax extension proposal by Richard Jones to transform any pattern into a
boolean predicate : (matches p) would be equivalent to a function that
returns true if the input matches the pattern. I have implemented it in
camlp4 (the code may be slightly bitrotten) in case you're interested:
  http://bluestorm.info/camlp4/pa_matches.ml.html



> I'm not familiar with operators and their precedence, but I wonder: is it
> possible to do something similar with OCaml?
>

In OCaml, the associativity/precedence of an operator is defined by its
first symbols. For example (++$*) has exactly the precedence of (+). You can
find all precedence classes and their prefixes in the OCaml Manual:
  http://caml.inria.fr/pub/docs/manual-ocaml/expr.html#@manual.kwd33
<http://caml.inria.fr/pub/docs/manual-ocaml/expr.html#@manual.kwd33>Though
this is less flexible that other languages that let you choose precedence
and associativity on a case per case basis, it gives a nice homogeneity to
binary operators: you don't need to look at the operator definition site to
have a (vague, unless you know the table by hearth) idea of its syntactic
properties.