Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Browse thread
Pattern matching and phantom types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-05-28 (10:44)
From: Matt Armstrong <matt@l...>
Subject: Re: Pattern matching and phantom types
Alain Frisch <frisch@c...> writes:

> On Tue, 28 May 2002, Matt Armstrong wrote:
>
>> type directory
>> type file
>> type absolute
>> type relative
>> type ('a,'b) fname = Fname of string
>>
>> <skip code for the other functions>
>>
>> let fname_to_string = match fname with
>> | (_, directory) Fname(s) -> s ^ "/"
>> | (_, filename) Fname(s) -> s;;
>> ---------------------------------------------------------
>>
>> With the above I get a syntax error. Is it possible to pattern match
>> in this way?
>
> As Rémi said, no. You have to keep in the runtime representation of
> the fname values something to know if they are file or directory.
> For _instance_, you can define:
>
> type ('a,'b) fname = File of string | Directory of string
>
> Another possibility is something like:
>
> type ('a,'b) fname = bool * string
>
> The bool component says whether the value is a directory or a file ...

Yes, that makes sense. I appreciate your two examples.


> The cleaner and easiest solution is probably to define two functions
> string_of_file and string_of_directory ...

When learning ocaml, I'm often concentrating so hard on syntax and new
paradigms that the obvious things are not always so obvious. :-)


-- 
Don't send mail to Carl.Owen@h...
The address is there for spammers to harvest.