Version française
Home     About     Download     Resources     Contact us    
Browse thread
list of objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Julian Assange <proff@i...>
Subject: list of objects

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?