Skip to content
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 an example on how to use lenses #22

Open
legrostdg opened this issue Feb 8, 2017 · 7 comments
Open

add an example on how to use lenses #22

legrostdg opened this issue Feb 8, 2017 · 7 comments

Comments

@legrostdg
Copy link

I can't get how to use the lenses generated by purescript-bridge...

Let's suppose I have the following haskell types defined:

data Book = Book { author :: Author, pages :: Int }
data Author = Author { name :: String }

given a purescript Book:

let a = { name: "James" }
let b = { author: a, pages: 100}

How can I access to the number of pages, and the author name?
I would expect something like this:

let pages = b ^. _pages
let name = b ^. _author ^. _name

but it does not seem to work...

@eskimor
Copy link
Owner

eskimor commented Feb 8, 2017

lenses are only generated if you prefix your fields with _, just like in haskell. So your record should be:

_author :: Author

and the lens access:

let pages = b^.pages

@legrostdg
Copy link
Author

legrostdg commented Feb 8, 2017

So, I have to use lenses with haskell if I want to use them with purescript?

@eskimor
Copy link
Owner

eskimor commented Feb 8, 2017

Actually no, but yes the intention is to have a more seamless translation. If you had lenses in Haskell you have them in PureScript too.

No because you don't have to have lenses in haskell, all you need is to prefix your field names with an underscore.

Alternatively if you really want lenses and prefixes in PS but not in Haskell you can add the prefixes by providing your own custom bridge.

@safareli
Copy link

what do you mean by "providing your own custom bridge"?
like if you have data Foo = Foo {fooName:: String} in haskell, what can be done so in generated code you have data Foo = Foo {name:: String}

@eskimor
Copy link
Owner

eskimor commented Jul 19, 2018

Hehe, sorry that suggestion did not receive too much thought ;-) You are right, you can't change record field names by means of a custom bridge, as bridges only translate types. If you want something like this, you would need to modify the SumTypes accordingly before passing them to writePSTypes. Check out RecordEntry in DataConstructor in SumType.

Simply modify the _recLabels accordingly. One thing though, if you do translations like this, the generic encodings will be incompatible by default, so you will have to take care of that too.

I hope that helps!

@safareli
Copy link

Thanks!

@peterbecich
Copy link
Contributor

Here is an example using view on generated Purescript types:

<> "\t Foo list length: "
<> (show (length $ view fooList foo :: Int))
<> "\t Foo map size: "
<> (show (Object.size $ view fooMap foo :: Int))

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

No branches or pull requests

4 participants