caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* object constructor syntax compiler weirdness
@ 2005-11-21  8:09 Mike Lin
  2005-11-21  8:48 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Mike Lin @ 2005-11-21  8:09 UTC (permalink / raw)
  To: caml-list

Hello,

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.


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

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] object constructor syntax compiler weirdness
  2005-11-21  8:09 object constructor syntax compiler weirdness Mike Lin
@ 2005-11-21  8:48 ` Jacques Garrigue
  2005-11-21 10:14   ` Mike Lin
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2005-11-21  8:48 UTC (permalink / raw)
  To: mikelin; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] object constructor syntax compiler weirdness
  2005-11-21  8:48 ` [Caml-list] " Jacques Garrigue
@ 2005-11-21 10:14   ` Mike Lin
  0 siblings, 0 replies; 3+ messages in thread
From: Mike Lin @ 2005-11-21 10:14 UTC (permalink / raw)
  To: caml-list

> 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)

Well, despite the parentheses being useless, I feel this should not be
a syntax error:

class type x = object val y : int end
>> fine
class type x = (object val y : int end)
>> syntax error

since:

module type X = sig val y : int end
>> fine
module type X = (sig val y : int end)
>> fine


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-11-21 10:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-21  8:09 object constructor syntax compiler weirdness Mike Lin
2005-11-21  8:48 ` [Caml-list] " Jacques Garrigue
2005-11-21 10:14   ` Mike Lin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).