C# Haskell:没有自己类型的实例
我正在尝试使用“call haskell to anything”包将自己的类型导出到C#,代码如下:C# Haskell:没有自己类型的实例,c#,haskell,instance,ffi,C#,Haskell,Instance,Ffi,我正在尝试使用“call haskell to anything”包将自己的类型导出到C#,代码如下: {-# LANGUAGE ForeignFunctionInterface #-} {-# LANGUAGE TemplateHaskell #-} module RectangleMover where import Foreign.C import FFI.Anything.TypeUncurry.Msgpack data Point = Point Int Int movePoin
{-# 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
当我尝试编译此文件时,会收到错误消息:
[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
test.hs:13:10:
Expected a constraint, but ‘Rectangle’ has kind ‘*’
In the instance declaration for ‘Rectangle’
所以我想我必须为“矩形”类型写一个“实例”。但正确的语法是什么?我已经尝试添加:
instance Rectangle where
但收到了以下错误消息:
[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
test.hs:13:10:
Expected a constraint, but ‘Rectangle’ has kind ‘*’
In the instance declaration for ‘Rectangle’
首先,您需要导入
Data.MessagePack.Object
。这将使类MessagePack
进入范围
然后你需要说:
instance MessagePack Rectangle where
toObject rect =
-- Insert your implementation here, returning an Object
fromObject obj =
-- Insert your implementation here, returning a Maybe Rectangle
将其与的文档进行比较。首先,您需要导入
Data.MessagePack.Object
。这将使类MessagePack
进入范围
然后你需要说:
instance MessagePack Rectangle where
toObject rect =
-- Insert your implementation here, returning an Object
fromObject obj =
-- Insert your implementation here, returning a Maybe Rectangle
将其与的文档进行比较。最后,我在ErikR()的帮助下解决了这个问题。 以下是最终代码:
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE DeriveGeneric #-}
module RectangleMover where
import Foreign.C
import FFI.Anything.TypeUncurry.Msgpack
import qualified Data.MessagePack as MP
import Data.MessagePack.Object
import Data.MessagePack.Aeson
import Data.Aeson
import GHC.Generics
instance ToJSON Point
instance FromJSON Point
data Point = Point Int Int deriving (Generic, Show)
movePoint :: Point -> Int -> Int -> Point
movePoint (Point x y) dx dy = Point (x + dx) (y + dy)
instance ToJSON Rectangle
instance FromJSON Rectangle
data Rectangle = Rectangle { corner :: Point, width :: Int, height :: Int } deriving (Generic, Show)
move :: Rectangle -> Int -> Int -> Rectangle
move r@(Rectangle {corner=c}) dx dy = r { corner = movePoint c dx dy }
toMsgPack :: Rectangle -> Maybe MP.Object
toMsgPack = decode . encode
p = Point 1 2
rect = Rectangle p 10 20
test = toMsgPack (rect)
最后,我在ErikR()的帮助下解决了这个问题。 以下是最终代码:
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE DeriveGeneric #-}
module RectangleMover where
import Foreign.C
import FFI.Anything.TypeUncurry.Msgpack
import qualified Data.MessagePack as MP
import Data.MessagePack.Object
import Data.MessagePack.Aeson
import Data.Aeson
import GHC.Generics
instance ToJSON Point
instance FromJSON Point
data Point = Point Int Int deriving (Generic, Show)
movePoint :: Point -> Int -> Int -> Point
movePoint (Point x y) dx dy = Point (x + dx) (y + dy)
instance ToJSON Rectangle
instance FromJSON Rectangle
data Rectangle = Rectangle { corner :: Point, width :: Int, height :: Int } deriving (Generic, Show)
move :: Rectangle -> Int -> Int -> Rectangle
move r@(Rectangle {corner=c}) dx dy = r { corner = movePoint c dx dy }
toMsgPack :: Rectangle -> Maybe MP.Object
toMsgPack = decode . encode
p = Point 1 2
rect = Rectangle p 10 20
test = toMsgPack (rect)
我不熟悉这个库,但它应该类似于
实例Data.MessagePack.Object.MessagePack矩形,其中..
,现在我得到:test.hs:13:10:不在范围内:类型构造函数或类'Data.MessagePack.Object.MessagePack'
您可能需要导入Data.MessagePack.Object
并使用实例MessagePack矩形,其中
我不熟悉库,但它应该类似于实例Data.MessagePack.Object.MessagePack矩形,其中…
。我尝试了这个,现在我得到:test.hs:13:10:不在范围内:类型构造函数或类'Data.MessagePack.Object.MessagePack'
您可能需要导入Data.MessagePack.Object
并使用实例MessagePack矩形,其中
不幸的是,我对Haskell很陌生。在这种情况下,toObject和fromObject的实现是什么?请查看Data.MessagePack.Object中实例的源代码,了解一些想法。它是从文档页面链接的。不幸的是,我对Haskell很陌生。在这种情况下,toObject和fromObject的实现是什么?请查看Data.MessagePack.Object中实例的源代码,了解一些想法。其链接来自文档页面。