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@yahoo.com wrote:
On Sunday, March 30, 2014 7:53 AM, Joeish W joeish80829@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@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.
On Sun, 2014-03-30 at 08:14 -0700, Joeish W 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
There is no other approach to use. You must always pass the right type to CFFI, and do type coercions beforehand either explicitly or implicitly through custom types.
On Sun, 2014-03-30 at 08:14 -0700, Joeish W wrote:
I was just wondering why cffi doesn't do automatic conversions.
One reason may be that the FFI of the underlying Lisps (some? all?) don't do automatic conversions. They signal an error instead. If some library always wraps values with additional calls like (COERCE ,X 'FLOAT), it'll cost performance in general. Advanced type-inferencing compilers like cmucl/sbcl might be able to eliminate that extra call, depending on context. But there's no reason to let CFFI impose an extra performance burden on other implementations.
Of course, that implies that mismatching int and float in "portable CFFI code" is unspecified, because the behaviour of the underlying FFI is -- presumably -- not the same across all Lisps. Some definitely signal an error, perhaps some will silently coerce.
Regards, Jörg Höhle