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

makePropertyObj code does not work and left undefined, 13.3. USING GENERIC METADATA, page 183 #33

Open
salimp2009 opened this issue Aug 27, 2022 · 2 comments

Comments

@salimp2009
Copy link

Hi,
I tried the code for makePropertyObj at 13.3. USING GENERIC METADATA, page 183
it gives type error. Also checked the code on github it is left as undefined

the original code is (shown as on the book)

makePropertyObj :: forall name . (KnownSymbol name) => Value -> Value
makePropertyObj v = object [pack (symbolVal $ Proxy @name) .= v ]

I noticed the error was due to Haskell could not infer the type as Key from the code pack (symbolVal $ Proxy @name) and was giving below error (which worked for makeTypeObj function)

• Couldn't match expected type ‘Key’ with actual type ‘Text’
• In the first argument of ‘(.=)’, namely
    ‘pack (symbolVal $ Proxy @name)’
  In the expression: pack (symbolVal $ Proxy @name) .= v
  In the first argument of ‘object’, namely
    ‘[pack (symbolVal $ Proxy @name) .= v]’typecheck(-Wdeferred-type-errors)

I made some small changes and Key to import list from aeson package also used fromText and imported Data.Aeson.Key (fromText) and used fromText to convert Text to key and it works i believe if it is the intent;

here is the slightly modified code;

makePropertObj :: forall name . KnownSymbol name => Value -> Value
makePropertObj v = object [fromText (pack (symbolVal $ Proxy @name)) .= v ]

and just did a quick check on it if it works as it is

-- >>>makePropertObj @"age" (makeTypeObject @Int)
-- Object (fromList [("age",Object (fromList [("type",String "integer")]))])

also the changes on the import list I made ;

import Data.Aeson (Value (..), (.=), object, Key)
import Data.Aeson.Key (fromText)
@isovector
Copy link
Owner

I think this must be bit-rotten, unfortunately! Another thing to tackle if a second edition ever happens. Thanks for the report!

@salimp2009
Copy link
Author

I think this must be bit-rotten, unfortunately! Another thing to tackle if a second edition ever happens. Thanks for the report!

i started learning Haskell couple of months ago . your book really helped me a lot to get into Type level programming but a newer version would be awesome.
Learning so much from you.. Thank you so much

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

2 participants