call-haskell-from-anything icon indicating copy to clipboard operation
call-haskell-from-anything copied to clipboard

Exporting own "data" type

Open manzznam opened this issue 9 years ago • 1 comments

I am trying to export a module with own types with the following code:

{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE TemplateHaskell #-}

module RectangleMover where

import Foreign.C
import FFI.Anything.TypeUncurry.Msgpack

data Point = Point Int Int
movePoint :: Point -> Int -> Int -> Point
movePoint (Point x y) dx dy = Point (x + dx) (y + dy)

data Rectangle = Rectangle { corner :: Point, width :: Int, height :: Int }
move :: Rectangle -> Int -> Int -> Rectangle
move r@(Rectangle {corner=c}) dx dy = r { corner = movePoint c dx dy }

foreign export ccall move_export :: CString -> IO CString
move_export :: CString -> IO CString
move_export = export move

But when i try to compile this i get the this error message:

[1 of 1] Compiling RectangleMover ( test.hs, test.o )

test.hs:19:15: No instance for (Data.MessagePack.Object.MessagePack Rectangle) arising from a use of ‘export’ In the expression: export move In an equation for ‘move_export’: move_export = export move

What am i missing? Or does this package support exporting own types at all?

manzznam avatar Jul 03 '16 12:07 manzznam

export :: (MSG.MessagePack (TypeList l), ToTypeList f l r, MSG.MessagePack r) => f -> CString -> IO CString

In other words, the type constituents of f (in other words, the respective types of its inputs and outputs) have to be instances of MessagePack (see the msgpack package). Your Rectangle type is missing such an instance.

abooij avatar Jul 04 '16 17:07 abooij