Version française
Home     About     Download     Resources     Contact us    
Browse thread
Fine-grained types with more general library
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Edgar Friendly <thelema314@g...>
Subject: Re: [Caml-list] Fine-grained types with more general library
Dawid Toton wrote:
> I have a type (e.g. of path to file)
> 
> type path = string list
> 
> and many operations (e.g. path manipulation). Then I need to have two
> kinds of paths (e.g. local and remote) and want type system to enforce
> proper usage (some functions act on remote paths only etc.). So I create
> new two types:
> 
> type remote_t = Remote path
> type local_t = Local path
> 
> Then I avoid using type path for greater safety. But I have a problem:
> can't use path manipulation library on these new types. I have created
> "mirror" versions of the library for each new type, but this made my
> source code unmaintainable. What is the right solution?
> 
> I'd prefer not to contaminate the path library with things specific to
> this particular problem, since it's reused.
> 
> Dawid
> 
type 'a path = string list
type remote_t = `Remote path
type local_t = `Local path

val works_on_remote_t : [> `Remote] path -> whatever
val works_on_local_t : [> `Local] path -> whatever
val works_on_any_t : 'a path -> whatever

See the following URL for more details on this trick (called Phantom Types):
http://caml.inria.fr/pub/ml-archives/caml-list/2001/09/081c77179ee2a3787233902a51633122.en.html

E