You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Similarly to #539, having a Maybe inside a newtype can cause confusion. The tutorial should explicitly demonstrate an optional newtype (especially because primary keys are optional and wrapping them in newtypes is common).
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
importOpaleyeimportData.Profunctor.Product.THdataUser'idnamesurname=User{pkUser::id
, name::name
, surname::surname}$(makeAdaptorAndInstance "pUser" ''User')
--| Polymorphic user ID typenewtypeUserId'a=UserIda$(makeAdaptorAndInstance "pUserId" ''UserId')
--| User ID field typetypeUserIdField=UserId' (FieldSqlInt4)
typeUserRead=User'UserIdField
(FieldSqlText)
(FieldSqlText)
typeUserWrite=User'
(MaybeUserIdField)
(FieldSqlText)
(FieldSqlText)
userTable::TableUserWriteUserRead
userTable = table "User"
(pUser User { pkUser = pUserId (UserId (tableField "PkUser"))
, name = tableField "Name"
, surname = tableField "Surname"
})
-- We get the error---- • Couldn't match type ‘UserId' a1_00’ with ‘Maybe UserIdField’---- Inference proceeded as follows---- tableField "PkUser" :: InferrableTableField w n r => TableFields w (Field_ n r)---- pUserId (UserId (tableField "PkUser"))-- :: InferrableTableField w n r => TableFields (UserId' w) (UserId (Field_ n r))---- userTable :: InferrableTableField w n r-- => Table (User' (UserId' w) (Field SqlText) (Field SqlText)) UserRead---- But the specified type is---- Table-- (User'-- (Maybe UserIdField)-- (Field SqlText)-- (Field SqlText))-- UserRead---- and (Maybe UserIdField) does not match (UserId' w)-- The fix is to put the `Maybe` inside the `UserId'`:typeUserIdWriteField=UserId' (Maybe (FieldSqlInt4))
typeFixedUserWrite=User'UserIdWriteField
(FieldSqlText)
(FieldSqlText)
fixedUserTable::TableFixedUserWriteUserRead
fixedUserTable = table "User"
(pUser User { pkUser = pUserId (UserId (tableField "PkUser"))
, name = tableField "Name"
, surname = tableField "Surname"
})
```Add newtyped write field example to TutorialBasic
The text was updated successfully, but these errors were encountered:
Issue reported by James Walker, thanks James!
Similarly to #539, having a
Maybe
inside a newtype can cause confusion. The tutorial should explicitly demonstrate an optional newtype (especially because primary keys are optional and wrapping them in newtypes is common).The text was updated successfully, but these errors were encountered: