* [Caml-list] Mutually recursive types and classes
@ 2003-06-19 17:45 Nick Name
2003-06-19 22:16 ` Olivier Andrieu
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Nick Name @ 2003-06-19 17:45 UTC (permalink / raw)
To: caml-list
Hi all,
I wish to declare a type like
type a = A of (b -> unit) | ...
where b is a class like
class b =
object (this)
method f : a -> unit =
function
A f -> f this
| _ -> unit
end
or, alternatively:
class b =
object
inherit [a] otherclass
...
end
How can I do such a thing, considering that I can't write an "and"
keyword between a type declaration and a class (or class type)
declaration?
Thanks for help
Vincenzo
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Mutually recursive types and classes
2003-06-19 17:45 [Caml-list] Mutually recursive types and classes Nick Name
@ 2003-06-19 22:16 ` Olivier Andrieu
2003-06-19 22:39 ` brogoff
2003-06-20 3:15 ` John Max Skaller
2 siblings, 0 replies; 5+ messages in thread
From: Olivier Andrieu @ 2003-06-19 22:16 UTC (permalink / raw)
To: Nick Name; +Cc: caml-list
Nick Name [Thursday 19 June 2003] :
> Hi all,
>
> I wish to declare a type like
>
> type a = A of (b -> unit) | ...
>
> where b is a class like
>
> class b =
> object (this)
> method f : a -> unit =
> function
> A f -> f this
> | _ -> unit
> end
>
> or, alternatively:
>
> class b =
> object
> inherit [a] otherclass
> ...
> end
>
> How can I do such a thing, considering that I can't write an "and"
> keyword between a type declaration and a class (or class type)
> declaration?
You could add a type parameter in your definition of a :
type 'a a = A of ('a -> unit) | ...
and then
class b = object (this : 'a) method f : 'a a -> unit = ... end
or
class b = object method f : b a -> unit = ... end
and eventually redefine a :
type a = b a
--
Olivier
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Mutually recursive types and classes
2003-06-19 17:45 [Caml-list] Mutually recursive types and classes Nick Name
2003-06-19 22:16 ` Olivier Andrieu
@ 2003-06-19 22:39 ` brogoff
2003-06-20 11:07 ` Nick Name
2003-06-20 3:15 ` John Max Skaller
2 siblings, 1 reply; 5+ messages in thread
From: brogoff @ 2003-06-19 22:39 UTC (permalink / raw)
To: caml-list
You can use the parameterization trick and introduce an extra type variable,
as has been discussed on the list before. This is the big hammer for solving
all sorts of forbidden recurrences in OCaml, like the functor instance/type
recurrence. Thankfully, that particular problem now appears to be fixed in the
CVS snapshot.
You can also have recursion between object types and types, and use coercion
to make sure everything matches, like so
type a = A of (b -> unit) | B
and b = < f : a -> unit >
class c =
object(this) method f = function A foo -> foo (this :> b) | _ -> () end
-- Brian
On Thu, 19 Jun 2003, Nick Name wrote:
> Hi all,
>
> I wish to declare a type like
>
> type a = A of (b -> unit) | ...
>
> where b is a class like
>
> class b =
> object (this)
> method f : a -> unit =
> function
> A f -> f this
> | _ -> unit
> end
>
> or, alternatively:
>
> class b =
> object
> inherit [a] otherclass
> ...
> end
>
> How can I do such a thing, considering that I can't write an "and"
> keyword between a type declaration and a class (or class type)
> declaration?
>
> Thanks for help
>
> Vincenzo
>
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Mutually recursive types and classes
2003-06-19 17:45 [Caml-list] Mutually recursive types and classes Nick Name
2003-06-19 22:16 ` Olivier Andrieu
2003-06-19 22:39 ` brogoff
@ 2003-06-20 3:15 ` John Max Skaller
2 siblings, 0 replies; 5+ messages in thread
From: John Max Skaller @ 2003-06-20 3:15 UTC (permalink / raw)
To: Nick Name; +Cc: caml-list
Nick Name wrote:
> Hi all,
>
> I wish to declare a type like
>
> type a = A of (b -> unit) | ...
>
> where b is a class like
>
> class b =
> object (this)
> method f : a -> unit =
> function
> A f -> f this
> | _ -> unit
> end
Parameterise the class:
class ['a] b' = ...
then say
type a = A of (b-> unit) ..
and b = a b'
--
John Max Skaller, mailto:skaller@ozemail.com.au
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
voice:61-2-9660-0850
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Mutually recursive types and classes
2003-06-19 22:39 ` brogoff
@ 2003-06-20 11:07 ` Nick Name
0 siblings, 0 replies; 5+ messages in thread
From: Nick Name @ 2003-06-20 11:07 UTC (permalink / raw)
To: caml-list
Thanks to all, I report another solution for completeness:
class b = ...
type a = A of b -> unit | ..
class c = object (this)
inherit b
method f : a -> unit = ...
end
wich is similar in spirit to Brian's but allows to write the "inherit"
part.
Vincenzo
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2003-06-20 11:05 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-19 17:45 [Caml-list] Mutually recursive types and classes Nick Name
2003-06-19 22:16 ` Olivier Andrieu
2003-06-19 22:39 ` brogoff
2003-06-20 11:07 ` Nick Name
2003-06-20 3:15 ` John Max Skaller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox