Skip to content

Conversation

@puffnfresh
Copy link
Contributor

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 #31

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 }
Copy link
Member

@garyb garyb Jul 19, 2016

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.

Copy link
Contributor

@paf31 paf31 Jul 28, 2016

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?

@garyb
Copy link
Member

garyb commented Jul 26, 2016

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: redundant parens here.

@paf31
Copy link
Contributor

paf31 commented Jul 28, 2016

Looks great 👍

@garyb
Copy link
Member

garyb commented Aug 9, 2016

Sorry about the delay in merging @puffnfresh, it's out now as 1.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants