Comments (4)
CStorable
is intended to represent the way to access in memory the C analog of a given type.
As far as I know, C does not actually support true sum types, thus the lack of support.
It might be sensible to write a CStorable
instance for sum types in the case where all constructors of the type took no arguments (by translation into an enum).
Is that what you were thinking of, or have I missed the mark? (Sorry for the slow response)
from c-storable-deriving.
well, i think it would be more useful to have something like
data Thing = Foo { foo1 :: CInt} | Bar { bar1 :: CChar }
becomes two c types, and a union for the fields like so
enum ThingType {
thingTypeFoo,
thingTypeBar
};
/* for missing field names just append a number to the data constructor made lower case */
union ThingFieldsUnionTag {
int foo1;
char bar1;
};
typedef union ThingFieldsUnionTag ThingFields;
struct ThingStructTag {
ThingType type;
ThingFields fields;
};
typedef struct ThingStructTag Thing;
The idea is to have a common representation that works, like i said, it doesn't necessarily have to be implemented in the library, but it just would be nice to at least have an example documenting how to do this.
[Edit: fixed accidental haskell type signature in c code. But also, this example is not general, see next comment for a better canonical c sum-type representation.]
from c-storable-deriving.
A more general example, with multiple fields:
data Thing = Foo CInt CChar CInt | Bar CChar CInt | Baz
becomes in c:
enum ThingType {
thingTypeFoo,
thingTypeBar,
thingTypeBaz
};
struct FooStructTag {
int foo1;
char foo2;
int foo3;
};
typedef struct FooStructTag Foo;
struct BarStructTag {
char bar1;
int bar2;
};
typedef struct BarStructTag Bar;
struct BazStructTag {
};
typedef struct BazStructTag Baz;
union ThingFieldsUnionTag {
Foo foo;
Bar bar;
Baz baz;
};
typedef union ThingFieldsUnionTag ThingFields;
struct ThingStructTag {
ThingType type;
ThingFields fields;
};
typedef struct ThingStructTag Thing;
[Edit: removed obsolete comment, from cut and paste]
from c-storable-deriving.
Sorry I've been so slow to respond.
I'm fully aware that it's possible to represent a union in C by type tagging a union. However, that's not the point of CStorable
. CStorable
is for typing out the direct analog of a C type and being able to interact with the C representation. What you're describing sounds like it'd make sense as an implementation strategy for Storable
, possibly using Generic
to allow your implementation to be easily plugged in for a given type.
I think the miscommunication here is that CStorable
is not for "here is a way of representing this in C", it is for "there is already a C representation for this, how do I gain the ability to read/write it in Haskell". While the type tagged union is a common pattern in C, unless you're seeing this exact formulation in a bunch of APIs (as otherwise it'd be incompatible) I don't think this library, or even the typeclass CStorable
is the right place for it.
from c-storable-deriving.
Related Issues (6)
- Working with StablePtr HOT 1
- StorableWrap is useless HOT 2
- Wrong offset in datatype with many fields HOT 1
- Is CStorable really necessary? HOT 1
- Use example HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c-storable-deriving.