* Modules within classes
@ 2006-09-13 7:44 Martin Hofmann
2006-09-13 8:10 ` [Caml-list] " Jonathan Roewen
2006-09-13 9:36 ` Pietro Abate
0 siblings, 2 replies; 4+ messages in thread
From: Martin Hofmann @ 2006-09-13 7:44 UTC (permalink / raw)
To: caml-list; +Cc: Oliver.Friedmann
Dear all,
Can someone explain what is wrong here and perhaps suggest a workaround?
class ['a] test ( l : 'a list) =
object
method result =
let module M =
struct
type t = 'a
let compare x y =
if x < y then -1 else if x > y then 1 else 0
end
in
let module MSet = Set.Make(M)
in MSet.elements (List.fold_right MSet.add l MSet.empty)
end;;
OCaml says: "Unbound type parameter 'a".
Our aim is of course not to program sorting the umpteenth time but to
have a means for using Pervasives.Set within a parametrised class.
The problem seems to be that although "class ['a]" binds 'a its use is
nevertheless disallowed in the scope of the class declaration.
Many thanks in advance!
Martin Hofmann (on behalf of Oliver Friedmann)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Modules within classes
2006-09-13 7:44 Modules within classes Martin Hofmann
@ 2006-09-13 8:10 ` Jonathan Roewen
2006-09-13 9:48 ` Jonathan Roewen
2006-09-13 9:36 ` Pietro Abate
1 sibling, 1 reply; 4+ messages in thread
From: Jonathan Roewen @ 2006-09-13 8:10 UTC (permalink / raw)
To: Martin Hofmann; +Cc: caml-list, Oliver.Friedmann
It's the same at normal scope:
type t = 'a;; doesn't compile.
you need: type 'a t = 'a, which isn't compatible with input type for Set.
I think the Set module is intentionally monomorphic...
Jonathan
On 9/13/06, Martin Hofmann <mhofmann@tcs.ifi.lmu.de> wrote:
> Dear all,
>
> Can someone explain what is wrong here and perhaps suggest a workaround?
>
> class ['a] test ( l : 'a list) =
> object
> method result =
> let module M =
> struct
> type t = 'a
> let compare x y =
> if x < y then -1 else if x > y then 1 else 0
> end
> in
> let module MSet = Set.Make(M)
> in MSet.elements (List.fold_right MSet.add l MSet.empty)
> end;;
>
> OCaml says: "Unbound type parameter 'a".
>
> Our aim is of course not to program sorting the umpteenth time but to
> have a means for using Pervasives.Set within a parametrised class.
>
> The problem seems to be that although "class ['a]" binds 'a its use is
> nevertheless disallowed in the scope of the class declaration.
>
> Many thanks in advance!
>
> Martin Hofmann (on behalf of Oliver Friedmann)
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Modules within classes
2006-09-13 7:44 Modules within classes Martin Hofmann
2006-09-13 8:10 ` [Caml-list] " Jonathan Roewen
@ 2006-09-13 9:36 ` Pietro Abate
1 sibling, 0 replies; 4+ messages in thread
From: Pietro Abate @ 2006-09-13 9:36 UTC (permalink / raw)
To: caml-list, caml-list
On Wed, Sep 13, 2006 at 09:44:30AM +0200, Martin Hofmann wrote:
> Can someone explain what is wrong here and perhaps suggest a workaround?
maybe you can try to encapsulate your class in a functor ?
but I guess this doesn't really solve your problem...
module Make(T : sig type a end) = struct
class test ( l : T.a list) =
object
method result =
let module M =
struct
type t = T.a
let compare x y =
if x < y then -1 else if x > y then 1 else 0
end
in
let module MSet = Set.Make(M)
in MSet.elements (List.fold_right MSet.add l MSet.empty)
end;;
end
:)
p
--
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
See http://www.fsf.org/philosophy/no-word-attachments.html
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Modules within classes
2006-09-13 8:10 ` [Caml-list] " Jonathan Roewen
@ 2006-09-13 9:48 ` Jonathan Roewen
0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Roewen @ 2006-09-13 9:48 UTC (permalink / raw)
To: Martin Hofmann; +Cc: caml-list, Oliver.Friedmann
As for a workaround, use List.sort. I presume there's a real reason to
want to use Set rather than the contrived example given though... if
not, then there's your solution (=
Jonathan
PS: The order of traversing the list doesn't seem to be of much
significance, so List.fold_left might be better suited.
On 9/13/06, Jonathan Roewen <jonathan.roewen@gmail.com> wrote:
> It's the same at normal scope:
>
> type t = 'a;; doesn't compile.
>
> you need: type 'a t = 'a, which isn't compatible with input type for Set.
>
> I think the Set module is intentionally monomorphic...
>
> Jonathan
>
> On 9/13/06, Martin Hofmann <mhofmann@tcs.ifi.lmu.de> wrote:
> > Dear all,
> >
> > Can someone explain what is wrong here and perhaps suggest a workaround?
> >
> > class ['a] test ( l : 'a list) =
> > object
> > method result =
> > let module M =
> > struct
> > type t = 'a
> > let compare x y =
> > if x < y then -1 else if x > y then 1 else 0
> > end
> > in
> > let module MSet = Set.Make(M)
> > in MSet.elements (List.fold_right MSet.add l MSet.empty)
> > end;;
> >
> > OCaml says: "Unbound type parameter 'a".
> >
> > Our aim is of course not to program sorting the umpteenth time but to
> > have a means for using Pervasives.Set within a parametrised class.
> >
> > The problem seems to be that although "class ['a]" binds 'a its use is
> > nevertheless disallowed in the scope of the class declaration.
> >
> > Many thanks in advance!
> >
> > Martin Hofmann (on behalf of Oliver Friedmann)
> >
> > _______________________________________________
> > Caml-list mailing list. Subscription management:
> > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> > Archives: http://caml.inria.fr
> > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> > Bug reports: http://caml.inria.fr/bin/caml-bugs
> >
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-09-13 9:48 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-13 7:44 Modules within classes Martin Hofmann
2006-09-13 8:10 ` [Caml-list] " Jonathan Roewen
2006-09-13 9:48 ` Jonathan Roewen
2006-09-13 9:36 ` Pietro Abate
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox