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: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Option functions (or lack thereof) + operator for composition
On 2010/11/16, at 20:27, Serge Le Huitouze wrote:
> 1. Option type
> ****************
> It seems that there is no predefined function to test an "'a option" for being
> specifically "None" or "Some _".

In ocaml you can compare at any type.
So you can just write
  (!curSelectedRow <> None)
which explains why there is no such function in the standard library.

> 2. Operator for composition (and its precedence)
> ********************************************************
> To get rid of many warnings, I wrapped some calls (the "connect" calls of
> my widgets) into "ignore (f x y)" statements.
> I've no particular grief in using "ignore", but I find the parentheses
> *really* annoying.
> 
> In Haskell, I would write "ignore $ f x y", which I find much lighter weight.
> 
> I'm not familiar with operators and their precedence, but I wonder: is it
> possible to do something similar with OCaml?

You can, but unfortunately $ does not have the right associativity.

# let ($) f x = f x;;
val ( $ ) : ('a -> 'b) -> 'a -> 'b = <fun>
# ignore $ 1+1;;
- : unit = ()
# succ $ succ 3;;
- : int = 5
# succ $ succ $ succ 3;;
Error: This expression has type int -> int
       but an expression was expected of type int

As you can see, it works fine for one argument, but doesn't work if you nest them.
(Actually, this is a question of point of view, since you can use it
in place of parentheses to sequence multiple arguments)

If you want the compositionality, you can use something starting with @:

# let (@@) f x = f x;;
val ( @@ ) : ('a -> 'b) -> 'a -> 'b = <fun>
# succ @@ succ @@ succ 3;;
- : int = 6

Note however that there is a simpler way to circumvent the problem:
use the "-w s" option on the command line, disabling the statement warning.
All my lablgtk code uses this flag :-)

Jacques Garrigue