-
Notifications
You must be signed in to change notification settings - Fork 41
Add a class to create Foreign types #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Allows specifying the exact format of JavaScript values for FFI code:
data A = A (Maybe String) (Maybe String)
instance aAsForeign :: AsForeign A where
write (A x y) = writeObject [ "x" .= Null x
, "y" .= Null y
]
foreign import data B :: *
foreign import convertImpl :: Foreign -> B
convert :: A -> B
convert = convertImpl <<< asForeign
Fixes purescript#31
| readArray value | isArray value = pure $ unsafeFromForeign value | ||
| readArray value = Left (TypeMismatch "array" (tagOf value)) | ||
|
|
||
| newtype Prop = Prop { key :: String, value :: Foreign } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to use a non-record type here, as data constructors with multiple fields perform better than records in most JS VMs (cheaper to construct due to the "hidden class" optimisation). Maybe just a type Prop = Tuple String Foreign even?
edit: as pointed out on IRC, Prop is destined for the FFI, so a record works better here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this and/or writeObject get comments (ideally with an example) please?
|
Ping @paf31. Any comments here or shall I merge this? |
| instance intAsForeign :: AsForeign Int where | ||
| write = toForeign | ||
|
|
||
| instance arrayAsForeign :: (AsForeign a) => AsForeign (Array a) where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: redundant parens here.
|
Looks great 👍 |
|
Sorry about the delay in merging @puffnfresh, it's out now as 1.1.0. |
Allows specifying the exact format of JavaScript values for FFI code:
Fixes #31