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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:57:27+00:00 2026-06-08T04:57:27+00:00

When using .isInstanceOf[GenericType[SomeOtherType]] , where GenericType and SomeOtherType are arbitrary types (of suitable kind),

  • 0

When using .isInstanceOf[GenericType[SomeOtherType]], where GenericType and SomeOtherType are arbitrary types (of suitable kind), the Scala compiler gives an unchecked warning due to type erasure:

scala> Some(123).isInstanceOf[Option[Int]]
<console>:8: warning: non variable type-argument Int in type Option[Int] is unchecked since it is eliminated by erasure
              Some(123).isInstanceOf[Option[Int]]
                                    ^
res0: Boolean = true

scala> Some(123).isInstanceOf[Option[String]]
<console>:8: warning: non variable type-argument String in type Option[String] is unchecked since it is eliminated by erasure
              Some(123).isInstanceOf[Option[String]]
                                    ^
res1: Boolean = true

However, if SomeOtherType is itself a generic type (e.g. List[String]), no warning is emitted:

scala> Some(123).isInstanceOf[Option[List[String]]]
res2: Boolean = true

scala> Some(123).isInstanceOf[Option[Option[Int]]]
res3: Boolean = true

scala> Some(123).isInstanceOf[Option[List[Int => String]]]
res4: Boolean = true

scala> Some(123).isInstanceOf[Option[(String, Double)]]
res5: Boolean = true

scala> Some(123).isInstanceOf[Option[String => Double]]
res6: Boolean = true

(recall that tuples and => are syntactic sugar for Tuple2[] and Function2[] generic types)

Why is no warning emitted? (All these are in the Scala REPL 2.9.1, with the -unchecked option.)

  • 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-08T04:57:29+00:00Added an answer on June 8, 2026 at 4:57 am

    I had a look to the Scala compiler sources, and I discovered something interesting looking at

    scala.tools.nsc.typechecker.Infer
    

    which is where you find the warning. If you look carefully at line 1399 to:

    def checkCheckable(pos: Position, tp: Type, kind: String)
    

    which is where the the the warning are generated, you see some nested methods including the check method:

     def check(tp: Type, bound: List[Symbol]) {
            def isLocalBinding(sym: Symbol) =
              sym.isAbstractType &&
              ((bound contains sym) ||
               sym.name == tpnme.WILDCARD || {
                val e = context.scope.lookupEntry(sym.name)
                (e ne null) && e.sym == sym && !e.sym.isTypeParameterOrSkolem && e.owner == context.scope
              })
            tp match {
              case SingleType(pre, _) =>
                check(pre, bound)
              case TypeRef(pre, sym, args) =>
                if (sym.isAbstractType) {
                  if (!isLocalBinding(sym)) patternWarning(tp, "abstract type ")
                } else if (sym.isAliasType) {
                  check(tp.normalize, bound)
                } else if (sym == NothingClass || sym == NullClass || sym == AnyValClass) {
                  error(pos, "type "+tp+" cannot be used in a type pattern or isInstanceOf test")
                } else {
                  for (arg <- args) {
                    if (sym == ArrayClass) check(arg, bound)
                    else if (arg.typeArgs.nonEmpty) ()   // avoid spurious warnings with higher-kinded types
                    else arg match {
                      case TypeRef(_, sym, _) if isLocalBinding(sym) =>
                        ;
                      case _ =>
                        patternWarning(arg, "non variable type-argument ")
                    }
                  }
                }
                check(pre, bound)
              case RefinedType(parents, decls) =>
                if (decls.isEmpty) for (p <- parents) check(p, bound)
                else patternWarning(tp, "refinement ")
              case ExistentialType(quantified, tp1) =>
                check(tp1, bound ::: quantified)
              case ThisType(_) =>
                ;
              case NoPrefix =>
                ;
              case _ =>
                patternWarning(tp, "type ")
            }
      }
    

    While I am not expert in the Scala compiler, we should all thanks the guys to making the code so self-explanatory. Let’s look inside the tp match block and the treated cases:

    • If its a single type
    • If it is a type ref
      • If is abstract type
      • If is an alias type
      • If its Null, Nothing, or Anyval
      • All other cases

    If you look to all other cases, there is a line which is also commented:

    else if (arg.typeArgs.nonEmpty) ()   // avoid spurious warnings with higher-kinded types
    

    That tells you exactly what happen if your type has other type parameter (as Function2, or Tuple2). The check function returns unit without performing any test.

    I do not for which reason this has been done this way, but you might want to open a bug at
    https://issues.scala-lang.org/browse/SI
    providing the code you posted here as an excellent test case, and reference to the Infer.scala source which I copied above.

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

Sidebar

Related Questions

What are the differences between these two code snippets? Using type : import types
Update: Based on the answers I initially went the route of using IsInstanceOf() which
Using a populated Table Type as the source for a TSQL-Merge. I want to
I am testing a parser I have written in Scala using ScalaTest. The parser
I just started playing with scala and have been using the Scala By Example
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using SQL Server 2008 R2 we are looking for a way to select the
Using CI for the first time and i'm smashing my head with this seemingly
Using the Redis info command, I am able to get all the stats of

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.