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 8628731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:37:20+00:00 2026-06-12T08:37:20+00:00

Recently I stumbled across a strange (to me) compiler error message. Consider the following

  • 0

Recently I stumbled across a strange (to me) compiler error message. Consider the following code:

trait Foo {
  type Res <: Foo
  type Bar[X <: Res]
}

class MyFoo extends Foo {
  override type Res = MyFoo
  override type Bar[X <: Res] = List[X]
}

type FOO[F <: Foo, R <: Foo, B[_ <: R]] = F { type Res = R; 
                                              type Bar[X <: R] = B[X] }

def process[F <: Foo, R <: Foo, B[_ <: R]](f: FOO[F, R, B]) {}

Now, if I want to call the process method I have to explicitly write the type parameters:

process[MyFoo, MyFoo, List](new MyFoo) // fine

If I write:

process(new MyFoo)

or

process((new MyFoo): FOO[MyFoo, MyFoo, List])

I get the following error message:

inferred kinds of the type arguments (MyFoo,MyFoo,List[X]) do not conform to the expected kinds of the type parameters (type F,type R,type B). List[X]’s type parameters do not match type B’s expected parameters: class List has one type parameter, but type B has one

Why isn´t the compiler able to infer the types (although I explicitly stated them at call parameter)? And what does that class List has one type parameter, but type B has one mean? Something has one, but the other has also one, and that´s why they don´t fit together???

  • 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-12T08:37:21+00:00Added an answer on June 12, 2026 at 8:37 am

    If we look to the Scala compiler, the sources could help us understanding what the problem is. I have never contributed to the Scala compiler, but I found the sources very readable and I have already investigated on that.

    The class responsible for type inference is scala.tools.nsctypechecker.Infer which you can find simply by looking in the Scala compiler sources for a part of your error. You’ll find out the following fragment:

      /** error if arguments not within bounds. */
        def checkBounds(pos: Position, pre: Type, owner: Symbol,
                        tparams: List[Symbol], targs: List[Type], prefix: String) = {
          //@M validate variances & bounds of targs wrt variances & bounds of tparams
          //@M TODO: better place to check this?
          //@M TODO: errors for getters & setters are reported separately
          val kindErrors = checkKindBounds(tparams, targs, pre, owner)
    
          if(!kindErrors.isEmpty) {
            error(pos,
              prefix + "kinds of the type arguments " + targs.mkString("(", ",", ")") +
              " do not conform to the expected kinds of the type parameters "+ tparams.mkString("(", ",", ")") + tparams.head.locationString+ "." +
              kindErrors.toList.mkString("\n", ", ", ""))
          } 
    

    So now the point is understanding why checkKindBounds(tparams, targs, pre, owner) returns those errors. If you go down the method call chain, you will see that the checkKindBounds call another method

    val errors = checkKindBounds0(tparams, targs, pre, owner, true)
    

    You’ll see the problem is connected to checking bounds of higher-kinded type, at line 5784, inside checkKindBoundsHK :

     if (!sameLength(hkargs, hkparams)) {
            if (arg == AnyClass || arg == NothingClass) (Nil, Nil, Nil) // Any and Nothing are kind-overloaded
            else {error = true; (List((arg, param)), Nil, Nil) } // shortcut: always set error, whether explainTypesOrNot
          }
    

    The test is not passed, it appears that in my debugger:

    hkargs$1 = {scala.collection.immutable.Nil$@2541}"List()"
    arg$1 = {scala.tools.nsc.symtab.Symbols$ClassSymbol@2689}"class List"
    param$1 = {scala.tools.nsc.symtab.Symbols$TypeSymbol@2557}"type B"
    paramowner$1 = {scala.tools.nsc.symtab.Symbols$MethodSymbol@2692}"method process"
    underHKParams$1 = {scala.collection.immutable.$colon$colon@2688}"List(type R)"
    withHKArgs$1 = {scala.collection.immutable.Nil$@2541}"List()"
    exceptionResult12 = null
    hkparams$1 = {scala.collection.immutable.$colon$colon@2688}"List(type R)"
    

    So it appears like there is one higher kinded param, type R, but there is no provided value for that.

    If you actually go back to the to checkKindBounds, you see that after the snippet:

     val (arityMismatches, varianceMismatches, stricterBounds) = (
            // NOTE: *not* targ.typeSymbol, which normalizes
            checkKindBoundsHK(tparamsHO, targ.typeSymbolDirect, tparam, tparam.owner, tparam.typeParams, tparamsHO)
          )
    

    the arityMismatches contains a tuple List, B. And now you can also see that the error message is wrong:

    inferred kinds of the type arguments (MyFoo,MyFoo,List[X]) do not
    conform to the expected kinds of the type parameters (type F,type
    R,type B). List[X]’s type parameters do not match type B’s expected
    parameters: class List has one type parameter, but type B has ZERO

    In fact if you put a breakpoint at line 5859 on the following call

    checkKindBoundsHK(tparamsHO, targ.typeSymbolDirect, tparam, tparam.owner, tparam.typeParams, tparamsHO)
    

    you can see that

    tparam = {scala.tools.nsc.symtab.Symbols$TypeSymbol@2472}"type B"
    targ = {scala.tools.nsc.symtab.Types$UniqueTypeRef@2473}"List[X]"
    

    Conclusion:

    For some reason, when dealing with complex higher-kinded types such as yours, Scala compiler inference is limited. I don’t know where it does come from, maybe you want to send a bug to the compiler team

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

Sidebar

Related Questions

Recently I stumbled over code such as this: void foo(const Bar* b) { ...
I recently stumbled across DBslayer ( http://code.nytimes.com/projects/dbslayer/wiki/WhyUseIt ) and wondered what is the actual
I recently stumbled across the following assembly instruction sequence: rep stos dword ptr [edi]
I recently stumbled across the following behaviour of gcc 3.2.2 writing a c program:
I stumbled across this code recently: void strcat( char* dest, char* src ) {
I'm refactoring some code that a friend wrote and recently stumbled across this function:
I recently stumbled across the following: <? $d=false; var_dump($d[123]); ?> which yielded NULL ,
I recently stumbled across an article that claims Microsoft is banning the memcpy() function
I've recently stumbled across geb and it looks like a good way to perform
I was recently looking to practice utilizing dictionaries in python and stumbled across these

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.