[Haskell-beginners] Haskell-style types in C or C++
Alexander Batischev
eual.jp at gmail.com
Sat Jul 30 21:58:55 CEST 2011
Hi!
On Sat, Jul 30, 2011 at 10:45:06AM -0800, Christopher Howard wrote:
> One of the things that I love about Haskell is the syntax for
> creating user defined types. E.g.:
>
> Data QueryResult = NoResult | DatabaseError String | Results [String]
>
> I can prototype an entire program around these self-made types and
> it is a lot of fun. Out of intellect curiosity, though: what would
> be the equivalent construction in C or C++? (Say, for the type
> defined above).
Recently, same question occurred to me too, and the only solution I
could think of is defining base class (analog of data type name,
QueryResult in your case) and creating subclasses for each of data type'
constructors (NoResult, DatabaseError, Results). And you should resort
to templates if you want something like [a] as a parameter.
Google provides few nice links [1] [2] which may be helpful.
Just for your information, types like in the example above are called
Algebraic Data Types (ADT).
1. http://cpp-next.com/archive/2010/09/algebraic-data-types-in-c/
2. http://stackoverflow.com/questions/16770/haskells-algebraic-data-types
--
Regards,
Alexander Batischev
1024D/69093C81
F870 A381 B5F5 D2A1 1B35 4D63 A1A7 1C77 6909 3C81
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110730/52855a39/attachment.pgp>
More information about the Beginners
mailing list