Skip to content

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

@salimp2009

Description

@salimp2009

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions