
On 27 April 2011 21:28, Alexander Solla
On Wed, Apr 27, 2011 at 11:16 AM, John Obbele
wrote: Second issue, I would like to find a way to dispatch parsers. I'm not very good at expressing my problem in english, so I will use another code example:
This sounds very hard in the general case. Others have shown you how to dispatch on two types. But there is no general data type which combines all (or even arbitrarily many) types. Somehow, "Read" is able to do this, but I don't know what kind of magic it uses.
Read always "demands its type" so it doesn't use any magic - if the input string doesn't conform it will throw an error. Any sensible binary format will have a scheme such as tag byte prefixes to control choice in parsing (binary parsing generally avoids all backtracking). If your binary data doesn't have a proper scheme it will be hard to parse for any language (or cast-to in the case of C), so the most sensible answer is to revise the format.