haskell - Assigning Asc/ Desc to variable later causes a compiler error -


i getting compiler error when try have like:

getorder :: text -> selectopt event getorder text =     case text of         "id"    -> direction eventid         "title" -> direction eventtitle      direction = if text == "id" desc else asc 

if change line, work:

where direction = asc 

same if change case - work:

case text of     "id"    -> desc eventid     "title" -> asc eventtitle 

so question why assigning asc , desc cause compiler error?

make direction function , move top-level:

direction text = if text == "id" desc else asc 

see updown function in answer:

https://stackoverflow.com/a/37380039/866915

update

in order direction eventid make sense, direction has have type:

direction :: entityfield event int 

in order direction eventtitle make sense has have type:

direction :: entityfield event string 

so if direction defined in clause this

where direction = if ... asc else desc 

it can't satisfy both type constraints. however, if make function:

direction t = if t == "id" asc else desc 

then polymorphic. means direction ... can have different types @ different call sites.

update

to use original code, try adding type signature:

where   direction :: entityfield r t -> selectopt r   direction = if text == "id" desc else asc 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -