Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] syntax for variable arity?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Remi VANICAT <vanicat@l...>
Subject: Re: [Caml-list] syntax for variable arity?
Michael Leary <leary@nwlink.com> writes:

> I want to write a function to abstract a series of functions with different
> numbers of args like these two:
> 
> let debug message = printf "Debug %s\n" message; fso
> let debug_line start_angle start_radius end_angle end_radius =
>   printf "DebugLine %f %f %f %f\n" start_angle start_radius end_angle end_radius;
>   fso
> 
> 
> I was thinking of:
> 
> type command =
>   Debug
>   | Debug_line
>   | ...

won't work : ocaml can't make a so easily function with different
numbers of args.


> 
> let com command _ =
>   match command with
>   | Debug ->
>       doit "some special format string" _
>   | Debug_line ->
>       doit "other format string" _
> 
> let doit _ = printf _ ; fso (* fso defined elsewhere *)
> 
> 
> let () = com Debug "debug message"
> ...
> let () = com Debug_line start_angle start_radius end_angle end_radius
> 
> 
> I know _ matches any value(s??), but I'm not clear on how to use the
> match elsewhere...  am I close?  Is there a better way to structure
> this?  

_ match only one value, it's the same to do

let f _ = something

and

let f x = sommething where x is not used


the solution for your problem is something as :

type command
  | Debug of string
  | Debugline of float * float * float * float

let com command =
  begin
    match command with
      | Debug st -> printf "Debug %s\n" st
      | Debugline (start_angle, start_radius, end_angle, end_radius) -> 
          printf "DebugLine %f %f %f %f\n" start_angle start_radius
            end_angle end_radius
  end; fso

-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr