caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] List of structurally typed objects
@ 2017-02-22 17:01 Richard W.M. Jones
  2017-02-22 17:09 ` Gabriel Scherer
  2017-02-22 17:28 ` Damien Guichard
  0 siblings, 2 replies; 14+ messages in thread
From: Richard W.M. Jones @ 2017-02-22 17:01 UTC (permalink / raw)
  To: caml-list

Is it possible to construct a list of structurally typed ("duck
typed") objects?

The code below seems as if superficially it should work, at least as
far as I understand the OCaml memory model and how objects work
internally.  However the implementation doesn't match the interface.

------ list_fns.mli ----------------------------
type 'a obj = < hello : string; .. > as 'a
val register_obj : string -> 'a obj -> unit
val get_obj : string -> 'a obj
------------------------------------------------

------ list_fns.ml -----------------------------
type 'a obj = < hello : string; .. > as 'a
let objs = ref []
let register_obj name obj = objs := (name, obj) :: !objs
let get_obj name = List.assoc name !objs
------------------------------------------------

$ ocamlc -c list_fns.mli
$ ocamlc -c list_fns.ml
File "list_fns.ml", line 1:
Error: The implementation list_fns.ml
       does not match the interface list_fns.cmi:
       Values do not match:
         val register_obj : string -> '_a -> unit
       is not included in
         val register_obj : string -> < hello : string; .. > obj -> unit
       File "list_fns.ml", line 3, characters 4-16: Actual declaration

I'm guessing that "constraint" should be used here, but how?

Rich.

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

* Re: [Caml-list] List of structurally typed objects
  2017-02-22 17:01 [Caml-list] List of structurally typed objects Richard W.M. Jones
@ 2017-02-22 17:09 ` Gabriel Scherer
  2017-02-22 17:36   ` Richard W.M. Jones
  2017-02-22 17:28 ` Damien Guichard
  1 sibling, 1 reply; 14+ messages in thread
From: Gabriel Scherer @ 2017-02-22 17:09 UTC (permalink / raw)
  To: Richard W.M. Jones; +Cc: caml users

The signature you demand is actually incorrect, because the return
type of get_obj is to permissive: it claims that it can return *any*
('a obj), so you could register an object with just the hello method
and get back an object with more methods.

On the other hand, if you use the monomorphic < hello : string > type
in the interface, then it is type-correct and the implementation you
provide is accepted. It is explicit in the fact that you can't recover
the extra methods of the object once they have been stored in this
lowest-common-denominator container.

On Wed, Feb 22, 2017 at 12:01 PM, Richard W.M. Jones <rich@annexia.org> wrote:
> Is it possible to construct a list of structurally typed ("duck
> typed") objects?
>
> The code below seems as if superficially it should work, at least as
> far as I understand the OCaml memory model and how objects work
> internally.  However the implementation doesn't match the interface.
>
> ------ list_fns.mli ----------------------------
> type 'a obj = < hello : string; .. > as 'a
> val register_obj : string -> 'a obj -> unit
> val get_obj : string -> 'a obj
> ------------------------------------------------
>
> ------ list_fns.ml -----------------------------
> type 'a obj = < hello : string; .. > as 'a
> let objs = ref []
> let register_obj name obj = objs := (name, obj) :: !objs
> let get_obj name = List.assoc name !objs
> ------------------------------------------------
>
> $ ocamlc -c list_fns.mli
> $ ocamlc -c list_fns.ml
> File "list_fns.ml", line 1:
> Error: The implementation list_fns.ml
>        does not match the interface list_fns.cmi:
>        Values do not match:
>          val register_obj : string -> '_a -> unit
>        is not included in
>          val register_obj : string -> < hello : string; .. > obj -> unit
>        File "list_fns.ml", line 3, characters 4-16: Actual declaration
>
> I'm guessing that "constraint" should be used here, but how?
>
> Rich.
>
> --
> 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

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

* Re: [Caml-list] List of structurally typed objects
  2017-02-22 17:01 [Caml-list] List of structurally typed objects Richard W.M. Jones
  2017-02-22 17:09 ` Gabriel Scherer
@ 2017-02-22 17:28 ` Damien Guichard
  2017-02-22 17:38   ` Richard W.M. Jones
  1 sibling, 1 reply; 14+ messages in thread
From: Damien Guichard @ 2017-02-22 17:28 UTC (permalink / raw)
  To: caml-list


Hi Richard.

Is this the code you want ?

type 'a obj_list = 'a list
constraint 'a = < hello : string; .. > as 'a

hope it helps,

Damien Guichard (SpiceGuid)

Richard W.M. Jones a écrit :
> Is it possible to construct a list of structurally typed ("duck
> typed") objects?
>
> The code below seems as if superficially it should work, at least as
> far as I understand the OCaml memory model and how objects work
> internally.  However the implementation doesn't match the interface.
>
> ------ list_fns.mli ----------------------------
> type 'a obj = < hello : string; .. > as 'a
> val register_obj : string -> 'a obj -> unit
> val get_obj : string -> 'a obj
> ------------------------------------------------
>
> ------ list_fns.ml -----------------------------
> type 'a obj = < hello : string; .. > as 'a
> let objs = ref []
> let register_obj name obj = objs := (name, obj) :: !objs
> let get_obj name = List.assoc name !objs
> ------------------------------------------------
>
> $ ocamlc -c list_fns.mli
> $ ocamlc -c list_fns.ml
> File "list_fns.ml", line 1:
> Error: The implementation list_fns.ml
>         does not match the interface list_fns.cmi:
>         Values do not match:
>           val register_obj : string -> '_a -> unit
>         is not included in
>           val register_obj : string -> < hello : string; .. > obj -> unit
>         File "list_fns.ml", line 3, characters 4-16: Actual declaration
>
> I'm guessing that "constraint" should be used here, but how?
>
> Rich.
>


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

* Re: [Caml-list] List of structurally typed objects
  2017-02-22 17:09 ` Gabriel Scherer
@ 2017-02-22 17:36   ` Richard W.M. Jones
  2017-02-22 17:43     ` Nicolás Ojeda Bär
  0 siblings, 1 reply; 14+ messages in thread
From: Richard W.M. Jones @ 2017-02-22 17:36 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: caml users

On Wed, Feb 22, 2017 at 12:09:37PM -0500, Gabriel Scherer wrote:
> The signature you demand is actually incorrect, because the return
> type of get_obj is to permissive: it claims that it can return *any*
> ('a obj), so you could register an object with just the hello method
> and get back an object with more methods.
> 
> On the other hand, if you use the monomorphic < hello : string > type
> in the interface, then it is type-correct and the implementation you
> provide is accepted. It is explicit in the fact that you can't recover
> the extra methods of the object once they have been stored in this
> lowest-common-denominator container.

Ah, I see.

Modifying the code to use the monomorphic type works, but I have to
downcast the objects to the right type.  For reference, the working
code is below.

Thanks,

Rich.

list_fns.mli --------------------------------------------------------
type obj = < hello : string >
val register_obj : string -> obj -> unit
val get_obj : string -> obj

list_fns.ml ---------------------------------------------------------
type obj = < hello : string >
let objs = ref []
let register_obj name obj = objs := (name, obj) :: !objs
let get_obj name = List.assoc name !objs

test.ml -------------------------------------------------------------
class foo = object
  method hello = "hello from foo"
  method goodbye () = print_endline "goodbye"
end

class bar = object
  method hello = "hello from bar"
end

let () =
  let o1 = new foo in
  let o2 = new bar in
  List_fns.register_obj "o1" (o1 :> List_fns.obj);
  List_fns.register_obj "o2" (o2 :> List_fns.obj);
  print_endline ("calling o1: " ^ (List_fns.get_obj "o1")#hello);
  print_endline ("calling o2: " ^ (List_fns.get_obj "o2")#hello)
--------------------------------------------------------------------

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

* Re: [Caml-list] List of structurally typed objects
  2017-02-22 17:28 ` Damien Guichard
@ 2017-02-22 17:38   ` Richard W.M. Jones
  2017-02-22 18:08     ` Ivan Gotovchits
  0 siblings, 1 reply; 14+ messages in thread
From: Richard W.M. Jones @ 2017-02-22 17:38 UTC (permalink / raw)
  To: Damien Guichard; +Cc: caml-list

On Wed, Feb 22, 2017 at 06:28:51PM +0100, Damien Guichard wrote:
> 
> Hi Richard.
> 
> Is this the code you want ?
> 
> type 'a obj_list = 'a list
> constraint 'a = < hello : string; .. > as 'a

Perhaps.  Is it possible to write the list_fns.mli interface
using this?

Rich.


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

* Re: [Caml-list] List of structurally typed objects
  2017-02-22 17:36   ` Richard W.M. Jones
@ 2017-02-22 17:43     ` Nicolás Ojeda Bär
  2017-02-22 17:53       ` Richard W.M. Jones
  0 siblings, 1 reply; 14+ messages in thread
From: Nicolás Ojeda Bär @ 2017-02-22 17:43 UTC (permalink / raw)
  To: Richard W.M. Jones; +Cc: Gabriel Scherer, caml users

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

Hi Richard,

Note that you can still write < hello: string; .. > in the signature of
register_obj and do the cast before adding your object to the list:

  let register_obj name obj = objs := (name, (obj :> < hello: string >)) ::
!objs

This way you do not need to downcast at each call-site of register_obj.

Cheers,
Nicolas



On Wed, Feb 22, 2017 at 6:36 PM, Richard W.M. Jones <rich@annexia.org>
wrote:

> On Wed, Feb 22, 2017 at 12:09:37PM -0500, Gabriel Scherer wrote:
> > The signature you demand is actually incorrect, because the return
> > type of get_obj is to permissive: it claims that it can return *any*
> > ('a obj), so you could register an object with just the hello method
> > and get back an object with more methods.
> >
> > On the other hand, if you use the monomorphic < hello : string > type
> > in the interface, then it is type-correct and the implementation you
> > provide is accepted. It is explicit in the fact that you can't recover
> > the extra methods of the object once they have been stored in this
> > lowest-common-denominator container.
>
> Ah, I see.
>
> Modifying the code to use the monomorphic type works, but I have to
> downcast the objects to the right type.  For reference, the working
> code is below.
>
> Thanks,
>
> Rich.
>
> list_fns.mli --------------------------------------------------------
> type obj = < hello : string >
> val register_obj : string -> obj -> unit
> val get_obj : string -> obj
>
> list_fns.ml ---------------------------------------------------------
> type obj = < hello : string >
> let objs = ref []
> let register_obj name obj = objs := (name, obj) :: !objs
> let get_obj name = List.assoc name !objs
>
> test.ml -------------------------------------------------------------
> class foo = object
>   method hello = "hello from foo"
>   method goodbye () = print_endline "goodbye"
> end
>
> class bar = object
>   method hello = "hello from bar"
> end
>
> let () =
>   let o1 = new foo in
>   let o2 = new bar in
>   List_fns.register_obj "o1" (o1 :> List_fns.obj);
>   List_fns.register_obj "o2" (o2 :> List_fns.obj);
>   print_endline ("calling o1: " ^ (List_fns.get_obj "o1")#hello);
>   print_endline ("calling o2: " ^ (List_fns.get_obj "o2")#hello)
> --------------------------------------------------------------------
>
> --
> 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: 3872 bytes --]

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

* Re: [Caml-list] List of structurally typed objects
  2017-02-22 17:43     ` Nicolás Ojeda Bär
@ 2017-02-22 17:53       ` Richard W.M. Jones
  0 siblings, 0 replies; 14+ messages in thread
From: Richard W.M. Jones @ 2017-02-22 17:53 UTC (permalink / raw)
  To: Nicolás Ojeda Bär; +Cc: Gabriel Scherer, caml users

On Wed, Feb 22, 2017 at 06:43:06PM +0100, Nicolás Ojeda Bär wrote:
> Hi Richard,
> 
> Note that you can still write < hello: string; .. > in the signature of
> register_obj and do the cast before adding your object to the list:
> 
>   let register_obj name obj = objs := (name, (obj :> < hello: string >)) ::
> !objs
> 
> This way you do not need to downcast at each call-site of register_obj.

Indeed, that will make my code simpler too.

Thanks,
Rich.

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

* Re: [Caml-list] List of structurally typed objects
  2017-02-22 17:38   ` Richard W.M. Jones
@ 2017-02-22 18:08     ` Ivan Gotovchits
  2017-02-22 19:04       ` Mikhail Mandrykin
  0 siblings, 1 reply; 14+ messages in thread
From: Ivan Gotovchits @ 2017-02-22 18:08 UTC (permalink / raw)
  To: Richard W.M. Jones; +Cc: Damien Guichard, caml-list

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

It is possible to generalize this approach using private row types.

list_fns.mli:

module Registry(Obj : sig
    type t = private < .. >
  end) : sig
  val register_obj : string -> Obj.t -> unit
  val get_obj : string -> Obj.t
end


list_fns.ml:

module Registry(Obj : sig
    type t = private < .. >
  end) = struct

  let objs = ref []
  let register_obj name obj = objs := (name, obj) :: !objs
  let get_obj name = List.assoc name !objs
end


test.ml:

class foo = object
  method hello = "hello from foo"
  method goodbye () = print_endline "goodbye"
end

class bar = object
  method hello = "hello from bar"
end

module Bars = List_fns.Registry(struct type t = bar end)

let () =
  let o1 = new foo in
  let o2 = new bar in
  Bars.register_obj "o1" (o1 :> bar);
  Bars.register_obj "o2" (o2 );
  print_endline ("calling o1: " ^ (Bars.get_obj "o1")#hello);
  print_endline ("calling o2: " ^ (Bars.get_obj "o2")#hello)




The List_fns.Registry functor will create a monomorphic registry for the
specified base type.


Probably, this may help you :)

Best wishes,
Ivan Gotovchits

On Wed, Feb 22, 2017 at 12:38 PM, Richard W.M. Jones <rich@annexia.org>
wrote:

> On Wed, Feb 22, 2017 at 06:28:51PM +0100, Damien Guichard wrote:
> >
> > Hi Richard.
> >
> > Is this the code you want ?
> >
> > type 'a obj_list = 'a list
> > constraint 'a = < hello : string; .. > as 'a
>
> Perhaps.  Is it possible to write the list_fns.mli interface
> using this?
>
> Rich.
>
>
> --
> 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: 3765 bytes --]

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

* Re: [Caml-list] List of structurally typed objects
  2017-02-22 18:08     ` Ivan Gotovchits
@ 2017-02-22 19:04       ` Mikhail Mandrykin
  2017-02-22 19:21         ` [Caml-list] Warning for unused variables Aymeric Fromherz
  0 siblings, 1 reply; 14+ messages in thread
From: Mikhail Mandrykin @ 2017-02-22 19:04 UTC (permalink / raw)
  To: caml-list, Ivan Gotovchits; +Cc: Richard W.M. Jones, Damien Guichard

On среда, 22 февраля 2017 г. 13:08:23 MSK Ivan Gotovchits wrote:
> It is possible to generalize this approach using private row types.

If at least a limited support for dynamic object typing is needed, it's also 
possible to construct a heterogeneous container of objects using several 
advanced features like first-class modules and GADT, but even in this case the 
exact type of the object should match precisely when extracted from the list 
or at least a finite (and small) number of possibilities for that type should 
be known. It's, however, still possible to upcast the object to some supertype 
upon addition and later extract and use it as the object of that supertype. An 
example: https://github.com/schrodibear/ocaml_tricks/blob/master/object_hlist/
obj_list.ml

Mikhail

> 
> list_fns.mli:
> 
> module Registry(Obj : sig
>     type t = private < .. >
>   end) : sig
>   val register_obj : string -> Obj.t -> unit
>   val get_obj : string -> Obj.t
> end
> 
> 
> list_fns.ml:
> 
> module Registry(Obj : sig
>     type t = private < .. >
>   end) = struct
> 
>   let objs = ref []
>   let register_obj name obj = objs := (name, obj) :: !objs
>   let get_obj name = List.assoc name !objs
> end
> 
> 
> test.ml:
> 
> class foo = object
>   method hello = "hello from foo"
>   method goodbye () = print_endline "goodbye"
> end
> 
> class bar = object
>   method hello = "hello from bar"
> end
> 
> module Bars = List_fns.Registry(struct type t = bar end)
> 
> let () =
>   let o1 = new foo in
>   let o2 = new bar in
>   Bars.register_obj "o1" (o1 :> bar);
>   Bars.register_obj "o2" (o2 );
>   print_endline ("calling o1: " ^ (Bars.get_obj "o1")#hello);
>   print_endline ("calling o2: " ^ (Bars.get_obj "o2")#hello)
> 
> 
> 
> 
> The List_fns.Registry functor will create a monomorphic registry for the
> specified base type.
> 
> 
> Probably, this may help you :)
> 
> Best wishes,
> Ivan Gotovchits
> 
> On Wed, Feb 22, 2017 at 12:38 PM, Richard W.M. Jones <rich@annexia.org>
> 
> wrote:
> > On Wed, Feb 22, 2017 at 06:28:51PM +0100, Damien Guichard wrote:
> > > Hi Richard.
> > > 
> > > Is this the code you want ?
> > > 
> > > type 'a obj_list = 'a list
> > > constraint 'a = < hello : string; .. > as 'a
> > 
> > Perhaps.  Is it possible to write the list_fns.mli interface
> > using this?
> > 
> > Rich.
> > 
> > 
> > --
> > 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


-- 
Mikhail Mandrykin
Linux Verification Center, ISPRAS
web: http://linuxtesting.org
e-mail: mandrykin@ispras.ru

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

* [Caml-list] Warning for unused variables
  2017-02-22 19:04       ` Mikhail Mandrykin
@ 2017-02-22 19:21         ` Aymeric Fromherz
  2017-02-22 19:43           ` Gabriel Scherer
  0 siblings, 1 reply; 14+ messages in thread
From: Aymeric Fromherz @ 2017-02-22 19:21 UTC (permalink / raw)
  To: caml-list

Hi,

Using OCaml 4.02.3, I do not have a warning for unused variables when
only one of the variables in a pair is unused.
For instance, the code

let (a,b) = (0, 42) in b;;

raises no warning, while

let (a,b) = (0,42) in 1;; raises the following

Warning 26: unused variable b.
Warning 26: unused variable a.

I would have expected a warning in the first case since a is unused.

Warnings for single unused variables, such as let a = 42 in 1 are still
raised.

What is the reason for this? Is this behaviour intended? How are unused
variables determined?

Thanks in advance,
Aymeric Fromherz

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

* Re: [Caml-list] Warning for unused variables
  2017-02-22 19:21         ` [Caml-list] Warning for unused variables Aymeric Fromherz
@ 2017-02-22 19:43           ` Gabriel Scherer
  2017-02-22 19:59             ` Aymeric Fromherz
  0 siblings, 1 reply; 14+ messages in thread
From: Gabriel Scherer @ 2017-02-22 19:43 UTC (permalink / raw)
  To: Aymeric Fromherz; +Cc: caml users

This would be a bug, but I cannot reproduce it on my machine, where
all OCaml versions I have (including 4.02.3) raise a warning as
expected.

On Wed, Feb 22, 2017 at 2:21 PM, Aymeric Fromherz
<aymeric.fromherz@ens.fr> wrote:
> Hi,
>
> Using OCaml 4.02.3, I do not have a warning for unused variables when
> only one of the variables in a pair is unused.
> For instance, the code
>
> let (a,b) = (0, 42) in b;;
>
> raises no warning, while
>
> let (a,b) = (0,42) in 1;; raises the following
>
> Warning 26: unused variable b.
> Warning 26: unused variable a.
>
> I would have expected a warning in the first case since a is unused.
>
> Warnings for single unused variables, such as let a = 42 in 1 are still
> raised.
>
> What is the reason for this? Is this behaviour intended? How are unused
> variables determined?
>
> Thanks in advance,
> Aymeric Fromherz
>
> --
> 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

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

* Re: [Caml-list] Warning for unused variables
  2017-02-22 19:43           ` Gabriel Scherer
@ 2017-02-22 19:59             ` Aymeric Fromherz
  2017-02-22 20:01               ` Gabriel Scherer
  0 siblings, 1 reply; 14+ messages in thread
From: Aymeric Fromherz @ 2017-02-22 19:59 UTC (permalink / raw)
  To: caml-list

Hi,

Trying with OCaml 4.04 on another machine, I have the same behaviour.
When running the ocaml interpreter with all warnings activated (-w A
option), a warning is raised in the first case, but it is a warning 27
instead of a warning 26.

What is the difference between these two warnings? Could it be that you
have all warnings activated by default?

Thanks,
Aymeric

On 22/02/2017 20:43, Gabriel Scherer wrote:
> This would be a bug, but I cannot reproduce it on my machine, where
> all OCaml versions I have (including 4.02.3) raise a warning as
> expected.
> 
> On Wed, Feb 22, 2017 at 2:21 PM, Aymeric Fromherz
> <aymeric.fromherz@ens.fr> wrote:
>> Hi,
>>
>> Using OCaml 4.02.3, I do not have a warning for unused variables when
>> only one of the variables in a pair is unused.
>> For instance, the code
>>
>> let (a,b) = (0, 42) in b;;
>>
>> raises no warning, while
>>
>> let (a,b) = (0,42) in 1;; raises the following
>>
>> Warning 26: unused variable b.
>> Warning 26: unused variable a.
>>
>> I would have expected a warning in the first case since a is unused.
>>
>> Warnings for single unused variables, such as let a = 42 in 1 are still
>> raised.
>>
>> What is the reason for this? Is this behaviour intended? How are unused
>> variables determined?
>>
>> Thanks in advance,
>> Aymeric Fromherz
>>
>> --
>> 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
> 

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

* Re: [Caml-list] Warning for unused variables
  2017-02-22 19:59             ` Aymeric Fromherz
@ 2017-02-22 20:01               ` Gabriel Scherer
  2017-02-22 20:17                 ` Aymeric Fromherz
  0 siblings, 1 reply; 14+ messages in thread
From: Gabriel Scherer @ 2017-02-22 20:01 UTC (permalink / raw)
  To: Aymeric Fromherz; +Cc: caml users

Good point, I had not noticed the different in warning number.

  `ocamlc -warn-help` says:

 26 Suspicious unused variable: unused variable that is bound
    with "let" or "as", and doesn't start with an underscore ("_")
    character.
 27 Innocuous unused variable: unused variable that is not bound with
    "let" nor "as", and doesn't start with an underscore ("_")
    character.

I agree that the distinction is somewhat arbitrary here: arguably
variables bound in a pattern by a *let* (instead of a
pattern-matching) could be considered "suspicous" as well. But we
don't want to change the semantics of an existing pattern too much, as
it tends to annoy people, so maybe it's best left as it is.

On Wed, Feb 22, 2017 at 2:59 PM, Aymeric Fromherz
<aymeric.fromherz@ens.fr> wrote:
> Hi,
>
> Trying with OCaml 4.04 on another machine, I have the same behaviour.
> When running the ocaml interpreter with all warnings activated (-w A
> option), a warning is raised in the first case, but it is a warning 27
> instead of a warning 26.
>
> What is the difference between these two warnings? Could it be that you
> have all warnings activated by default?
>
> Thanks,
> Aymeric
>
> On 22/02/2017 20:43, Gabriel Scherer wrote:
>> This would be a bug, but I cannot reproduce it on my machine, where
>> all OCaml versions I have (including 4.02.3) raise a warning as
>> expected.
>>
>> On Wed, Feb 22, 2017 at 2:21 PM, Aymeric Fromherz
>> <aymeric.fromherz@ens.fr> wrote:
>>> Hi,
>>>
>>> Using OCaml 4.02.3, I do not have a warning for unused variables when
>>> only one of the variables in a pair is unused.
>>> For instance, the code
>>>
>>> let (a,b) = (0, 42) in b;;
>>>
>>> raises no warning, while
>>>
>>> let (a,b) = (0,42) in 1;; raises the following
>>>
>>> Warning 26: unused variable b.
>>> Warning 26: unused variable a.
>>>
>>> I would have expected a warning in the first case since a is unused.
>>>
>>> Warnings for single unused variables, such as let a = 42 in 1 are still
>>> raised.
>>>
>>> What is the reason for this? Is this behaviour intended? How are unused
>>> variables determined?
>>>
>>> Thanks in advance,
>>> Aymeric Fromherz
>>>
>>> --
>>> 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
>>
>
> --
> 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

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

* Re: [Caml-list] Warning for unused variables
  2017-02-22 20:01               ` Gabriel Scherer
@ 2017-02-22 20:17                 ` Aymeric Fromherz
  0 siblings, 0 replies; 14+ messages in thread
From: Aymeric Fromherz @ 2017-02-22 20:17 UTC (permalink / raw)
  To: caml-list

I didn't know -warn-help. That's very complete.

Indeed, I would have considered that this case was suspicious, but I
perfectly understand your point.
Thanks a lot!

On 22/02/2017 21:01, Gabriel Scherer wrote:
> Good point, I had not noticed the different in warning number.
> 
>   `ocamlc -warn-help` says:
> 
>  26 Suspicious unused variable: unused variable that is bound
>     with "let" or "as", and doesn't start with an underscore ("_")
>     character.
>  27 Innocuous unused variable: unused variable that is not bound with
>     "let" nor "as", and doesn't start with an underscore ("_")
>     character.
> 
> I agree that the distinction is somewhat arbitrary here: arguably
> variables bound in a pattern by a *let* (instead of a
> pattern-matching) could be considered "suspicous" as well. But we
> don't want to change the semantics of an existing pattern too much, as
> it tends to annoy people, so maybe it's best left as it is.
> 
> On Wed, Feb 22, 2017 at 2:59 PM, Aymeric Fromherz
> <aymeric.fromherz@ens.fr> wrote:
>> Hi,
>>
>> Trying with OCaml 4.04 on another machine, I have the same behaviour.
>> When running the ocaml interpreter with all warnings activated (-w A
>> option), a warning is raised in the first case, but it is a warning 27
>> instead of a warning 26.
>>
>> What is the difference between these two warnings? Could it be that you
>> have all warnings activated by default?
>>
>> Thanks,
>> Aymeric
>>
>> On 22/02/2017 20:43, Gabriel Scherer wrote:
>>> This would be a bug, but I cannot reproduce it on my machine, where
>>> all OCaml versions I have (including 4.02.3) raise a warning as
>>> expected.
>>>
>>> On Wed, Feb 22, 2017 at 2:21 PM, Aymeric Fromherz
>>> <aymeric.fromherz@ens.fr> wrote:
>>>> Hi,
>>>>
>>>> Using OCaml 4.02.3, I do not have a warning for unused variables when
>>>> only one of the variables in a pair is unused.
>>>> For instance, the code
>>>>
>>>> let (a,b) = (0, 42) in b;;
>>>>
>>>> raises no warning, while
>>>>
>>>> let (a,b) = (0,42) in 1;; raises the following
>>>>
>>>> Warning 26: unused variable b.
>>>> Warning 26: unused variable a.
>>>>
>>>> I would have expected a warning in the first case since a is unused.
>>>>
>>>> Warnings for single unused variables, such as let a = 42 in 1 are still
>>>> raised.
>>>>
>>>> What is the reason for this? Is this behaviour intended? How are unused
>>>> variables determined?
>>>>
>>>> Thanks in advance,
>>>> Aymeric Fromherz
>>>>
>>>> --
>>>> 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
>>>
>>
>> --
>> 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
> 

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

end of thread, other threads:[~2017-02-22 20:10 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-22 17:01 [Caml-list] List of structurally typed objects Richard W.M. Jones
2017-02-22 17:09 ` Gabriel Scherer
2017-02-22 17:36   ` Richard W.M. Jones
2017-02-22 17:43     ` Nicolás Ojeda Bär
2017-02-22 17:53       ` Richard W.M. Jones
2017-02-22 17:28 ` Damien Guichard
2017-02-22 17:38   ` Richard W.M. Jones
2017-02-22 18:08     ` Ivan Gotovchits
2017-02-22 19:04       ` Mikhail Mandrykin
2017-02-22 19:21         ` [Caml-list] Warning for unused variables Aymeric Fromherz
2017-02-22 19:43           ` Gabriel Scherer
2017-02-22 19:59             ` Aymeric Fromherz
2017-02-22 20:01               ` Gabriel Scherer
2017-02-22 20:17                 ` Aymeric Fromherz

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