New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should === use the type of the left hand side and ignore the type of the right hand side? #1986

Closed
tanin47 opened this Issue Feb 5, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@tanin47
Copy link

tanin47 commented Feb 5, 2019

I have a set of Enums implemented with sealed abstract class as shown below:

object Country {
  sealed abstract class Value
  object Thailand extends Value
  object Singapore extends Value
}

// And here's the model for the database
case class Campaign(country: Country.Value)
class CampaignTable(tag: Tag) extends Table[Campaign](tag, "campaigns") {

  def country = column[Country.Value]("country")

  def * = (
    ...
    country,
    ...
  ) <> ((Campaign.apply _).tupled, Campaign.unapply)
}

Then, I define BaseColumnType[Country.Value]. This seems to work until I need to use, for example, Country.Thailand in a where clause as shown below:

query.filter(_.status === Country.Thailand)

The compilation error is:

Cannot perform option-mapped operation
[error]       with type: (Country.Value, Country.Thailand.type) => R
[error]   for base type: (Country.Value, Country.Value) => Boolean

It seems that somehow Country.Thailand.type is being used for finding an implicit conversion and etc.

There are a few workarounds for the code above:

query.filter(_.status === Country.Thailand.asInstanceOf[Country.Value])
// or
query.filter(_.status.===[Country.Value, Country.Value](Country.Thailand))

I wonder if if makes sense to depends on the left hand side's type instead. Also, is there a cleaner workaround?

@tanin47 tanin47 changed the title [Suggestion] should === use the type of the left hand side and ignore the type of the right hand side? should === use the type of the left hand side and ignore the type of the right hand side? Feb 5, 2019

@tanin47 tanin47 changed the title should === use the type of the left hand side and ignore the type of the right hand side? Should === use the type of the left hand side and ignore the type of the right hand side? Feb 5, 2019

@hvesalai

This comment has been minimized.

Copy link
Member

hvesalai commented Feb 6, 2019

What you are missing is an OptionMapper such as this:

    implicit def getOptionMapper2TT[B1, B2 <: Country.Value : BaseTypedType, P2 <: B2, BR] =
      OptionMapper2.plain.asInstanceOf[OptionMapper2[B1, B2, BR, B1, P2, BR]]

@hvesalai hvesalai closed this Feb 6, 2019

@tanin47

This comment has been minimized.

Copy link
Author

tanin47 commented Feb 6, 2019

It works well. Thank you very much !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment