
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). -- frigidcode.com theologia.indicium.us

You should look into the Union datatype. It has been a long while since I coded in C but I believe it allowed you to define a struct-like datatype which could contain exactly 1 value from a list of datatypes. I found a forum post about it: http://www.go4expert.com/forums/showthread.php?t=15 Hope that helps. On Sat, Jul 30, 2011 at 11:45 AM, Christopher Howard < christopher.howard@frigidcode.com> 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).
-- frigidcode.com theologia.indicium.us
______________________________**_________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners
-- Michael Xavier http://www.michaelxavier.net LinkedIn http://www.linkedin.com/pub/michael-xavier/13/b02/a26

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

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@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

typedef enum { QueryResultTypeNoResult, QueryResultTypeDatabaseError, QueryResultTypeResults } QueryResultType; typedef union { char *error; char **results; } QueryResultData; typedef struct { QueryResultType type; QueryResultData data; } QueryResult Or something to that effect… Bob On 30 Jul 2011, at 19:45, 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).
-- frigidcode.com theologia.indicium.us
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

For: Data QueryResult = NoResult | DatabaseError String | Results [String] A close approximation I can think of is: enum {NoResult, DatabaseError, Results}; struct QueryResult { int type; union { char* error_msg; struct { char** msgs; int len; } results; }; }; https://gist.github.com/1116058 is a simple use case of the code I suggested. I am not sure there is only 1 solution to this though. On Sat, Jul 30, 2011 at 1:45 PM, Christopher Howard < christopher.howard@frigidcode.com> 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).
-- frigidcode.com theologia.indicium.us
______________________________**_________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners
-- John Harrison

Christopher Howard
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).
I think, the closest and cleanest translation is a set of four classes, one QueryResult superclass with subclasses NoResult, DatabaseError and Results. This also gives you something resembling Haskell's value constructors, although not for free. Finally it helps with separation of concerns, when you write accessor functions. You don't get pattern matching, but you can get combinators like 'maybe' for Maybe or 'either' for Either. Unions work, too, but they are not nearly as clean. In general, avoid unions in C++, even though they seem to be a natural way to express ADTs. They really are not. Class hierarchies are the way to go. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

On 07/31/11 15:58, Ertugrul Soeylemez wrote:
Unions work, too, but they are not nearly as clean. In general, avoid unions in C++, even though they seem to be a natural way to express ADTs. They really are not. Class hierarchies are the way to go.
Or boost::variant. It's actual algebraic data types, at the expense of mighty weird syntax for pattern-matching because the Boost developers had to come up with something that compiled in C++. ~Isaac
participants (7)
-
Alexander Batischev
-
Christopher Howard
-
Ertugrul Soeylemez
-
Isaac Dupree
-
John Harrison
-
Michael Xavier
-
Thomas Davie