[Cffi-devel] Fw: Casting variables and functions

Joeish W joeish80829 at yahoo.com
Sun Mar 30 15:14:27 UTC 2014


I was just wondering why cffi doesn't do automatic conversions.  Since casting is used so much in c I  think it would do automatic conversions...Do experts in CFFI normally use this approach,  do they cast on the Lisp side or use another approach..I'd like to start learning the best way to do things

On , Joeish W <joeish80829 at yahoo.com> wrote:
 
On Sunday, March 30, 2014 7:53 AM, Joeish W <joeish80829 at yahoo.com> wrote:
> 
>I was just wondering why cffi doesn't do automatic conversions.  Since casting is used so much in c I  think it would do automatic conversions...Do experts in CFFI normally use this approach,  do they cast on the Lisp side or use another approach..I'd like to start learning the best way to do things
>>
>>On Sunday, March 30, 2014 7:45 AM, Stelian Ionescu <sionescu at cddr.org> wrote:
>> 
>>On Sun, 2014-03-30 at 07:18 -0700, Joeish W wrote:
>>>> Thanks for getting back to me., I really appreciate it
>>>
>>>> Using coerce  though I'm already casting it to a float on the lisp
>>>> side...I was wondering if I can cast a 4 to a float with CFFI and have
>>>> the output be a 4.0f0 like it works in C...without doing anything on
>>>> the Lisp side
>>>
>>>CFFI doesn't do automatic coercions, but you can do it with a custom
>>>type:
>>>
>>>(define-foreign-type float+int () ()
>>>  (:actual-type :float)
>>>  (:simple-parser float+int))
>>>
>>>(defmethod expand-to-foreign (value (type float+int))
>>>  `(coerce ,value 'float))
>>>
>>>(with-foreign-object (val :float)
>>>  (setf (mem-ref val 'float+int) 4)
>>>  (mem-ref val :float))
>>>
>>>
>>>-- 
>>>Stelian Ionescu a.k.a. fe[nl]ix
>>>Quidquid latine dictum sit, altum videtur.
>>>
>>>
>>>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20140330/81ef8a7f/attachment.html>


More information about the cffi-devel mailing list