List Info

Thread: "ocaml_beginners"::[] Re: Proxy class




"ocaml_beginners"::[] Re: Proxy class
user name
2006-06-02 10:47:59
Jon Harrop <jonffconsultancy.com> writes:

> On Friday 02 June 2006 10:38, Maxim Treskin wrote:
>> Is it possible to dynamically generate class
methods in OCaml?
>
> No because OCaml is statically typed. You can, of
course, get equivalent 
> functionality using some other approach in OCaml.
>
>> I need some functionality, like AUTOLOAD in Perl,
when class listen all
>> received messages and throws method relative it.
>
> You're asking for (string -> function) dispatch.
This can be implemented 
> using, for example, as association list with
polymorphic variant constructors 
> to represent member names and closures to represent
member bodies:
>
> # let bindings =
>     [`foo, (fun () -> print_endline
"foo");
>      `bar, (fun () -> print_endline
"bar")];;
> val bindings : ([> `bar | `foo ] * (unit ->
unit)) list =
>   [(`foo, <fun>); (`bar, <fun>)]
>
> Invoke the "foo" method:
>
> # List.assoc `foo bindings ();;
> foo
> - : unit = ()
>
> Add a new method:
>
> # let bindings =
>     (`baz, (fun () -> print_endline
"baz")) :: bindings;;
> val bindings : ([> `bar | `baz | `foo ] * (unit
-> unit)) list =
>   [(`baz, <fun>); (`foo, <fun>); (`bar,
<fun>)]
>
> You're asking for something that is fundamentally bad
design in OCaml though. 
> I suspect you should amortise your various methods into
a single OCaml 
> function that accepts an argument (of a variant type)
and pattern matches 
> over it to decide what to do:
>
> # type methods = Foo | Bar | Baz;;
> type methods = Foo | Bar | Baz
> # let invoke = function
>     | Foo -> print_endline "foo"
>     | Bar -> print_endline "bar"
>     | Baz -> print_endline "baz";;
> val invoke : methods -> unit = <fun>
> # invoke Bar;;
> bar
> - : unit = ()
> #
>
> Essentially, what you're asking for has nothing to do
with methods, objects, 
> classes or AUTOLOAD.
>
> -- 
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> Objective CAML for Scientists
> http://www.ffconsultancy.com/products/ocaml_for_scient
ists

Thank you!

This workaround seems very good

-- 
Maxim Treskin
SWsoft Inc.
HSPcomplete Developer
Email: mtreskinswsoft.com
Jabber: Zertjabber.ru
ICQ: 274404840



------------------------ Yahoo! Groups Sponsor
--------------------~--> 
Home is just a click away.  Make Yahoo! your home page now.
http://us.click.yahoo.com/DHchtC/3FxNAA/yQLSAA/saFolB/TM

------------------------------------------------------------
--------~-> 

Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



[1]

about | contact  Other archives ( Real Estate discussion Medical topics )