Version franēaise
Home     About     Download     Resources     Contact us    
Browse thread
Compiler feature - useful or not?
[ 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] Compiler feature - useful or not?
Martin Jambon wrote:
> On Fri, 16 Nov 2007, Edgar Friendly wrote:
> 
>> Martin Jambon wrote:
>>> Please don't take my suggestions too seriously, but it could be cool to
>>> define types such as:
>>>
>>> type bit = [ 0 | 1 ]
>>>
>>>
>>> Martin
>>
>> How about this:
>>
>> type permission =
>>  User_read -> 0o400 | User_write -> 0o200 | User_execute -> 0o100
>> | Group_read -> 0o40 | Group_write -> 0o20 | Group_execute -> 0o10
>> | World_read ->  0o4 | World_write ->  0o2 | World_execute ->  0o1
>>
>> let combine (pl: permission list) =
>>     List.fold_left (fun a b -> a lor (b :> int)) 0 pl
> 
> I'm afraid of losing the following basic feature ("your first OCaml
> program"):
> 
> # 0;;
> - : int
> 
> and instead get:
> 
> # 0;;
> - : [< int > 0 ]
> 
> Perhaps polymorphism should not be the default, i.e. we would have to
> use a special keyword:
> 
> # poly 0;;
> - : [< int > 0 ]
> # 0;;
> - : int
> 
Explicit casts, my friend.  Explicit casts to convert from int to
permission and back.  And automatically generated runtime checks to
ensure that you don't try to convert ( 37 :> permission ).  1 remains an
int, and if you want World_execute (or true or anything else whose
runtime representation is 1), a checked cast becomes necessary.

E.