scala - Using operators overloading and implicit def for a nice syntax -
i'm trying represent boolean queries in nice way in scala. want able define queries way :
"word1" && "word2" || !"word3"
which means, may guess : find documents containing "word1" , "word2" or not containing "word3".
for purpose, wrote case classes, overloaded required operators , created implicit conversions (i removed irrelevant class code) :
abstract class query { def &&(q : query) = new and(this,q) def ||(q : query) = new or (this,q) def unary_! = new not(this) } object query { implicit def tolitteral(s : string) = new litteral(s) } case class litteral(term : string) extends query case class and(q1 : query, q2 : query) extends query case class or (q1 : query, q2 : query) extends query case class not(q : query) extends query
however, when phone call booleansearch(q : query)
method, conversion not go well. example, booleansearch(!"word")
not compile, saying unary_!
not fellow member of string
. same goes litteral
conversion not succeed. possible address or should give on having such nice expressions ?
scala operator-overloading implicit-conversion
No comments:
Post a Comment