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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:47:35+00:00 2026-06-09T20:47:35+00:00

Possible Duplicate: How do I get around type erasure on Scala? Or, why can't

  • 0

Possible Duplicate:
How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections?

I ran the following code:

scala>  var s = new Stack()push(1)
s: scalatest.Stack[Int] = 1 

scala>  s match { case s : Stack[String] => print("Hello")}
<console>:12: warning: non variable type-argument String in type pattern scalatest.Stack[String] is unchecked since it is eliminated by erasure
              s match { case s : Stack[String] => print("Hello")
}

Stack is the class taken from http://www.scala-lang.org/node/129. If I run this code without -unchecked flag it will print “Hello”. Why is that the case?

  • 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-09T20:47:37+00:00Added an answer on June 9, 2026 at 8:47 pm

    The problem is that you’re matching s to be of type Stack[String]. During runtime, it’s possible to determine if s is of type Stack, but because of Java’s type erasure it’s not possible to determine if s is of type Stack[String], Stack[Int] etc. So no matter what the type parameter was, it gets matched by the case expression. This is why Scala issues the warning. It is the same as if you match as

    s match { case s : Stack[_] => print("Hello")}
    

    (which will compile without warnings).


    Edit: A workaround (for Java too) is to create a specific class that doesn’t have type parameters any more. For example:

    import scala.collection.mutable.Stack;
    
    object Test extends App {
      class MyStack extends Stack[Int];
      class MyOtherStack extends Stack[String];
    
      val s: Stack[_] = new MyStack().push(1);
    
      s match {
        case s : MyOtherStack => print("Hello String");
        case s : MyStack => print("Hello Int");
      }
    }
    

    It has a drawback that you cannot use it for immutable containers, because their methods create new objects and they won’t be instances of these our specific subclasses.

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

Sidebar

Related Questions

Possible Duplicate: How can I get around declaring an unused variable in a for
Possible Duplicate: Get URL of ASP.Net Page in code-behind I'm trying to hold current
Possible Duplicate: get name of current PHP script in include file Can an included
Possible Duplicate: C# Get control by name I have been looking around for an
Possible Duplicate: Get random item from array I need a code to set a
Possible Duplicate: Android How to draw a smooth line following your finger I'm new
Possible Duplicate: c# Can I use reflection to inspect the code in a method?
Possible Duplicate: Can you write object oriented code in C? Hi! Just for the
Possible Duplicate: Zend_Validate::is() Static Check, get validation message work around I need a simple
Possible Duplicate: Get variable name. javascript “reflection” Is there a way to know the

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.