caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] question: what is the recommended use case of `val` in class type
@ 2016-07-02 17:23 Hongbo Zhang (BLOOMBERG/ 731 LEX)
  2016-07-03  6:23 ` Jacques Garrigue
  0 siblings, 1 reply; 9+ messages in thread
From: Hongbo Zhang (BLOOMBERG/ 731 LEX) @ 2016-07-02 17:23 UTC (permalink / raw)
  To: nicolas.ojeda.bar; +Cc: caml-list, gabriel.scherer

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

Hi Nicolas, thanks! It matches with my expectation.
I am thinking of overriding such syntax for bucklescript FFI, we can use either

class type t = object val mutable x : int end 
or
class type t = object method x : int method x_set : int -> unit end

Currently I think the former looks better, since the user can tell it is getter or setter without relying on naming convention
From: nicolas.ojeda.bar@lexifi.com At: 07/02/16 13:08:28
To: HONGBO ZHANG (BLOOMBERG/ 731 LEX)
Cc: caml-list@inria.fr, gabriel.scherer@gmail.com
Subject: Re: [Caml-list] question: what is the recommended use case of `val` in class type

Hi Hongbo,

As you observed, `val` can be used via inheritance to expose some private state to subclasses without exposing it to the outside.

Cheers
Nicolas

On Sat, Jul 2, 2016 at 7:02 PM, Hongbo Zhang (BLOOMBERG/ 731 LEX) <hzhang295@bloomberg.net> wrote:

Thanks for your reply. But if `val` is not accessible from outside, why it is the part of class type signature, any reason for this design?

From: gabriel.scherer@gmail.com At: 07/02/16 13:00:34
To: HONGBO ZHANG (BLOOMBERG/ 731 LEX)
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] question: what is the recommended use case of `val` in class type

Objects have some private state, and they expose methods that can be called from the outside. "val" fields correspond to such private state, they are not accessible from outside and are thus not part of an object's type.

You can always expose a value field to the outside through a "getter" method to access it (and a "setter" method to mutate it if relevant), but that is often considered dubious object-oriented style -- it tends to go against good encapsulation.

On Sat, Jul 2, 2016 at 12:45 PM, Hongbo Zhang (BLOOMBERG/ 731 LEX) <hzhang295@bloomberg.net> wrote:

Dear all,
    I have a question about val in class type, is it only useful in inheritance?
    for example
 
    class type text = object val mutable text : string end      
 
    let f (x : text ) = x#text;;                                                                                                                                                               
                      ^                                                                                                                                                                      
Error: This expression has type text                                                                                                                                                         
       It has no method text   
    Thanks -- Hongbo



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

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

* Re: [Caml-list] question: what is the recommended use case of `val` in class type
  2016-07-02 17:23 [Caml-list] question: what is the recommended use case of `val` in class type Hongbo Zhang (BLOOMBERG/ 731 LEX)
@ 2016-07-03  6:23 ` Jacques Garrigue
  2016-07-04 10:19   ` Goswin von Brederlow
  0 siblings, 1 reply; 9+ messages in thread
From: Jacques Garrigue @ 2016-07-03  6:23 UTC (permalink / raw)
  To: Hongbo Zhang; +Cc: nicolas.ojeda.bar, OCaML List Mailing, Gabriel Scherer

Hi Hongo,

You may want to have a look at a small camlp4 syntax extension I wrote a long time ago,
which allows to do what you say.
Using it, one would write:

     val mutable x with accessor

to say that it can be accessed through the x and x_set methods.

The code is here:
http://www.math.nagoya-u.ac.jp/~garrigue/code/ocaml.html

Note that I didn’t try to support functional state changes, i.e. adding a method
    method x_upd x’ = {< x = x’ >}

Jacques

On 2016/07/03 02:23, Hongbo Zhang (BLOOMBERG/ 731 LEX) wrote:
> 
> Hi Nicolas, thanks! It matches with my expectation.
> I am thinking of overriding such syntax for bucklescript FFI, we can use either
> 
> class type t = object val mutable x : int end 
> or
> class type t = object method x : int method x_set : int -> unit end
> 
> Currently I think the former looks better, since the user can tell it is getter or setter without relying on naming convention
> From: nicolas.ojeda.bar@lexifi.com At: 07/02/16 13:08:28
> To: HONGBO ZHANG (BLOOMBERG/ 731 LEX)
> Cc: caml-list@inria.fr, gabriel.scherer@gmail.com
> Subject: Re: [Caml-list] question: what is the recommended use case of `val` in class type
> Hi Hongbo,
> 
> As you observed, `val` can be used via inheritance to expose some private state to subclasses without exposing it to the outside.
> 
> Cheers
> Nicolas
> 
> On Sat, Jul 2, 2016 at 7:02 PM, Hongbo Zhang (BLOOMBERG/ 731 LEX) <hzhang295@bloomberg.net> wrote:
> Thanks for your reply. But if `val` is not accessible from outside, why it is the part of class type signature, any reason for this design?
> 
> From: gabriel.scherer@gmail.com At: 07/02/16 13:00:34
> To: HONGBO ZHANG (BLOOMBERG/ 731 LEX)
> Cc: caml-list@inria.fr
> Subject: Re: [Caml-list] question: what is the recommended use case of `val` in class type
> Objects have some private state, and they expose methods that can be called from the outside. "val" fields correspond to such private state, they are not accessible from outside and are thus not part of an object's type.
> 
> You can always expose a value field to the outside through a "getter" method to access it (and a "setter" method to mutate it if relevant), but that is often considered dubious object-oriented style -- it tends to go against good encapsulation.
> 
> On Sat, Jul 2, 2016 at 12:45 PM, Hongbo Zhang (BLOOMBERG/ 731 LEX) <hzhang295@bloomberg.net> wrote:
> Dear all,
>     I have a question about val in class type, is it only useful in inheritance?
>     for example
>  
>     class type text = object val mutable text : string end      
>  
>     let f (x : text ) = x#text;;                                                                                                                                                               
>                       ^                                                                                                                                                                      
> Error: This expression has type text                                                                                                                                                         
>        It has no method text   
>     Thanks -- Hongbo
> 
> 



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

* Re: [Caml-list] question: what is the recommended use case of `val` in class type
  2016-07-03  6:23 ` Jacques Garrigue
