Foreign Import
There are lot of examples in the lib directory, from which you can easily discover that Erlang code and Hamler code are in the same directory with same filename, when defining a foreign function.
For example, in Data.Eq
we defined Eq
instances for different types. These are done with Erlang Code.
In Eq.erl we have:
eqCharImpl(C1, C2) -> C1 =:= C2.
This is a function we defined to compare two Char
s.
In Eq we can easily import this with:
instance Eq Char where
eq = eqCharImpl
foreign import eqCharImpl :: Char -> Char -> Boolean
The nice thing is that we can give eqCharImpl
a type; however there is no way that Hamler can check this against the actual code you’ve written in Erlang. So when you are doing something not pure, remember to wrap the output with IO.
foreign import readFile :: String -> IO String
当前内容版权归 hamler-lang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 hamler-lang .