I have this Haskell Code:
module RectangleMover where
data Point = Point Int Int deriving (Show)
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 } deriving (Show)
move :: Rectangle -> Int -> Int -> Rectangle
move r@(Rectangle {corner=c}) dx dy = r { corner = movePoint c dx dy }
p = Point 1 2
hsRec = Rectangle p 10 20
The equivalent C# code for this would be:
class Point
{
private int x;
private int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public void Move(int dx, int dy)
{
this.x += dx;
this.y += dy;
}
}
class Rectangle
{
private Point point;
private int width;
private int height;
public Rectangle(Point p, int width, int height)
{
this.point = p;
this.width = width;
this.height = height;
}
public void Move(int dx, int dy)
{
this.point.Move(dx, dy);
}
}
Point p = new Point(1,2);
Rectangle csRec = new Rectangle(p, 10, 20);
My question now is how to pass the "instance" hsRec Haskell to C# and the csRec from C# to Haskell. The common way in this case would be to create a DLL from Haskell code with the FFI and call this DLL from C#. Also the other way round, creating a DLL from C# and call this DLL from Haskell.
To export from Haskell here is a simple example with integer:
{-# LANGUAGE ForeignFunctionInterface #-}
module Add () where
import Foreign.C.Types
add :: CInt -> CInt -> CInt
add x y = x + y
foreign export ccall add :: CInt -> CInt -> CInt
But how is it possible to pass more complex types between these languages? In this case passing an object of type rectangle. Is it maybe possible to convert an object to a JSON or XML string and pass it over to the other language?