25 Jul 2000 08:56:09 GMT, Marcin 'Qrczak' Kowalczyk <[EMAIL PROTECTED]> pisze:
> > and why do you pass what seem to be conversion functions as
> > parameters, do they not follow from the types?),
>
> Not always, e.g. char * may be in, out, or in/out parameter.
> It will get hairy with Unicode.
>
> But maybe the combination of original and marshalled type, together
> with more explicit treatment of out parameters, would allow convenient
> overloading. I must try, fortunately types are usually explicit.
As I am thinking about it, it could make sense.
There was a problem that passing [a] to a foreign function expecting
Ptr [a] (Ptr is a newtyped Addr), where a is any Storable type,
can be done in several ways. Generally this might be simply an
array with length explicitly passed somewhere else, but strings are
'\0'-terminated and some arrays of pointers are NULL-terminated.
This can be solved by providing more types. Instead of using Ptr String
as a type of a foreign parameter, a newtype CString or the like can
express that it should be '\0'-terminated.
A problem is that there cannot exist both
instance ForeignArg A b
instance ForeignArg a B
because they are overlapping. It means that all instances must use a
concrete top level type constructor in the first type. Or the second.
The foreign type looks more appropriate for being fixed, because
instance Integral a => ForeignArg a Int32
(where the first type is for Haskell and the second is foreign) makes
more sense than the opposite: the set of foreign types is more limited.
It still requires explicit mentioning of all foreign types and their
supported newtypes, but maybe it's not that bad. It excludes
instance ForeignArg Bool Int32
We can live with it.
Out parameters would require explicit allocation of a memory and
passing the raw pointer, thanks to
instance ForeignArg (Ptr a) (Ptr a)
Fortunately
instance Storable a => ForeignArg a (Ptr a)
is not considered overlapping with the former, so passing structures
by pointer would be supported. Oops, but
instance Storable a => ForeignArg [a] (Ptr [a])
is overlapping, so I have to throw away the idea that Ptr [a]
expresses a pointer to an array of a :-( It must be
instance Storable a => ForeignArg [a] (Ptr a)
I'll try to use these ideas, but I'm afraid that overlapping instances
will bite from time to time.
--
__("< Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
\__/ GCS/M d- s+:-- a23 C+++$ UL++>++++$ P+++ L++>++++$ E-
^^ W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP+ t
QRCZAK 5? X- R tv-- b+>++ DI D- G+ e>++++ h! r--%>++ y-