Version française
Home     About     Download     Resources     Contact us    
Browse thread
Polymorphic class types?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Matt Gushee <matt@g...>
Subject: Polymorphic class types?
Hello, all--

I am trying to design a high-level GUI library in object-oriented OCaml.
 It will be based on LablTk, but that's not really relevant--right now
I'm just trying to work out skeleton classes; the current code doesn't
even import LablTk yet.

Anyway, the concept I'm trying to implement is a notecard metaphor,
where the user will interact with information through a number of cards,
which can be iconified, deiconified, moved around, etc[*]. Cards should
also be able to display arbitrary data types--in practice I think the
data will mostly be key-value structures where the values are mostly
strings, but I don't want to limit it to that.

So you are probably starting to get a sense of the problem: I need a
basic framework of display logic that is independent of the application
data, for positioning cards on the canvas, handling events, etc. But I
also need to be able to get and set the content of cards in response to
GUI events--the content being some arbitrary data structure, as
mentioned above.

So I have been trying to work with parameterized class type like this:

  class type ['a] card = object
    method id : string
    method place : int -> int -> unit
    method show : ?expanded:bool -> unit -> unit
    method hide : unit -> unit
    method expand : unit -> unit
    method collapse : int -> int -> unit
    method content : (string * 'a) list
    method title : string

  class type ['a] cp_canvas = object
    method base : Widget.canvas Widget.widget
    method cards : 'a card list
    method get_card : string -> 'a card
    method add_card : 'a card -> unit
    method show : selection -> unit
    method hide : selection -> unit
    method expand : selection -> unit
    method collapse : selection -> unit

But I don't think it's going to work. The sticking point is that there
need to be various subtypes of cards, each with appropriate display
logic for its content type, yet the canvas needs to be able to manage
cards in a generic manner--i.e. it needs to work with the 'card'
type--but of course that type doesn't exist, because 'card' takes a
parameter, right?

I won't go into detail here about the problems I've been having--I
suspect the design is wrong, but I have no clear idea of how to do it
right. I know my library could use an abstract 'data' type, which could
then be implemented in an application as, for example,

  type data =
    | I of int
    | F of float
    | S of string

... but I'd rather not if it can be helped. And I don't care very much
if it's object-oriented ... I started this several months ago and put it
aside for a while, so I don't remember what exactly I was thinking, but
probably the main point of using objects was to make it easy to add new
card types.

So, I would appreciate suggestions.

[*] Am I reinventing HyperCard? Could be ...

Matt Gushee
Englewood, CO, USA