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

* Re: list of objects
  2000-10-02 16:38 list of objects Julian Assange
@ 2000-10-03  9:53 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 2000-10-03  9:53 UTC (permalink / raw)
  To: Julian Assange, caml-list

Your approach looks reasonable.  As for your question:

> 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:
> val sig_list : '_a Sigs.plain list = [<obj>; <obj>]
> Works just fine! What gives?

The '_a is a non-generalized type variable, i.e. an unknown type that
will be determined later at first use.  (See the FAQ
http://caml.inria.fr/FAQ/FAQ_EXPERT-eng.html for more detailed
explanations.)

For separately-compiled units, the notion of "first use" is unclear,
since the unit can be referenced by several other separately-compiled
units, which could each instantiate the unknown type in different
ways.  So, to keep things simple, the compiler requires that no
non-generalized type variables remain in the types inferred for
definitions of a compilation unit.

Thus, you need to put either a .mli file giving the type you want for
sig_list, or a type constraint on the definition of sig_list:
        let sig_list = ([new plain; new simple] : t Sigs.plain list)
with the type you want for "t".

Hope this helps,

- Xavier Leroy



^ 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