@ 2016-07-04 10:19   ` Goswin von Brederlow
  2016-07-04 21:56     ` Jacques Garrigue
  0 siblings, 1 reply; 9+ messages in thread
From: Goswin von Brederlow @ 2016-07-04 10:19 UTC (permalink / raw)
  To: caml-list

On Sun, Jul 03, 2016 at 03:23:35PM +0900, Jacques Garrigue wrote:
> Hi Hongo,
> 
> You may want to have a look at a small camlp4 syntax extension I wrote a long time ago,
> which allows to do what you say.
> Using it, one would write:
> 
>      val mutable x with accessor
> 
> to say that it can be accessed through the x and x_set methods.
> 
> The code is here:
> http://www.math.nagoya-u.ac.jp/~garrigue/code/ocaml.html
> 
> Note that I didn’t try to support functional state changes, i.e. adding a method
>     method x_upd x’ = {< x = x’ >}
> 
> Jacques

What's the point of x then? Wouldn't it make more sense to only expose
the accessors so even derived classes will go through them instead of
accessing x directly?

Question: Can ppx attributes generate the accessors but also hide the
"val mutable x" when no mli file is present?

MfG
	Goswin

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

* Re: [Caml-list] question: what is the recommended use case of `val` in class type
  2016-07-04 10:19   ` Goswin von Brederlow
@ 2016-07-04 21:56     ` Jacques Garrigue
  0 siblings, 0 replies; 9+ messages in thread
From: Jacques Garrigue @ 2016-07-04 21:56 UTC (permalink / raw)
  To: Goswin von Brederlow; +Cc: OCaML List Mailing

On 2016/07/04 19:19, Goswin von Brederlow wrote:
> 
> On Sun, Jul 03, 2016 at 03:23:35PM +0900, Jacques Garrigue wrote:
>> Hi Hongo,
>> 
>> You may want to have a look at a small camlp4 syntax extension I wrote a long time ago,
>> which allows to do what you say.
>> Using it, one would write:
>> 
>>     val mutable x with accessor
>> 
>> to say that it can be accessed through the x and x_set methods.
>> 
>> The code is here:
>> http://www.math.nagoya-u.ac.jp/~garrigue/code/ocaml.html
>> 
>> Note that I didn’t try to support functional state changes, i.e. adding a method
>>    method x_upd x’ = {< x = x’ >}
>> 
>> Jacques
> 
> What's the point of x then? Wouldn't it make more sense to only expose
> the accessors so even derived classes will go through them instead of
> accessing x directly?

Maybe, if OCaml were an object-oriented language.
But the accessor approach doesn’t mix too well with multiple inheritance, whereas
ocaml instance variables do.

> Question: Can ppx attributes generate the accessors but also hide the
> "val mutable x" when no mli file is present?


Not so easy to do, because you need to write an explicit class interface to hide
the instance variable, and for this you need to write the type of the methods x and x_set.
Here is some code that does it, and could be translated into an extension:

class c (x : int) = object
   inherit (fun x -> object val mutable x = x method x = x method x_set y = x <- y end :  'a -> object method x : 'a method x_set : 'a -> unit end) x
end;;

One could generate the inherit line.

Jacques

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

* Re: [Caml-list] question: what is the recommended use case of `val` in class type
@ 2016-07-05 13:00 Hongbo Zhang (BLOOMBERG/ 731 LEX)
  0 siblings, 0 replies; 9+ messages in thread
From: Hongbo Zhang (BLOOMBERG/ 731 LEX) @ 2016-07-05 13:00 UTC (permalink / raw)
  To: garrigue; +Cc: caml-list, gabriel.scherer, nicolas.ojeda.bar

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

Hi Jacques, thanks for your extension. I ended up using attributes for FFI, like

   method height : int [@@set]

  and people can use `x##height#= 30`, the advantage is that people don't need remember the name mangling, the nice thing is that we generate signature like this(simplified)
   
   method height : int 
   method height#= int -> unit

  This even works with merlin auto-compiletion : )
From: garrigue@math.nagoya-u.ac.jp At: 07/03/16 02:24:25
To: HONGBO ZHANG (BLOOMBERG/ 731 LEX)
Cc: caml-list@inria.fr, gabriel.scherer@gmail.com, nicolas.ojeda.bar@lexifi.com
Subject: Re: [Caml-list] question: what is the recommended use case of `val` in class type

Hi Hongo,

You may want to have a look at a small camlp4 syntax extension I wrote a long time ago,
which allows to do what you say.
Using it, one would write:

     val mutable x with accessor

to say that it can be accessed through the x and x_set methods.

The code is here:
http://www.math.nagoya-u.ac.jp/~garrigue/code/ocaml.html

Note that I didn’t try to support functional state changes, i.e. adding a method
    method x_upd x’ = {< x = x’ >}

Jacques

