From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Edgar Friendly <thelema314@gmail.com>
Cc: caml-list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Compiler feature - useful or not?
Date: Wed, 14 Nov 2007 12:01:11 +0100 [thread overview]
Message-ID: <1195038071.29197.85.camel@localhost.localdomain> (raw)
In-Reply-To: <473A363F.2080301@gmail.com>
Am Dienstag, den 13.11.2007, 17:41 -0600 schrieb Edgar Friendly:
> When one writes
>
> type row = int
> type col = int
>
> This allows one to use the type names "row" and "col" as synonyms of
> int. But it doesn't prevent one from using a value of type row in the
> place of a value of type col. OCaml allows us to enforce row as
> distinct from int two ways:
>
> 1) Variants:
> type row = Row of int
> type col = Col of int
>
> Downside: unnecessary boxing and tagging
> conversion from row -> int: (fun r -> match r with Row i -> i)
Note that you can write much shorter:
(fun Row i -> i), or
let some_function (Row i) = ...
If the small runtime penalty is not essential, this is probably the best
way to do it.
Gerd
> conversion from int -> row: (fun i -> Row i)
>
> 2) Functors:
> module type RowCol =
> sig
> type row
> val int_of_row : row -> int
> val row_of_int : int -> row
> type col
> val int_of_col : col -> int
> val col_of_int : int -> col
> end
>
> module Main = functor (RC: RowCol) -> struct
> (* REST OF PROGRAM HERE *)
> end
>
> Any code using rows and cols could be written to take a module as a
> parameter, and because of the abstraction granted when doing so, type
> safety is ensured.
>
> Downside: functor overhead, misuse of functors, need to write
> boilerplate conversion functions
> conversion from row -> int, int -> row: provided by RowCol boilerplate
>
> IS THE FOLLOWING POSSIBLE:
> Modify the type system such that one can declare
>
> type row = new int
> type col = new int
>
> Row and col would thus become distinct from int, and require explicit
> casting/coercion (2 :> row). There would be no runtime overhead for use
> of these types, only bookkeeping overhead at compilation.
>
> Downside: compiler changes (hopefully not too extensive)
> conversion from row -> int: (fun r -> (r :> int)) (* might need (r : row
> :> int) if it's not already inferred *)
> conversion from int -> row: (fun i -> (i :> row))
>
> Thoughts? Do any of you use Variants or Functors to do this now? Do
> you find this style of typing useful?
>
> E.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
--
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany
gerd@gerd-stolpmann.de http://www.gerd-stolpmann.de
Phone: +49-6151-153855 Fax: +49-6151-997714
------------------------------------------------------------
next prev parent reply other threads:[~2007-11-14 11:01 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-13 23:41 Edgar Friendly
2007-11-14 0:08 ` [Caml-list] " Yaron Minsky
2007-11-14 0:21 ` Martin Jambon
2007-11-14 7:58 ` Pierre Weis
2007-11-14 12:37 ` Alain Frisch
2007-11-14 13:56 ` Virgile Prevosto
2007-11-14 14:35 ` Pierre Weis
2007-11-14 16:38 ` Alain Frisch
2007-11-14 18:43 ` Pierre Weis
2007-11-14 19:19 ` Edgar Friendly
2007-11-15 6:29 ` Alain Frisch
2007-11-15 13:26 ` Pierre Weis
2007-11-15 17:29 ` Edgar Friendly
2007-11-15 20:28 ` Fernando Alegre
2007-11-16 0:47 ` Brian Hurt
2007-11-15 22:37 ` Michaël Le Barbier
2007-11-15 22:24 ` Michaël Le Barbier
2007-11-16 0:30 ` Yaron Minsky
2007-11-16 1:51 ` Martin Jambon
2007-11-16 9:23 ` Alain Frisch
2007-11-16 14:17 ` rossberg
2007-11-16 15:08 ` Martin Jambon
2007-11-16 16:43 ` Martin Jambon
2007-11-16 16:46 ` Till Varoquaux
2007-11-16 17:27 ` Edgar Friendly
2007-11-16 17:47 ` Martin Jambon
2007-11-16 17:54 ` Edgar Friendly
2007-11-16 18:10 ` Fernando Alegre
2007-11-16 19:18 ` David Allsopp
2007-11-16 19:32 ` Fernando Alegre
2007-11-16 19:50 ` Gerd Stolpmann
2007-11-16 17:31 ` Fernando Alegre
2007-11-16 17:43 ` Edgar Friendly
2007-11-16 0:46 ` Christophe TROESTLER
2007-11-16 8:23 ` Andrej Bauer
2007-11-16 8:58 ` Jean-Christophe Filliâtre
2007-11-16 9:13 ` Andrej Bauer
2007-11-16 9:48 ` Christophe TROESTLER
2007-11-14 16:57 ` Edgar Friendly
2007-11-14 21:04 ` Pierre Weis
2007-11-14 22:09 ` Edgar Friendly
2007-11-15 0:17 ` Jacques Garrigue
2007-11-15 6:23 ` Edgar Friendly
2007-11-15 10:53 ` Vincent Hanquez
2007-11-15 13:48 ` Jacques Carette
2007-11-15 14:43 ` Jon Harrop
2007-11-15 16:54 ` Martin Jambon
2007-11-14 16:09 ` Edgar Friendly
2007-11-14 16:20 ` Brian Hurt
2007-11-14 11:01 ` Gerd Stolpmann [this message]
2007-11-14 10:57 ` Jon Harrop
2007-11-14 14:37 ` Zheng Li
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1195038071.29197.85.camel@localhost.localdomain \
--to=info@gerd-stolpmann.de \
--cc=caml-list@yquem.inria.fr \
--cc=thelema314@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox