Version française
Home     About     Download     Resources     Contact us    
Browse thread
object constructor syntax compiler weirdness
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] object constructor syntax compiler weirdness
From: Mike Lin <mikelin@mit.edu>

> The following are by no means common or advisable syntax, but I was
> curious if there is a rationale behind why certain things are syntax
> errors or not. I'm using 3.08.4 -- apologies if this was fixed or
> modified in 3.09.

I have no rationale, but at least I can explain.

> class circle radius =
>   let pi = 3.14159 in
>   object
>    method area = pi *. radius *.radius
>   end
> 
> >> fine
> 
> class circle2 radius =
>   let pi = 3.14159 in
>     print_endline "Creating a circle";
>     object
>      method area = pi *. radius *.radius
>     end
> 
> >> syntax error

The things you are able to write in the prefix of a class constructor
are more limited than in normal functions.
In the above case, I would say that it would be a bit confusing.
You can of course write it
  let () = print_endline "Creating a circle" in object ... end

Another thing you cannot write is

  class ... = let module M = ... in object ... end.

In that cases there are difficulties related to mixing module scope
with class definitions (which have there own notion of scope), making
it rather difficult to introduce it.

> Most curiously:
> 
> class circle3 radius =
>   let pi = 3.14159 in
>     (object
>       method area = pi *. radius *.radius
>      end)
> 
> >> fine
> 
> class circle4 radius =
>   let pi = 3.14159 in
>     begin
>      object
>       method area = pi *. radius *.radius
>      end
>     end
> 
> >> syntax error

Parentheses in class definitions are useless, so there is no reason to
add a begin .. end construct. I suppose that the first syntax is only
allowed as a degenerated case of
   class c = (object ... end : ct)

Jacques Garrigue