On 2016/07/03 02:23, Hongbo Zhang (BLOOMBERG/ 731 LEX) wrote:
> 
> Hi Nicolas, thanks! It matches with my expectation.
> I am thinking of overriding such syntax for bucklescript FFI, we can use either
> 
> class type t = object val mutable x : int end 
> or
> class type t = object method x : int method x_set : int -> unit end
> 
> Currently I think the former looks better, since the user can tell it is getter or setter without relying on naming convention
> From: nicolas.ojeda.bar@lexifi.com At: 07/02/16 13:08:28
> To: HONGBO ZHANG (BLOOMBERG/ 731 LEX)
> Cc: caml-list@inria.fr, gabriel.scherer@gmail.com
> Subject: Re: [Caml-list] question: what is the recommended use case of `val` in class type
> Hi Hongbo,
> 
> As you observed, `val` can be used via inheritance to expose some private state to subclasses without exposing it to the outside.
> 
> Cheers
> Nicolas
> 
> On Sat, Jul 2, 2016 at 7:02 PM, Hongbo Zhang (BLOOMBERG/ 731 LEX) <hzhang295@bloomberg.net> wrote:
> Thanks for your reply. But if `val` is not accessible from outside, why it is the part of class type signature, any reason for this design?
> 
> From: gabriel.scherer@gmail.com At: 07/02/16 13:00:34
> To: HONGBO ZHANG (BLOOMBERG/ 731 LEX)
> Cc: caml-list@inria.fr
> Subject: Re: [Caml-list] question: what is the recommended use case of `val` in class type
> Objects have some private state, and they expose methods that can be called from the outside. "val" fields correspond to such private state, they are not accessible from outside and are thus not part of an object's type.
> 
> You can always expose a value field to the outside through a "getter" method to access it (and a "setter" method to mutate it if relevant), but that is often considered dubious object-oriented style -- it tends to go against good encapsulation.
> 
> On Sat, Jul 2, 2016 at 12:45 PM, Hongbo Zhang (BLOOMBERG/ 731 LEX) <hzhang295@bloomberg.net> wrote:
> Dear all,
>     I have a question about val in class type, is it only useful in inheritance?
>     for example
>  
>     class type text = object val mutable text : string end      
>  
>     let f (x : text ) = x#text;;                                                                                                                                                               
>                       ^                                                                                                                                                                      
> Error: This expression has type text                                                                                                                                                         
>        It has no method text   
>     Thanks -- Hongbo
> 
> 


-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


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

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

* Re: [Caml-list] question: what is the recommended use case of `val` in class type
  2016-07-02 17:02 Hongbo Zhang (BLOOMBERG/ 731 LEX)
@ 2016-07-02 17:07 ` Nicolas Ojeda Bar
  0 siblings, 0 replies; 9+ messages in thread
From: Nicolas Ojeda Bar @ 2016-07-02 17:07 UTC (permalink / raw)
  To: Hongbo Zhang; +Cc: gabriel.scherer, caml-list

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

Hi Hongbo,

As you observed, `val` can be used via inheritance to expose some private
state to subclasses without exposing it to the outside.

Cheers
Nicolas

On Sat, Jul 2, 2016 at 7:02 PM, Hongbo Zhang (BLOOMBERG/ 731 LEX) <
hzhang295@bloomberg.net> wrote:

> Thanks for your reply. But if `val` is not accessible from outside, why it
> is the part of class type signature, any reason for this design?
>
> From: gabriel.scherer@gmail.com At: 07/02/16 13:00:34
> To: HONGBO ZHANG (BLOOMBERG/ 731 LEX) <hzhang295@bloomberg.net>
> Cc: caml-list@inria.fr
> Subject: Re: [Caml-list] question: what is the recommended use case of
> `val` in class type
>
> Objects have some private state, and they expose methods that can be
> called from the outside. "val" fields correspond to such private state,
> they are not accessible from outside and are thus not part of an object's
> type.
>
> You can always expose a value field to the outside through a "getter"
> method to access it (and a "setter" method to mutate it if relevant), but
> that is often considered dubious object-oriented style -- it tends to go
> against good encapsulation.
>
> On Sat, Jul 2, 2016 at 12:45 PM, Hongbo Zhang (BLOOMBERG/ 731 LEX) <
> hzhang295@bloomberg.net> wrote:
>
>> Dear all,
>> I have a question about val in class type, is it only useful in
>> inheritance?
>> for example
>> class type text = object val mutable text : string end
>> let f (x : text ) = x#text
>> <#m_1963343833435410216_m_-4902517353790339526_text>;;
>> ^
>> Error: This expression has type text
>> It has no method text
>> Thanks -- Hongbo
>>
>
>

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

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

* Re: [Caml-list] question: what is the recommended use case of `val` in class type
@ 2016-07-02 17:02 Hongbo Zhang (BLOOMBERG/ 731 LEX)
  2016-07-02 17:07 ` Nicolas Ojeda Bar
  0 siblings, 1 reply; 9+ messages in thread
From: Hongbo Zhang (BLOOMBERG/ 731 LEX) @ 2016-07-02 17:02 UTC (permalink / raw)
  To: gabriel.scherer; +Cc: caml-list

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

Thanks for your reply. But if `val` is not accessible from outside, why it is the part of class type signature, any reason for this design?

From: gabriel.scherer@gmail.com At: 07/02/16 13:00:34
To: HONGBO ZHANG (BLOOMBERG/ 731 LEX)
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] question: what is the recommended use case of `val` in class type

Objects have some private state, and they expose methods that can be called from the outside. "val" fields correspond to such private state, they are not accessible from outside and are thus not part of an object's type.

You can always expose a value field to the outside through a "getter" method to access it (and a "setter" method to mutate it if relevant), but that is often considered dubious object-oriented style -- it tends to go against good encapsulation.

On Sat, Jul 2, 2016 at 12:45 PM, Hongbo Zhang (BLOOMBERG/ 731 LEX) <hzhang295@bloomberg.net> wrote:

Dear all,
    I have a question about val in class type, is it only useful in inheritance?
    for example
  
    class type text = object val mutable text : string end      
  
    let f (x : text ) = x#text;;                                                                                                                                                               
                      ^                                                                                                                                                                      
Error: This expression has type text                                                                                                                                                         
       It has no method text   
    Thanks -- Hongbo



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

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

* Re: [Caml-list] question: what is the recommended use case of `val` in class type
  2016-07-02 16:45 Hongbo Zhang (BLOOMBERG/ 731 LEX)
@ 2016-07-02 16:59 ` Gabriel Scherer
  0 siblings, 0 replies; 9+ messages in thread
From: Gabriel Scherer @ 2016-07-02 16:59 UTC (permalink / raw)
  To: Hongbo Zhang; +Cc: caml users

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

Objects have some private state, and they expose methods that can be called
from the outside. "val" fields correspond to such private state, they are
not accessible from outside and are thus not part of an object's type.

You can always expose a value field to the outside through a "getter"
method to access it (and a "setter" method to mutate it if relevant), but
that is often considered dubious object-oriented style -- it tends to go
against good encapsulation.

On Sat, Jul 2, 2016 at 12:45 PM, Hongbo Zhang (BLOOMBERG/ 731 LEX) <
hzhang295@bloomberg.net> wrote:

> Dear all,
> I have a question about val in class type, is it only useful in
> inheritance?
> for example
> class type text = object val mutable text : string end
> let f (x : text ) = x#text <#m_-4902517353790339526_text>;;
> ^
> Error: This expression has type text
> It has no method text
> Thanks -- Hongbo
>

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

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

* [Caml-list] question: what is the recommended use case of `val` in class type
@ 2016-07-02 16:45 Hongbo Zhang (BLOOMBERG/ 731 LEX)
  2016-07-02 16:59 ` Gabriel Scherer
  0 siblings, 1 reply; 9+ messages in thread
From: Hongbo Zhang (BLOOMBERG/ 731 LEX) @ 2016-07-02 16:45 UTC (permalink / raw)
  To: caml-list

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

Dear all,
    I have a question about val in class type, is it only useful in inheritance?
    for example
   
    class type text = object val mutable text : string end      
   
    let f (x : text ) = x#text;;                                                                                                                                                               
                      ^                                                                                                                                                                      
Error: This expression has type text                                                                                                                                                         
       It has no method text   
    Thanks -- Hongbo

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

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

end of thread, other threads:[~2016-07-05 13:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-02 17:23 [Caml-list] question: what is the recommended use case of `val` in class type Hongbo Zhang (BLOOMBERG/ 731 LEX)
2016-07-03  6:23 ` Jacques Garrigue
2016-07-04 10:19   ` Goswin von Brederlow
2016-07-04 21:56     ` Jacques Garrigue
  -- strict thread matches above, loose matches on Subject: below --
2016-07-05 13:00 Hongbo Zhang (BLOOMBERG/ 731 LEX)
2016-07-02 17:02 Hongbo Zhang (BLOOMBERG/ 731 LEX)
2016-07-02 17:07 ` Nicolas Ojeda Bar
2016-07-02 16:45 Hongbo Zhang (BLOOMBERG/ 731 LEX)
2016-07-02 16:59 ` Gabriel Scherer

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