Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Caml-list] private object constructor?
@ 2017-02-13 18:23 Steffen Smolka
  2017-02-13 18:57 ` Ivan Gotovchits
  0 siblings, 1 reply; 3+ messages in thread
From: Steffen Smolka @ 2017-02-13 18:23 UTC (permalink / raw)
  To: Caml List

[-- Attachment #1: Type: text/plain, Size: 285 bytes --]

Hi,

Is it possible to expose an object or class type in OCaml without exposing
the constructor, i.e. the ability to construct objects of the type? OCaml
supports private ADTs and type synonyms, but the private keyword doesn't
seem to work with classes or objects.

Thanks,
-- Steffen

[-- Attachment #2: Type: text/html, Size: 369 bytes --]

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

* Re: [Caml-list] private object constructor?
  2017-02-13 18:23 [Caml-list] private object constructor? Steffen Smolka
@ 2017-02-13 18:57 ` Ivan Gotovchits
  2017-02-13 19:11   ` Steffen Smolka
  0 siblings, 1 reply; 3+ messages in thread
From: Ivan Gotovchits @ 2017-02-13 18:57 UTC (permalink / raw)
  To: Steffen Smolka; +Cc: Caml List

[-- Attachment #1: Type: text/plain, Size: 975 bytes --]

Hi Steffen,

Your question contains the answer :) You can indeed expose an object type
and a class type without exposing the class.

module Example : sig
  class type t = object
      method print : unit
    end
end = struct
    class t = object
      method print = print_endline "yep"
    end
  end

The class implementation will not be exposed, so `new Example.t` will say
`Unbound class Example.t`. But, the class type and object type will be
exposed,
e.g, you may say `type printable = Example.t` (yes, class type declaration
declares both the type and the class type).

Regards,
Ivan


On Mon, Feb 13, 2017 at 1:23 PM, Steffen Smolka <smolka@cs.cornell.edu>
wrote:

> Hi,
>
> Is it possible to expose an object or class type in OCaml without exposing
> the constructor, i.e. the ability to construct objects of the type? OCaml
> supports private ADTs and type synonyms, but the private keyword doesn't
> seem to work with classes or objects.
>
> Thanks,
> -- Steffen
>

[-- Attachment #2: Type: text/html, Size: 1667 bytes --]

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

* Re: [Caml-list] private object constructor?
  2017-02-13 18:57 ` Ivan Gotovchits
@ 2017-02-13 19:11   ` Steffen Smolka
  0 siblings, 0 replies; 3+ messages in thread
From: Steffen Smolka @ 2017-02-13 19:11 UTC (permalink / raw)
  To: Ivan Gotovchits; +Cc: Caml List

[-- Attachment #1: Type: text/plain, Size: 1145 bytes --]

Thanks, this works :)

-- Steffen

On Mon, Feb 13, 2017 at 1:57 PM, Ivan Gotovchits <ivg@ieee.org> wrote:

> Hi Steffen,
>
> Your question contains the answer :) You can indeed expose an object type
> and a class type without exposing the class.
>
> module Example : sig
>   class type t = object
>       method print : unit
>     end
> end = struct
>     class t = object
>       method print = print_endline "yep"
>     end
>   end
>
> The class implementation will not be exposed, so `new Example.t` will say
> `Unbound class Example.t`. But, the class type and object type will be
> exposed,
> e.g, you may say `type printable = Example.t` (yes, class type declaration
> declares both the type and the class type).
>
> Regards,
> Ivan
>
>
> On Mon, Feb 13, 2017 at 1:23 PM, Steffen Smolka <smolka@cs.cornell.edu>
> wrote:
>
>> Hi,
>>
>> Is it possible to expose an object or class type in OCaml without
>> exposing the constructor, i.e. the ability to construct objects of the
>> type? OCaml supports private ADTs and type synonyms, but the private
>> keyword doesn't seem to work with classes or objects.
>>
>> Thanks,
>> -- Steffen
>>
>
>

[-- Attachment #2: Type: text/html, Size: 2221 bytes --]

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

end of thread, other threads:[~2017-02-13 19:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-13 18:23 [Caml-list] private object constructor? Steffen Smolka
2017-02-13 18:57 ` Ivan Gotovchits
2017-02-13 19:11   ` Steffen Smolka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox