Browse thread
[Caml-list] Newbie: Aliases for polymorphic variants
- andrew cooke
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2004-02-28 (12:44) |
From: | andrew cooke <andrew@a...> |
Subject: | [Caml-list] Newbie: Aliases for polymorphic variants |
Hi, I've just been looking at polymorphic variants. They seem quite neat. However, I have some questions.... 1 - It seems to me that they could be particularly useful for solving those nasty practical problems that occur way down the line with old systems where you want to combine previously separate features in some way. However, to do that, it seems like all code has to use polymorphic variants from the start. Is that right? That seems OK, except that they seem to make for a lot more typing. Is there a syntax for defining something like type aliases? I'm looking for something like: alias 'a Node = `Node int * 'a `Node * 'a `Node so that I can type just 'a Node instead of `Node int * 'a `Node * 'a `Node everywhere 2 - Alternatively, say I have type 'a beginWithF = [Foo of 'a | Flob of int * 'a] type 'a beginWithG = [Goo of 'a] is there a syntax something like: type 'a beginWithFOrG = ['a beginWithF | 'a beginWithG] or do I have to explicitly list all the separate constructors? 3 - Am I right in thinking that fucntions cannot be defined with pattern matching to the left of "=" in OCaml? (ie not like Haskell) If this were possible then I think you'd have ad-hoc polymorphism for free because you could have: fun sum (`Integer i) (`Integer j) = `Integer (i + j) fun sum (`Real x) (`Real y) = `Real (x +. y) Does that make sense? 4 - Taking (3) further, couldn't you give all types an implicit polymorphic variant constructor? Then you could have types like [int | float] directly. Does that make sense? Thanks, Andrew -- __ _ __ ___ ___| |_____ work web site: http://www.ctio.noao.edu/~andrew / _` / _/ _ \/ _ \ / / -_) personal web site: http://www.acooke.org/andrew \__,_\__\___/\___/_\_\___| personal gallery: http://www.acooke.org/pancito ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners