caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Unbound type variables
@ 2001-09-13 17:15 Patrick M Doane
  2001-09-13 17:36 ` Olivier Andrieu
  2001-09-13 17:47 ` Didier Le Botlan
  0 siblings, 2 replies; 4+ messages in thread
From: Patrick M Doane @ 2001-09-13 17:15 UTC (permalink / raw)
  To: caml-list

While writing code today, I came across an annoying problem with using
polymorphic variants and objects. The basic code fragment is this:

class c = object
  method m x =
    match x with
      | `A -> ()
      | `B -> ()
      | `C -> ()
end

which produces an error:

The method m has type
  [< `A | `B | `C] -> unit
where .. is unbound

Now, there is a simple solution to the problem:

  type t = [ `A | `B | `C]

  ...

  match (x:t) with


but one of the benefits of using polymorphic variants is that I can
usually avoid such redundant declarations.

I'm also suprised by the inferred type, since the polymorphism
just causing a failure and there isn't anyway I'm aware of to be able to
bind that row variable to the type of the class.  Would it be better to
type the expression differently from within an object context?

Any other ways I can improve what I'm doing?

Patrick

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Unbound type variables
  2001-09-13 17:15 [Caml-list] Unbound type variables Patrick M Doane
@ 2001-09-13 17:36 ` Olivier Andrieu
  2001-09-13 17:47 ` Didier Le Botlan
  1 sibling, 0 replies; 4+ messages in thread
From: Olivier Andrieu @ 2001-09-13 17:36 UTC (permalink / raw)
  To: Patrick M Doane; +Cc: caml-list

 Patrick M Doane [Thursday 13 September 2001] :
 >
 > While writing code today, I came across an annoying problem with using
 > polymorphic variants and objects. The basic code fragment is this:
 > 
 > class c = object
 >   method m x =
 >     match x with
 >       | `A -> ()
 >       | `B -> ()
 >       | `C -> ()
 > end
 > 
 > which produces an error:
 > 
 > The method m has type
 >   [< `A | `B | `C] -> unit
 > where .. is unbound

 > ... there isn't anyway I'm aware of to be able to
 > bind that row variable to the type of the class.  

yes you can (if I understand you correctly) :

 class ['a] c = object
   method m : 'a -> unit = function
       | `A -> ()
       | `B -> ()
       | `C -> ()
 end

and you get

 class ['a] c : 
   object
    constraint 'a = [< `A | `B | `C] 
    method m : 'a -> unit 
   end

-- 
   Olivier
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Unbound type variables
  2001-09-13 17:47 ` Didier Le Botlan
@ 2001-09-13 17:40   ` Patrick M Doane
  0 siblings, 0 replies; 4+ messages in thread
From: Patrick M Doane @ 2001-09-13 17:40 UTC (permalink / raw)
  To: Didier Le Botlan; +Cc: caml-list

Interesting - thanks to you and Olivier for pointing this out!

I wasn't aware of how to do this or what the 'constraint' aspect might be
used for.

Patrick

On Thu, 13 Sep 2001, Didier Le Botlan wrote:

> What do you think of a parameterized class ?
> 
> class ['a] c = object
>   method m (x:'a) =
>     match x with
>       | `A -> ()
>       | `B -> ()
>       | `C -> ()
> end
> 
> class [[< `A | `B | `C] as 'b] c :
>   object constraint 'b = 'b method m : 'b -> unit end    
> 
> Beware that the 'a parameter is freezed for each instance of the class,
> that is you cannot call method m twice on the same object with
> incompatible arguments.
> 
> 
> --
> Didier Le Botlan
> http://cristal.inria.fr/~lebotlan
> 

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Unbound type variables
  2001-09-13 17:15 [Caml-list] Unbound type variables Patrick M Doane
  2001-09-13 17:36 ` Olivier Andrieu
@ 2001-09-13 17:47 ` Didier Le Botlan
  2001-09-13 17:40   ` Patrick M Doane
  1 sibling, 1 reply; 4+ messages in thread
From: Didier Le Botlan @ 2001-09-13 17:47 UTC (permalink / raw)
  To: Patrick M Doane; +Cc: caml-list

What do you think of a parameterized class ?

class ['a] c = object
  method m (x:'a) =
    match x with
      | `A -> ()
      | `B -> ()
      | `C -> ()
end

class [[< `A | `B | `C] as 'b] c :
  object constraint 'b = 'b method m : 'b -> unit end    

Beware that the 'a parameter is freezed for each instance of the class,
that is you cannot call method m twice on the same object with
incompatible arguments.


--
Didier Le Botlan
http://cristal.inria.fr/~lebotlan
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2001-09-13 17:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-13 17:15 [Caml-list] Unbound type variables Patrick M Doane
2001-09-13 17:36 ` Olivier Andrieu
2001-09-13 17:47 ` Didier Le Botlan
2001-09-13 17:40   ` Patrick M Doane

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