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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:38:14+00:00 2026-05-17T02:38:14+00:00

I have the following Scala code. import scala.actors.Actor object Alice extends Actor { this.start

  • 0

I have the following Scala code.

import scala.actors.Actor

object Alice extends Actor {
  this.start
  def act{
    loop{
      react {
        case "Hello" => sender ! "Hi"
        case i:Int => sender ! 0
      }
    }
  }
}
object Test {
  def test = {
    (Alice !? (100, "Hello")) match {
      case i:Some[Int] => println ("Int received "+i)
      case s:Some[String] => println ("String received "+s)
      case _ =>
    }
    (Alice !? (100, 1)) match {
      case i:Some[Int] => println ("Int received "+i)
      case s:Some[String] => println ("String received "+s)
      case _ =>
    }
  }
}

After doing Test.test, I get the output:

scala> Test.test
Int received Some(Hi)
Int received Some(0)

I was expecting the output

String received Some(Hi)
Int received Some(0)

What is the explanation?

As a second question, I get unchecked warnings with the above as follows:

C:\scalac -unchecked a.scala
a.scala:17: warning: non variable type-argument Int in type pattern Some[Int] is unchecked since it is eliminated by erasure
      case i:Some[Int] => println ("Int received "+i)
             ^
a.scala:18: warning: non variable type-argument String in type pattern Some[String] is unchecked since it is eliminated by erasure
      case s:Some[String] => println ("String received "+s)
             ^
a.scala:22: warning: non variable type-argument Int in type pattern Some[Int] is unchecked since it is eliminated by erasure
      case i:Some[Int] => println ("Int received "+i)
             ^
a.scala:23: warning: non variable type-argument String in type pattern Some[String] is unchecked since it is eliminated by erasure
      case s:Some[String] => println ("String received "+s)
             ^
four warnings found

How can I avoid the warnings?

EDIT: Thanks for the suggestions. Daniel’s idea is nice but does not seem to work with generic types, as in the example below

def test[T] = (Alice !? (100, "Hello")) match { 
   case Some(i: Int) => println ("Int received "+i) 
   case Some(t: T) => println ("T received ") 
   case _ =>  
}

The following error warning is encountered: warning: abstract type T in type pattern T is unchecked since it is eliminated by erasure

  • 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-05-17T02:38:15+00:00Added an answer on May 17, 2026 at 2:38 am

    This is due to type-erasure. The JVM does not know of any type parameter, except on arrays. Because of that, Scala code can’t check whether an Option is an Option[Int] or an Option[String] — that information has been erased.

    You could fix your code this way, though:

    object Test {
      def test = {
        (Alice !? (100, "Hello")) match {
          case Some(i: Int) => println ("Int received "+i)
          case Some(s: String) => println ("String received "+s)
          case _ =>
        }
        (Alice !? (100, 1)) match {
          case Some(i: Int) => println ("Int received "+i)
          case Some(s: String) => println ("String received "+s)
          case _ =>
        }
      }
    }
    

    This way you are not testing what the type of Option is, but what the type of its contents are — assuming there is any content. A None will fall through to the default case.

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

Sidebar

Related Questions

I have the following scala code: package dummy import javax.servlet.http.{HttpServlet, HttpServletRequest => HSReq, HttpServletResponse
I am looking into scala TCO and have written the following code import scala.annotation.tailrec
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
Consider the following Scala code: package scala_java object MyScala { def setFunc(func: Int =>
In Scala IDE I have the following code: package pkg.one object S { class
Consider following code in Scala: object Test { class A {} class B extends
Disclaimer: absolute novice in Scala :( I have the following defined: def tryAndReport(body: Unit)
I have to translate the following code from Java to Scala: EDIT: added if-statements
I've got a simple question for you :) I have the following Scala code,
Suppose I have following code def foo(x:Int):Unit = { if (x == 1) println

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.