Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* list of objects
@ 2000-10-02 16:38 Julian Assange
  2000-10-03  9:53 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Julian Assange @ 2000-10-02 16:38 UTC (permalink / raw)
  To: caml-list; +Cc: proff


I have a variety of signature schemes for text. I'd like to have
a list of these schemes, so I can do such things as:

        List.map (fun x -> x#make text) schemes

Neither classes or modules are first class objects. However, 
objects are. So, my first thought was to create something along the lines
of 

        let schemes = [new scheme_11; ...; new scheme_n]

Since the objects are functional (at least so far) this seems like an
acceptable approach. 

  class virtual signature =
  object 
  end
    
  class ['a] plain =
  object (self)
    inherit signature
    method make s = s
    method digest s = Digest.string (self#make s)
    method compare (a:'a) b = a = b
  end
    
  class ['a] simple =
  object
    inherit ['a] plain 
    method make s = string_filter (alphabetize >> lower) s
  end

let sig_list = [new plain; new simple] 

On compilation, this produces:

The type of this expression, '_a Sigs.plain list,
contains type variables that cannot be generalized

However sending the exact same code to the top level,
produces:
module Sigs :
  sig
    val vanilla : 'a -> 'a
    val lower : char option -> char option
    val alphabetize : char option -> char option
    val string_filter : (char option -> char option) -> string -> string
    class virtual signature : object end
    class ['a] plain :
      object
        method compare : 'a -> 'a -> bool
        method digest : string -> Digest.t
        method make : string -> string
      end
    class ['a] simple :
      object
        method compare : 'a -> 'a -> bool
        method digest : string -> Digest.t
        method make : string -> string
      end
  end
val sig_list : '_a Sigs.plain list = [<obj>; <obj>]

Works just fine! What gives?



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

end of thread, other threads:[~2000-10-03 19:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-10-02 16:38 list of objects Julian Assange
2000-10-03  9:53 ` Xavier Leroy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox