Closed NicolasDP closed 8 years ago
Great idea to do some work on this area. A few notes:
Foreignable
is not a great word, as a native speaker. It just sounds a bit wrong.Storable
instead of Foreignable
, and StorableFixed
instead of Storable
?class StorableFixed a => PrimType a
?size
and alignment
took proxy a
instead of Proxy a
then Ptr
would already be a valid proxy. The downside is I think it has issues with newtype deriving (not sure if they can be worked around or not).I'm looking forward to this work.
I agree Foreignable
is not a good choice.
What about SizedStorable
? I updated the Proposal and added more comments and precisions.
I think you are right that PrimType
could have SizedStorable
as constraint.
We could also remove primSizeInBytes
which is only the SizedStroable
's function size
.
I think StorableFixed
convey better the idea of fixed size than SizedStorable
.
For PrimType
, this is not related to Storable
. I don't think it's a good idea to link the concept
Overview
Foundation aims to bring better typed object representation in order to help users to quickly understand a given API, but also to gives better type checking and therefor semantic validation at compile time.
This is a draft proposal on what could look like a new interface for Foreign Storable.
Foreign interface: The state of the art
Storable object (object which can be marshalled to/from foreign interfaces, but not only) are originally defined as follow:
This type class seems to provide what one would need to safely marshall objects to/from a given address.
Limitations of the current state
sizeOf
,alignment
andp***ElemOff
), this is not the culture of theFoundation
to leave such non labeled size and offsets;peekElemOff
ispeekByteOff ptr (off * sizeOf undef)
);p***ElemOff
may not apply for certain types).New storable type classes
Storable type class
Without having any information about the size of a given type, we want to be able to peek or poke an object from a given pointer. In this case, the
sizeOf
information is not relevant. A given object can have a variable size and yet bepeeked
orpoked
at a given address.Some primitives already interface this
Storable
:Word8
,Int
... PrimType type classAn example of object that can be shared with foreign libraries is a CString. It is litterally an array of
Word8
terminated by a zero.There are also C Structure of variable size which can be defined:
In this case, the structure will have a dynamically known size that
peek
orpoke
can easily implements:A
SizedStorable
is aStorable
element which can be peeked/poked from/to an offset and an address in memory and to do so, the only information we need is its size and its alignment.And now it is very simple to define generic function using the 2 given interfaces.
Compatibility with prim types