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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:08:01+00:00 2026-05-20T15:08:01+00:00

I have a script. It runs without warnings. $ cat ~/tmp/so1.scala import org.yaml.snakeyaml.Yaml class

  • 0

I have a script. It runs without warnings.

$ cat ~/tmp/so1.scala 
import org.yaml.snakeyaml.Yaml

class JavaMapIteratorWrapper[K,V] (map: java.util.Map[K,V]) {
  def foreach (f: Tuple2 [K, V] => Unit): Unit = {
    val iter = map.entrySet.iterator
    while (iter.hasNext) {
      val entry = iter.next
      f (entry.getKey, entry.getValue)
    }
  }
}

implicit def foreachJavaMap[K,V] (map: java.util.Map[K,V]): JavaMapIteratorWrapper[K,V] = new JavaMapIteratorWrapper[K,V](map)

val yaml = new Yaml;
(yaml load (io.Source.fromFile(argv(0)).mkString)) match {
  case map: java.util.Map [_, _] => {
    for (entry <- map) {
      entry match {
        case ("id", id: String) => System.out.println ("ID is " + id)
        case (n: String, v: String) => System.out.println (n + " = " + v)
      }
    }
  }
}

$ scala -unchecked -classpath jar/snakeyaml-1.7.jar ~/tmp/so1.scala eg/default.yaml
(program output as expected)

I’d like to extract the loop into its own function. So I try that.

$ cat ~/tmp/so2.scala 
import org.yaml.snakeyaml.Yaml

class JavaMapIteratorWrapper[K,V] (map: java.util.Map[K,V]) {
  def foreach (f: Tuple2 [K, V] => Unit): Unit = {
    val iter = map.entrySet.iterator
    while (iter.hasNext) {
      val entry = iter.next
      f (entry.getKey, entry.getValue)
    }
  }
}

implicit def foreachJavaMap[K,V] (map: java.util.Map[K,V]): JavaMapIteratorWrapper[K,V] = new JavaMapIteratorWrapper[K,V](map)

val processMap = (map: java.util.Map [_, _]) => {
  for (entry <- map) {      // line 16
    entry match {
      case ("id", id: String) => System.out.println ("ID is " + id)
      case (n: String, v: String) => System.out.println (n + " = " + v)
    }
  }
}

val yaml = new Yaml;
(yaml load (io.Source.fromFile(argv(0)).mkString)) match {
  case map: java.util.Map [_, _] => processMap (map)
}

$ scala -unchecked -classpath jar/snakeyaml-1.7.jar ~/tmp/so2.scala eg/default.yaml
(fragment of so2.scala):16: error: type mismatch;
 found   : map.type (with underlying type java.util.Map[_, _])
 required: java.util.Map[_$1,_$2] where type _$2, type _$1
  for (entry <- map) {
                 ^
one error found
!!!
discarding <script preamble>

The loop being in its own function means it requires a more specific type. Okay.

I’ll try with java.util.Map [AnyRef, AnyRef] instead of java.util.Map [_, _].

$ cat ~/tmp/so3.scala 
import org.yaml.snakeyaml.Yaml

class JavaMapIteratorWrapper[K,V] (map: java.util.Map[K,V]) {
  def foreach (f: Tuple2 [K, V] => Unit): Unit = {
    val iter = map.entrySet.iterator
    while (iter.hasNext) {
      val entry = iter.next
      f (entry.getKey, entry.getValue)
    }
  }
}

implicit def foreachJavaMap[K,V] (map: java.util.Map[K,V]): JavaMapIteratorWrapper[K,V] = new JavaMapIteratorWrapper[K,V](map)

val processMap = (map: java.util.Map [AnyRef, AnyRef]) => {
  for (entry <- map) {
    entry match {
      case ("id", id: String) => System.out.println ("ID is " + id)
      case (n: String, v: String) => System.out.println (n + " = " + v)
    }
  }
}

val yaml = new Yaml;
(yaml load (io.Source.fromFile(argv(0)).mkString)) match {
  case map: java.util.Map [AnyRef, AnyRef] => processMap (map)      // line 26
}

$ scala -unchecked -classpath jar/snakeyaml-1.7.jar ~/tmp/so3.scala eg/default.yaml
(fragment of so3.scala):26: warning: non variable type-argument AnyRef in type pattern is unchecked since it is eliminated by erasure
  case map: java.util.Map [AnyRef, AnyRef] => processMap (map)
                       ^
one warning found
!!!
discarding <script preamble>
(program output as expected)

So now it runs, but it gives me a warning. How do I eliminate that warning?

Notes:

  1. org.yaml.snakeyaml.Yaml is written in Java, so I can’t use type manifests. (Can I?)
  2. My real program uses several Java libraries, so I want to be warned when I make possibly false assumptions about what types
    I’m being given. But how do I tell the compiler “yes, I’ve checked this, it’s correct, don’t warn me about it again”?
  3. I’m using scala 2.7.7 (because that’s the version that’s packaged with Ubuntu).
  • 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-20T15:08:02+00:00Added an answer on May 20, 2026 at 3:08 pm

    You could try removing your custom wrapper to start with. The (2.8.1) Scala standard library already includes a wrapper to use Java collection types more idiomatically, in scala.collection.JavaConverters. (note: the scala. prefix is not needed when importing this)

    I’d also make processMap a method instead of a function, and add type params:

    import collection.JavaConverters._
    
    def processMap[K,V](map: Map[K, V]): Unit = {
      for (entry <- map) {
        entry match {
          case ("id", id: String) => System.out.println ("ID is " + id)
          case (n: String, v: String) => System.out.println (n + " = " + v)
        }
      }
    }
    
    val yaml = new Yaml
    (yaml load (io.Source.fromFile(argv(0)).mkString)) match {
      case map: java.util.Map[_, _] => processMap(map.asScala)
    }
    

    Note the asScala method on the second to last line…

    When dealing with Java/Scala interop, it’s generally a best practice to convert from Java to Scala collections at the earliest opportunity, and to convert back as late as possible.

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

Sidebar

Related Questions

I have a bash script which runs without needing to be root . But
I have a script that runs every two minutes for a Tweet-getter application. In
I have a php script that is run once a minute. The script runs
I have a PHP script that runs as a CGI program and the HTTP
I have a bash script that runs a simulation program written in Fortran 90,
I have a lingo script which runs some data processing for a Flash movie.
I have a python script the runs this code: strpath = sudo svnadmin create
I have a php script that runs a mysql query, then loops the result,
Currently I have a bash script which runs the find command, like so: find
Hi guys I have a weird question, I have a cli php script runs

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.