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

Add Fail constraints for instances which don't belong? #49

Closed
chexxor opened this Issue Dec 24, 2016 · 5 comments

Comments

Projects
None yet
4 participants
@chexxor
Copy link
Contributor

chexxor commented Dec 24, 2016

As mentioned in previous issues, like #30, this library shouldn't include instances for common PS types, instead only instances for JS-native types.

To prevent creation of more issues asking for instances like these, should we add a Fail constraint, as described in paf31/24-days-of-purescript-2016 21. Custom Type Errors? I'd like to add one for Maybe, where the error message instructs to use the Null or Undefined newtype instead.

@paf31

This comment has been minimized.

Copy link
Member

paf31 commented Dec 24, 2016

Actually, I would prefer to remove the IsForeign and AsForeign classes entirely, and just use combinators instead. The reason is related to what you brought up - in most cases of ADTs, there isn't a single good decoder/encoder pairs.

Either that, or we should require that IsForeign decodes its arguments without changing representation, in the sense that toForeign >>> read = id.

@garyb

This comment has been minimized.

Copy link
Member

garyb commented Dec 24, 2016

I'd agree with removing AsForeign, since it's redundant given there's a toForeign that accepts any type. Also agree with the IsForeign law, as per other comment.

@chexxor

This comment has been minimized.

Copy link
Contributor

chexxor commented Jan 7, 2017

I'd like to further motivate the use of combinators instead of classes. This library is in the realm of parsers, parsing data from foreign data, and I believe parsers normally don't rely on unique type classes.

@chrisdotcode

This comment has been minimized.

Copy link

chrisdotcode commented Jun 3, 2017

Hi guys,

Now that {Is,As}Foreign is removed, it's (still) possible to decode data from foreign types using the combinators in purescript-foreign{-generic} (readProp, read{String,Int,etc.}, and the Decode class (although technically the decode class might be intended for use with generics, it works fine to implement foreign instances for other types)), but what combinators are available for converting to Foreign types? We have toForeign, but how do you create non-primitive objects? I don't remember the exact code, but before you used to be able to do something like:

person = writeObject
    [ "name" .= "chris"
    , "age" .= 100
    ]

Is there any current replacement for the above? (I searched Pursuit and couldn't find anything not relating to React that did this) If not, I'd be willing to implement what's needed myself/create another package. Thanks!

@garyb

This comment has been minimized.

Copy link
Member

garyb commented Jun 3, 2017

Foreign is only really intended for sanitising incoming JS/FFI values, so in toForeign <<< myFromForeignFunction should be id essentially - if you want to encode data out of PS too in alternative format then purescript-argonaut is more what you're looking for (although it's restricted to JSON). :)

-foreign-generic doesn't quite stick to those rules, if I remember rightly, but that's partially why it's not in the core libraries. 😄

I guess the other option in the above would be to encode just by rewriting as the desired record and the toForeigning that:

person = toForeign { name: "chris", age: 100 }

Although it is a little "risky", as you're not forced to encode all the values involved.

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