Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Passing class type as parameter?

18 views
Skip to first unread message

Jeremy Cowgar

unread,
Sep 13, 2006, 3:00:42 PM9/13/06
to caml...@yquem.inria.fr
Can I do something like:

class base_model = object(self)
method from_array ary = ...
end ;;

class user = object(self)
inherit base_model
...
end ;;

let finder sql class_type =
query_database sql ;
let res = new class_type in
res#from_array res ;;

let users = finder "SELECT * FROM users" user in
xxx yyy ;;

Ok. That is not working code, prob has syntax errors as well, but you
get my idea. The problem I am having is passing the class to the
generic finder method.

Thanks,

Jeremy

_______________________________________________
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

Jacques Garrigue

unread,
Sep 13, 2006, 8:04:26 PM9/13/06
to jer...@cowgar.com
From: Jeremy Cowgar <jer...@cowgar.com>

> Can I do something like:
>
> class base_model = object(self)
> method from_array ary = ...
> end ;;
>
> class user = object(self)
> inherit base_model
> ...
> end ;;
>
> let finder sql class_type =
> query_database sql ;
> let res = new class_type in
> res#from_array res ;;
>
> let users = finder "SELECT * FROM users" user in
> xxx yyy ;;
>
> Ok. That is not working code, prob has syntax errors as well, but you
> get my idea. The problem I am having is passing the class to the
> generic finder method.

No, you can't, but you can pass the class constructor in place, which
is just equivalent.

let finder sql class_new =
query_database sql ;
let res = class_new () in
res#from_array res ;;

let users = finder "SELECT * FROM users" (fun () -> new user) in
xxx yyy ;;

Note that in general the class constructor takes arguments, so you
don't need the above anonymous function.

Jacques Garrigue

0 new messages