List Info

Thread: "ocaml_beginners"::[] Giving a genuine OCaml type to enum ints ?




"ocaml_beginners"::[] Giving a genuine OCaml type to enum ints ?
country flaguser name
France
2008-03-14 16:13:47

Hi !

Trying to play with Ocaml curses

http://www.nongnu.org/ocaml-tmk/

and its colours, for example.

Things are, of course, looking very close to C curses because it's a binding.

Please consider the related init_pair / A.color_pair functions.

Some arbitrary integers are used, kind of 'handles' for the pairs :

assert (init_pair 1 Color.red Color.white);
assert (init_pair 2 Color.blue Color.yellow) ...

A.color_pair 1
accesses red on white pair

Please what is the way to program things cleanly ? I.e. giving a true OCaml
type to these ugly enumerated integers, à la C ?

Would be glad to define a variant type like this :
type colour_pairs = Red_on_white | Blue_on_yellow | ...

And then to call wrapper functions around 'init_pair' and 'A.color_pair'.
The aim is to shut unclean things in a small module in order to forget them.
So it would become possible to work the usual OCaml safe way.

Any light ?

Thanks,

Fabrice

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Giving a genuine OCaml type to enum ints ?
country flaguser name
United States
2008-03-14 17:29:41

Fabrice Marchant wrote:
> Trying to play with Ocaml curses
>
> http://www.nongnu.org/ocaml-tmk/ <http://www.nongnu.org/ocaml-tmk/>
&gt;
> and its colours, for example.
>
> Things are, of course, looking very close to C curses because it's a
> binding.
>
> Please consider the related init_pair / A.color_pair functions.
>
> Some arbitrary integers are used, kind of 'handles' for the pairs :
>
> assert (init_pair 1 Color.red Color.white);
> assert (init_pair 2 Color.blue Color.yellow) ...
>
> A.color_pair 1
> accesses red on white pair
>;
> Please what is the way to program things cleanly ? I.e. giving a true OCaml
&gt; type to these ugly enumerated integers, à la C ?
>
> Would be glad to define a variant type like this :
> type colour_pairs = Red_on_white | Blue_on_yellow | ...
>
> And then to call wrapper functions around 'init_pair' and 'A.color_pair'.
&gt; The aim is to shut unclean things in a small module in order to forget them.
&gt; So it would become possible to work the usual OCaml safe way.

How about integer constants:

let color_box = 1
and color_background = 2
and color_menu = 3;;

assert (init_pair color_box Color.red Color.white);
assert (init_pair color_background Color.blue Color.yellow) ...
assert (init_pair color_menu Color.green Color.cyan) ...

A.color_pair color_background

(keep the names abstract rather than red_on_white, so you can change
colors easily)

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Giving a genuine OCaml type to enum ints ?
country flaguser name
France
2008-03-14 17:03:49

Many thanks,

On Fri, 14 Mar 2008 15:29:41 -0700
Karl Zilles < zilles%401969web.com">zilles1969web.com> wrote:

> How about integer constants:
>
> let color_box = 1
> and color_background = 2
> and color_menu = 3;;
>
> assert (init_pair color_box Color.red Color.white);
> assert (init_pair color_background Color.blue Color.yellow) ...
> assert (init_pair color_menu Color.green Color.cyan) ...
>
> A.color_pair color_background
Maybe with this ( GUI ? )example, there are few chances to get some :
A.color_pair 944

because the colour_pair arguments is probably immediately explicited by one of the 3 constants, not the result of a computation.

However I would be happy to give the compiler the possibility to perform its static type checking.

> (keep the names abstract rather than red_on_white, so you can change
> colors easily)
I see, but this would remain possible with an extra definition around :
let color_box = red_on_white

Before to see the perfect solution, I'll nevertheless program things the way you've explained.

Regards,

Fabrice

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )