[Haskell-beginners] calling virtual function from base class in FFI (using type classes)

Nathan Hüsken nathan.huesken at posteo.de
Wed Dec 19 22:42:02 CET 2012


Hey,

I am trying to write FFI bindings for a C++ class structure.

There is a base class with a bunch of (virtual) functions:

class Base {
   virtual void func1();
   virtual void func2();
}

Now I have many classes that derive from the base class, and I want to 
be able to call func1 and func2 on them.
But I do not want to write an ffi function for every derived object and 
every virtual function in base class. So I am trying:

data DerivedStruct
type Derived = Ptr DerivedStruct

class BaseFunc a

instance BaseFunc DerivedStruct

foreign import ccall "callBaseFunc1" func1 :: BaseFunc a => Ptr a -> IO ()

ghc gives me:

     Unacceptable result type in foreign declaration:
       BaseFunc a => Ptr a -> IO ()
     When checking declaration:
       foreign import ccall safe "static callBaseFunc1" func1
         :: BaseFunc a => Ptr a -> IO ()

a) I do not get the error message. The return type is acceptable? Why is 
IO () not acceptable?
b) How can I structure this?

Thanks!
Nathan



More information about the Beginners mailing list