* [Caml-list] Passing self to a new object
@ 2001-10-28 15:56 Andrew Lawson
2001-10-29 9:02 ` Jacques Garrigue
0 siblings, 1 reply; 4+ messages in thread
From: Andrew Lawson @ 2001-10-28 15:56 UTC (permalink / raw)
To: caml-list
Hi all
I'm writing a gui program where the callback for a button
creates a new object (also a gui) and needs to pass it a reference to
itself in order that the new object can contact the original for
information. The classes look something like this;
class xyz =
let top = ... in
let ... = ... in
let btnNew = ... ~command:(fun () = new abc self) top in
object (self)
...
...
end
class abc (parent:xyz) =
object (self)
var myparent = parent
...
end
My error is;
This expression has type
< btnDelete : 'a; btnEdit : 'b; btnExit : 'c; btnFind : 'd;
btnNew : Widget.button Widget.widget; get_config : 'e -> 'f;
list_record_directory : 'g; new_filename : 'h; read_configuration :
'i;
self : 'j; set_config : 'k -> 'l -> 'm; topLevel : 'n; .. >
but is here used with type 'j
Self type cannot escape its class
And I have no clue what I am doing wrong :)
All suggestions appreciated
Andrew
--
Andrew Lawson
andrew@absentis.com
-------------------
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] Passing self to a new object
2001-10-28 15:56 [Caml-list] Passing self to a new object Andrew Lawson
@ 2001-10-29 9:02 ` Jacques Garrigue
2001-10-29 20:30 ` Andrew Lawson
0 siblings, 1 reply; 4+ messages in thread
From: Jacques Garrigue @ 2001-10-29 9:02 UTC (permalink / raw)
To: andrew; +Cc: caml-list
From: Andrew Lawson <andrew@absentis.com>
> I'm writing a gui program where the callback for a button
> creates a new object (also a gui) and needs to pass it a reference to
> itself in order that the new object can contact the original for
> information. The classes look something like this;
>
> class xyz =
> let top = ... in
> let ... = ... in
> object (self)
> method btnNew = ... ~command:(fun () = new abc self) top in
> ...
> ...
> end
>
> and abc (parent:xyz) =
> object (self)
> var myparent = parent
> ...
> end
>
>My error is;
> .....
> Self type cannot escape its class
You've bumped into a painful quirk of ocaml's object system: self's
type is not xyz, and must be handled with care.
(By the way, your program seemed strange, since you were using self
and abc before defining them, but I suppose it was a copy error, and I
corrected it.)
The general solution to this is to first define a class type:
class type xyz_t = object ('self)
method
btnNew : ...
...
end
Then change the code in btnNew to
new abc (self :> xyz_t)
Remark that making xyz_t exact may be a bit lengthy, but you may leave
out methods you don't need to call from abc.
Hope this helps,
Jacques Garrigue
-------------------
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] Passing self to a new object
2001-10-29 9:02 ` Jacques Garrigue
@ 2001-10-29 20:30 ` Andrew Lawson
2001-10-30 8:09 ` Jacques Garrigue
0 siblings, 1 reply; 4+ messages in thread
From: Andrew Lawson @ 2001-10-29 20:30 UTC (permalink / raw)
To: caml-list
On Mon, Oct 29, 2001 at 06:02:07PM +0900, Jacques Garrigue wrote:
> From: Andrew Lawson <andrew@absentis.com>
>
> > I'm writing a gui program where the callback for a button
> > creates a new object (also a gui) and needs to pass it a reference to
> > itself in order that the new object can contact the original for
> > information. The classes look something like this;
> >
> > class xyz =
> > let top = ... in
> > let ... = ... in
> > object (self)
> > method btnNew = ... ~command:(fun () = new abc self) top in
> > ...
> > ...
> > end
> >
> > and abc (parent:xyz) =
> > object (self)
> > var myparent = parent
> > ...
> > end
> >
> >My error is;
> > .....
> > Self type cannot escape its class
>
> You've bumped into a painful quirk of ocaml's object system: self's
> type is not xyz, and must be handled with care.
Ouch, I knew that even such an otherwise nice language would turn
out to have some fuzzy bits someday :)
> (By the way, your program seemed strange, since you were using self
> and abc before defining them, but I suppose it was a copy error, and I
> corrected it.)
I admit this was a quick example of the problem, not the program
itself, which has many more hairy bits.
> The general solution to this is to first define a class type:
> class type xyz_t = object ('self)
> method
> btnNew : ...
> ...
> end
Well this gives me an excuse to start playing with types and
interfaces anyway. Is this a 'known problem, will be sorted
eventually' or a 'just live with it' sort of thing?
> Then change the code in btnNew to
> new abc (self :> xyz_t)
Casting! and here was I giving a C++ programmer some abuse
about this last week :)
> Remark that making xyz_t exact may be a bit lengthy, but you may leave
> out methods you don't need to call from abc.
Well, thats not so bad then ... I suppose good style should ahve
me doing this anyway.
> Hope this helps,
Most definitely
> Jacques Garrigue
Thanks very much
Andrew
--
Andrew Lawson
andrew@absentis.com
-------------------
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] Passing self to a new object
2001-10-29 20:30 ` Andrew Lawson
@ 2001-10-30 8:09 ` Jacques Garrigue
0 siblings, 0 replies; 4+ messages in thread
From: Jacques Garrigue @ 2001-10-30 8:09 UTC (permalink / raw)
To: andrew; +Cc: caml-list
From: Andrew Lawson <andrew@absentis.com>
> > The general solution to this is to first define a class type:
> > class type xyz_t = object ('self)
> > method
> > btnNew : ...
> > ...
> > end
>
> Well this gives me an excuse to start playing with types and
> interfaces anyway. Is this a 'known problem, will be sorted
> eventually' or a 'just live with it' sort of thing?
Well, this was delayed for a very long time, but I'm starting to think
that we should really do something about it.
> > Then change the code in btnNew to
> > new abc (self :> xyz_t)
>
> Casting! and here was I giving a C++ programmer some abuse
> about this last week :)
No, this is a coercion. This is perfectly type-safe (and checked at
compile time). This is needed for type inference. Languages with
explicit type information, like Java, do it silently.
Cheers,
Jacques Garrigue
-------------------
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-10-30 8:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-28 15:56 [Caml-list] Passing self to a new object Andrew Lawson
2001-10-29 9:02 ` Jacques Garrigue
2001-10-29 20:30 ` Andrew Lawson
2001-10-30 8:09 ` Jacques Garrigue
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox