[Haskell-beginners] Haskell-style types in C or C++

Christopher Howard christopher.howard at frigidcode.com
Sat Jul 30 22:55:34 CEST 2011


On 07/30/2011 11:58 AM, Alexander Batischev wrote:
> 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
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

Thank you, this puts me on the right track. The above links were rather 
difficult to follow, but the following article has been pretty good so far:

http://cpp-next.com/archive/2010/07/algebraic-data-types/

-- 
frigidcode.com
theologia.indicium.us



More information about the Beginners mailing list