Using the operators you gave on your example, it's just a matter of providing an explicit type signature. The following works fine:
expr = \mp -> case dynamicQueryField dc of
DynFieldName -> (mp ^. ItemName) `op` val value
DynFieldInserted -> (mp ^. ItemInserted) `op` val dateValue
DynFieldActive -> (mp ^. ItemActive) `op` val boolValue
op :: PersistField a => SqlExpr (Value a) -> SqlExpr (Value a) -> SqlExpr (Value Bool)
op = case dynamicQueryOp dc of
SqlBinOpEq -> (==.)
SqlBinOpNotEq -> (!=.)
SqlBinOpGtEq -> (>=.)
SqlBinOpLtEq -> (<=.)
SqlBinOpLike -> unsafeSqlBinOp " ILIKE "
If any of the operators had more constraints on its arguments (e.g., Num a
), then the approach above would force the whole op
to have the union of all constraints.