English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
unused variable error with objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-03-20 (14:06)
From: Jeremy Yallop <jeremy.yallop@e...>
Subject: Re: [Caml-list] unused variable error with objects
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 = ()

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