Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:31:55+00:00 2026-06-13T04:31:55+00:00

I’m trying to implement this: def buildQuery() { val restrictions: ConjunctionRestriction[String, Int] = (name

  • 0

I’m trying to implement this:

def buildQuery() {
  val restrictions: ConjunctionRestriction[String, Int] =
    ("name" is "Some One") and ("age" is 20)
}

implicit def stringToEqualsRestrictionBuilder[T](fieldName: String)
                                                : EqualsRestrictionBuilder[T] =
  new EqualsRestrictionBuilder[T](fieldName)

implicit def restrictionToConjunctionBuilder[L,R](restriction: Restriction[L])
                                                 : ConjunctionBuilder[L,R] =
  new ConjunctionBuilder[L,R](restriction)

case class Restrictions(restrictions: Restriction[_]*)

trait Restriction[T] {
  def toString: String
}

class EqualsRestriction[T](val fieldName: String, val value: T)
    extends Restriction[T] {
  override def toString = fieldName + "=" + value
}

class ConjunctionRestriction[A,B](val lhs: Restriction[A],
                                  val rhs: Restriction[B]) 
    extends Restriction[(A,B)] {
  override def toString = "(" + lhs + ") AND (" + rhs + ")"
}

class EqualsRestrictionBuilder[T](val fieldName: String,
                                  val restriction: Option[Restriction[T]] = None) {

  def is(value: Int) =
    new EqualsRestriction[Int](fieldName, value)

  def is(value: String) =
    new EqualsRestriction[String](fieldName, "\"" + value + "\"")
}

class ConjunctionBuilder[L,R](val lhs: Restriction[L]) {
  def and(rhs: Restriction[R]) = new ConjunctionRestriction[L,R](lhs, rhs)
}

The compiler gives me error:

error: type mismatch;
found   : MyOuterClass.this.EqualsRestriction[Int]
required: MyOuterClass.this.Restriction[R]
val restrictions: ConjunctionRestriction[String, Int] =
  ("name" is "Some One") and ("age" is 20)

I havn’t had the scala type system all figured out. what is wrong with this?

Thanks

EDIT

Fixed by changing ConjunctionBuilder to have only one parameter type L:

class ConjunctionBuilder[L](val lhs: Restriction[L]) {
  def and[R](rhs: Restriction[R]) = new ConjunctionRestriction[L,R](lhs, rhs)
}

implicit def restrictionToConjunctionBuilder[L](restriction: Restriction[L])
                                               : ConjunctionBuilder[L] =
  new ConjunctionBuilder[L](restriction)

But could someone explain why with the R parameter type, the compile fails?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:31:56+00:00Added an answer on June 13, 2026 at 4:31 am

    It fails when ConjunctionBuilder has the R type parameter simply because when applying the implicit conversion restrictionToConjunctionBuilder, the compiler can only infer L (from the parameter restriction).
    The type parameter R does not appear anywhere in the parameter list, so there is no way it can be inferred.
    When type parameters cannot be inferred, you have to pass them explicitly (but of course in the case of implicit conversion, this defeats the purpose).
    By example: the following compiles correctly:

    val restrictions: ConjunctionRestriction[String, Int] =
      (("name" is "Some One"): ConjunctionBuilder[String, Int]) and ("age" is 20)
    

    Short of explictly specifying the type parameters, the compiler cannot bind R, and thus cannot prove that the parameter to the method and is of the right type.
    Indeed, the method expects a Restriction[R], and we give it a Restriction[Int]. This can only match if R == Int, which the compiler can’t prove as R is unbound.

    Your fix is totally correct: move the type parameter R to the definition of and. This way when applying the implicit conversion restrictionToConjunctionBuilder, the compiler can fully infer all the type parameters (that is, the uniqye type parameter L) from the parameter restriction.
    Then, when applying and it can infer R from its parameter rhs

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.