Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I form the union of scala SortedMaps?
    text
    copied!<p>(I'm using Scala nightlies, and see the same behaviour in 2.8.0b1 RC4. I'm a Scala newcomer.)</p> <p>I have two <code>SortedMap</code>s that I'd like to form the union of. Here's the code I'd like to use:</p> <pre><code>import scala.collection._ object ViewBoundExample { class X def combine[Y](a: SortedMap[X, Y], b: SortedMap[X, Y]): SortedMap[X, Y] = { a ++ b } implicit def orderedX(x: X): Ordered[X] = new Ordered[X] { def compare(that: X) = 0 } } </code></pre> <p>The idea here is the 'implicit' statement means <code>X</code>s can be converted to <code>Ordered[X]</code>s, and then it makes sense combine <code>SortedMap</code>s into another <code>SortedMap</code>, rather than just a map.</p> <p>When I compile, I get</p> <pre><code>sieversii:scala-2.8.0.Beta1-RC4 scott$ bin/scalac -versionScala compiler version 2.8.0.Beta1-RC4 -- Copyright 2002-2010, LAMP/EPFL sieversii:scala-2.8.0.Beta1-RC4 scott$ bin/scalac ViewBoundExample.scala ViewBoundExample.scala:8: error: type arguments [ViewBoundExample.X] do not conform to method ordered's type parameter bounds [A &lt;: scala.math.Ordered[A]] a ++ b ^ one error found </code></pre> <p>It seems my problem would go away if that type parameter bound was <code>[A &lt;% scala.math.Ordered[A]]</code>, rather than <code>[A &lt;: scala.math.Ordered[A]]</code>. Unfortunately, I can't even work out where the method 'ordered' lives! Can anyone help me track it down?</p> <p>Failing that, what am I meant to do to produce the union of two <code>SortedMap</code>s? If I remove the return type of combine (or change it to <code>Map</code>) everything works fine --- but then I can't rely on the return being sorted!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload