* unused variable error with objects
@ 2008-03-20 13:40 Ralph Douglass
2008-03-20 13:57 ` [Caml-list] " Julien Signoles
2008-03-20 13:58 ` Jeremy Yallop
0 siblings, 2 replies; 4+ messages in thread
From: Ralph Douglass @ 2008-03-20 13:40 UTC (permalink / raw)
To: Caml List
[-- Attachment #1: Type: text/plain, Size: 1080 bytes --]
I'm a little confused by some code that I'm trying to compile some code with
"-w Ae -warn-error Ae".
Below is an examle:
class foo = object
method bar ~(snoo : string) = ()
end;;
let biff = new foo;;
biff#bar ~snoo:"hi";;
When I compile it (ocamlc -c -w Ae -warn-error Ae foo.ml), as expected, I
get:
File "foo.ml", line 2, characters 15-19:
Warning Z: unused variable snoo.
With labeled functions, I usually do something like ~snoo:_, but it does not
work in this exampled with methods:
---
class foo = object
method bar ~(snoo : string):_ = ()
end;;
let biff = new foo;;
biff#bar ~snoo:"hi";;
--
ocamlc -c -w Ae -warn-error Ae foo.ml
File "foo.ml", line 2, characters 15-19:
Warning Z: unused variable snoo.
On a whim, I did the following, which surprisingly worked:
class foo = object
method bar ~(_snoo : string) = ()
end;;
let biff = new foo;;
biff#bar ~_snoo:"hi";;
And that compiles, which confused me. But I have to put the underbar into
the method call, which seems wrong.
Is there a solution to this?
I'm running 3.10.1, from godi.
Thanks!
--
Ralph
[-- Attachment #2: Type: text/html, Size: 1436 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] unused variable error with objects
2008-03-20 13:40 unused variable error with objects Ralph Douglass
@ 2008-03-20 13:57 ` Julien Signoles
2008-03-20 13:58 ` Jeremy Yallop
1 sibling, 0 replies; 4+ messages in thread
From: Julien Signoles @ 2008-03-20 13:57 UTC (permalink / raw)
To: Ralph Douglass; +Cc: Caml List
> With labeled functions, I usually do something like ~snoo:_, but it does not
> work in this exampled with methods:
> ---
> class foo = object
> method bar ~(snoo : string):_ = ()
> end;;
>
> let biff = new foo;;
> biff#bar ~snoo:"hi";;
> --
> ocamlc -c -w Ae -warn-error Ae foo.ml
> File "foo.ml", line 2, characters 15-19:
> Warning Z: unused variable snoo.
>
> On a whim, I did the following, which surprisingly worked:
>
> class foo = object
> method bar ~(_snoo : string) = ()
> end;;
>
> let biff = new foo;;
> biff#bar ~_snoo:"hi";;
>
> And that compiles, which confused me. But I have to put the underbar into
> the method call, which seems wrong.
> Is there a solution to this?
Try this:
class foo = object
method bar ~snoo:(_:string) = ()
end;;
(new foo)#bar ~snoo:"hi";;
--
Julien
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] unused variable error with objects
2008-03-20 13:40 unused variable error with objects Ralph Douglass
2008-03-20 13:57 ` [Caml-list] " Julien Signoles
@ 2008-03-20 13:58 ` Jeremy Yallop
2008-03-20 15:42 ` Ralph Douglass
1 sibling, 1 reply; 4+ messages in thread
From: Jeremy Yallop @ 2008-03-20 13:58 UTC (permalink / raw)
To: Ralph Douglass; +Cc: Caml List
Ralph Douglass wrote:
> With labeled functions, I usually do something like ~snoo:_, but it does
> not work in this exampled with methods:
> ---
> class foo = object
> method bar ~(snoo : string):_ = ()
> end;;
In this case the ':_' denotes a type annotation, using the "any-type"
expression '_'. You're declaring the return type of the method, as
you'll see if you change the annotation to something incompatible with
unit, such as
class foo = object
method bar ~(snoo : string):int = ()
end
> On a whim, I did the following, which surprisingly worked:
>
> class foo = object
> method bar ~(_snoo : string) = ()
> end;;
This works because the "unused variables" warning is disabled for
variables which start with an underscore. It's a useful feature for
normal bindings but I don't think you should use it for labels, since it
changes the interface.
> Is there a solution to this?
I think the following is what you want:
class foo = object
method bar ~snoo:(_:string) = ()
end
Jeremy.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] unused variable error with objects
2008-03-20 13:58 ` Jeremy Yallop
@ 2008-03-20 15:42 ` Ralph Douglass
0 siblings, 0 replies; 4+ messages in thread
From: Ralph Douglass @ 2008-03-20 15:42 UTC (permalink / raw)
To: Jeremy Yallop; +Cc: Caml List
[-- Attachment #1: Type: text/plain, Size: 1355 bytes --]
Great, that does indeed give me exactly what I want. I guess I just got
confused by the type annotation syntax, and for some reason I had the order
wrong.
Thanks again!
On 3/20/08, Jeremy Yallop <jeremy.yallop@ed.ac.uk> wrote:
>
> Ralph Douglass wrote:
> > With labeled functions, I usually do something like ~snoo:_, but it does
> > not work in this exampled with methods:
> > ---
> > class foo = object
> > method bar ~(snoo : string):_ = ()
> > end;;
>
>
> In this case the ':_' denotes a type annotation, using the "any-type"
> expression '_'. You're declaring the return type of the method, as
> you'll see if you change the annotation to something incompatible with
> unit, such as
>
> class foo = object
> method bar ~(snoo : string):int = ()
> end
>
>
> > On a whim, I did the following, which surprisingly worked:
> >
> > class foo = object
> > method bar ~(_snoo : string) = ()
> > end;;
>
>
> This works because the "unused variables" warning is disabled for
> variables which start with an underscore. It's a useful feature for
> normal bindings but I don't think you should use it for labels, since it
> changes the interface.
>
>
> > Is there a solution to this?
>
>
> I think the following is what you want:
>
>
> class foo = object
> method bar ~snoo:(_:string) = ()
> end
>
>
> Jeremy.
>
--
Ralph
[-- Attachment #2: Type: text/html, Size: 2023 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2008-03-20 15:42 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-20 13:40 unused variable error with objects Ralph Douglass
2008-03-20 13:57 ` [Caml-list] " Julien Signoles
2008-03-20 13:58 ` Jeremy Yallop
2008-03-20 15:42 ` Ralph Douglass
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox