Version française
Home     About     Download     Resources     Contact us    
Browse thread
Passing class type as parameter?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Passing class type as parameter?
From: Jeremy Cowgar <jeremy@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