Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
polymorphic variants
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <mottl@m...>
Subject: Re: polymorphic variants
Followup to my last mail:

Actually, you need not necessarily double decode things (first the network
message, then the user data): you can do this in one step, too, also using
the Marshal-module.

It does not matter whether you know the internals of the user data or not
(when the type is abstract): the Marshal-module will always do the right
thing during decoding - but make sure that you only pass on the user data
to functions of the right type (i.e. ones that can really handle it),
otherwise your program will crash.

E.g. (recv takes the raw string from the network now):

  type admin_args = ...
  type user_data  (* abstract *)

  type argument = MsgA of admin_args * user_data | MsgB of ... | ...

  let recv network_data =
    let msg : argument = Marshal.from_string network_data 0 in
    match msg with
    | MsgA (admin_args, user_data) ->
        handle_admin_args admin_args;
        call_user_program user_data
    | ...

Best regards,
Markus Mottl

Markus Mottl,,