This page is no longer maintained — Please continue to the home page at www.scala-lang.org

why does this not compile?

11 replies
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.

the error is
error: could not find implicit value for parameter ord:
scala.math.Ordering[scala.math.Ordered[_ >: _$1 with _$2]]
} else if (value > highest) {

the code, directly compileable:

def findHighestAndRating[T](t: Traversable[T], f: T => Ordered[_],
filter: T => Boolean = (t:T) => true, ifEqual: (T, T) => T = (t1:T,
t2:T) => t2) = {

var best = null.asInstanceOf[T]
var highest = null.asInstanceOf[Ordered[_]]
for (e <- t if filter(e)) {
val value = f(e)
if (highest == null) {
best = e
} else if (value > highest) {
best = e
highest = value
} else if (value == highest) {
best = ifEqual(e, best)
}
}
if (best == null) {
None
} else {
Some(best -> highest)
}
}

value and highest are both Ordered[_], so why is there a problem?

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: why does this not compile?
Running with -Xprint:typer, which is your dearest friend at times like these:
            var best: T = null.asInstanceOf[T];            var highest: Ordered[U] = null.asInstanceOf[Ordered[U]];             t.withFilter(((e: T) => filter.apply(e))).foreach[Unit](((e: T) => {              val value: scala.math.Ordered[U] = f.apply(e);              if (highest.==(null))                best = e               else                if (math.this.Ordered.orderingToOrdered[scala.math.Ordered[U]](value)().>(highest))
value.>(highest) doesn't type check, as Ordered[U]#> expects a parameter of type 'U', not 'Ordered[U]'. The compiler then tries to make things work with an Implicit View, which also fails. You only see the second error, which is definitely a usability problem with scalac.
BTW, the implicit conversion Ordered.orderingToOrdered is considered *without* an import, because it is the companion object of the  type of the qualifier 'value'. This is known as the Implicit Scope.
-jason


On Fri, Nov 26, 2010 at 2:49 PM, HamsterofDeath <h-star@gmx.de> wrote:
the error is
error: could not find implicit value for parameter ord:
scala.math.Ordering[scala.math.Ordered[_ >: _$1 with _$2]]
} else if (value > highest) {

the code, directly compileable:

 def findHighestAndRating[T](t: Traversable[T], f: T => Ordered[_],
filter: T => Boolean = (t:T) => true, ifEqual: (T, T) => T = (t1:T,
t2:T) => t2) = {

   var best = null.asInstanceOf[T]
   var highest = null.asInstanceOf[Ordered[_]]
   for (e <- t if filter(e)) {
     val value = f(e)
     if (highest == null) {
       best = e
     } else if (value > highest) {
       best = e
       highest = value
     } else if (value == highest) {
       best = ifEqual(e, best)
     }
   }
   if (best == null) {
     None
   } else {
     Some(best -> highest)
   }
 }

value and highest are both Ordered[_], so why is there a problem?

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: why does this not compile?
my actual error was a simple one, i saw it the moment i tried to use comparable instead of ordered

Am 26.11.2010 15:37, schrieb Jason Zaugg:
gpFnFscpO24Z9j+nnTTGV [at] mail [dot] gmail [dot] com" type="cite"> Running with -Xprint:typer, which is your dearest friend at times like these:
            var best: T = null.asInstanceOf[T];             var highest: Ordered[U] = null.asInstanceOf[Ordered[U]];             t.withFilter(((e: T) => filter.apply(e))).foreach[Unit](((e: T) => {               val value: scala.math.Ordered[U] = f.apply(e);               if (highest.==(null))                 best = e               else                 if (math.this.Ordered.orderingToOrdered[scala.math.Ordered[U]](value)().>(highest))
value.>(highest) doesn't type check, as Ordered[U]#> expects a parameter of type 'U', not 'Ordered[U]'. The compiler then tries to make things work with an Implicit View, which also fails. You only see the second error, which is definitely a usability problem with scalac.
BTW, the implicit conversion Ordered.orderingToOrdered is considered *without* an import, because it is the companion object of the  type of the qualifier 'value'. This is known as the Implicit Scope.
-jason


On Fri, Nov 26, 2010 at 2:49 PM, HamsterofDeath <h-star [at] gmx [dot] de" rel="nofollow">h-star@gmx.de> wrote:
the error is
error: could not find implicit value for parameter ord:
scala.math.Ordering[scala.math.Ordered[_ >: _$1 with _$2]]
} else if (value > highest) {

the code, directly compileable:

 def findHighestAndRating[T](t: Traversable[T], f: T => Ordered[_],
filter: T => Boolean = (t:T) => true, ifEqual: (T, T) => T = (t1:T,
t2:T) => t2) = {

   var best = null.asInstanceOf[T]
   var highest = null.asInstanceOf[Ordered[_]]
   for (e <- t if filter(e)) {
     val value = f(e)
     if (highest == null) {
       best = e
     } else if (value > highest) {
       best = e
       highest = value
     } else if (value == highest) {
       best = ifEqual(e, best)
     }
   }
   if (best == null) {
     None
   } else {
     Some(best -> highest)
   }
 }

value and highest are both Ordered[_], so why is there a problem?


H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: why does this not compile?
i'm still struggling to get this done.what i want to do is to find the highest element of a collection, and i want to rate each element by a function that calculates a comparable[x] for each element of the collection.
"max" already does that, but i also want to return the actual X that was used.



Am 26.11.2010 16:15, schrieb HamsterofDeath:
4CEFCF0C [dot] 3020905 [at] gmx [dot] de" type="cite"> my actual error was a simple one, i saw it the moment i tried to use comparable instead of ordered

Am 26.11.2010 15:37, schrieb Jason Zaugg:
gpFnFscpO24Z9j+nnTTGV [at] mail [dot] gmail [dot] com" type="cite"> Running with -Xprint:typer, which is your dearest friend at times like these:
            var best: T = null.asInstanceOf[T];             var highest: Ordered[U] = null.asInstanceOf[Ordered[U]];             t.withFilter(((e: T) => filter.apply(e))).foreach[Unit](((e: T) => {               val value: scala.math.Ordered[U] = f.apply(e);               if (highest.==(null))                 best = e               else                 if (math.this.Ordered.orderingToOrdered[scala.math.Ordered[U]](value)().>(highest))
value.>(highest) doesn't type check, as Ordered[U]#> expects a parameter of type 'U', not 'Ordered[U]'. The compiler then tries to make things work with an Implicit View, which also fails. You only see the second error, which is definitely a usability problem with scalac.
BTW, the implicit conversion Ordered.orderingToOrdered is considered *without* an import, because it is the companion object of the  type of the qualifier 'value'. This is known as the Implicit Scope.
-jason


On Fri, Nov 26, 2010 at 2:49 PM, HamsterofDeath <h-star [at] gmx [dot] de" rel="nofollow">h-star@gmx.de> wrote:
the error is
error: could not find implicit value for parameter ord:
scala.math.Ordering[scala.math.Ordered[_ >: _$1 with _$2]]
} else if (value > highest) {

the code, directly compileable:

 def findHighestAndRating[T](t: Traversable[T], f: T => Ordered[_],
filter: T => Boolean = (t:T) => true, ifEqual: (T, T) => T = (t1:T,
t2:T) => t2) = {

   var best = null.asInstanceOf[T]
   var highest = null.asInstanceOf[Ordered[_]]
   for (e <- t if filter(e)) {
     val value = f(e)
     if (highest == null) {
       best = e
     } else if (value > highest) {
       best = e
       highest = value
     } else if (value == highest) {
       best = ifEqual(e, best)
     }
   }
   if (best == null) {
     None
   } else {
     Some(best -> highest)
   }
 }

value and highest are both Ordered[_], so why is there a problem?



H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: why does this not compile?
the core problem is:
previously, i used a function T => Double to rate elements of a list and collect the highest one. i also returned the double. no problem
now, if i want to allow any comparable type as a rating, the rating function must return "Comparable[X] AND X", meaning it must be something like Double, which is both Double and Comparable[Double]. in java, you could write that as void foo<X extends A & B>

is this possible in scala?


Am 26.11.2010 16:29, schrieb HamsterofDeath:
4CEFD260 [dot] 1090007 [at] gmx [dot] de" type="cite"> i'm still struggling to get this done.what i want to do is to find the highest element of a collection, and i want to rate each element by a function that calculates a comparable[x] for each element of the collection.
"max" already does that, but i also want to return the actual X that was used.



Am 26.11.2010 16:15, schrieb HamsterofDeath:
4CEFCF0C [dot] 3020905 [at] gmx [dot] de" type="cite"> my actual error was a simple one, i saw it the moment i tried to use comparable instead of ordered

Am 26.11.2010 15:37, schrieb Jason Zaugg:
gpFnFscpO24Z9j+nnTTGV [at] mail [dot] gmail [dot] com" type="cite"> Running with -Xprint:typer, which is your dearest friend at times like these:
            var best: T = null.asInstanceOf[T];             var highest: Ordered[U] = null.asInstanceOf[Ordered[U]];             t.withFilter(((e: T) => filter.apply(e))).foreach[Unit](((e: T) => {               val value: scala.math.Ordered[U] = f.apply(e);               if (highest.==(null))                 best = e               else                 if (math.this.Ordered.orderingToOrdered[scala.math.Ordered[U]](value)().>(highest))
value.>(highest) doesn't type check, as Ordered[U]#> expects a parameter of type 'U', not 'Ordered[U]'. The compiler then tries to make things work with an Implicit View, which also fails. You only see the second error, which is definitely a usability problem with scalac.
BTW, the implicit conversion Ordered.orderingToOrdered is considered *without* an import, because it is the companion object of the  type of the qualifier 'value'. This is known as the Implicit Scope.
-jason


On Fri, Nov 26, 2010 at 2:49 PM, HamsterofDeath <h-star [at] gmx [dot] de" rel="nofollow">h-star@gmx.de> wrote:
the error is
error: could not find implicit value for parameter ord:
scala.math.Ordering[scala.math.Ordered[_ >: _$1 with _$2]]
} else if (value > highest) {

the code, directly compileable:

 def findHighestAndRating[T](t: Traversable[T], f: T => Ordered[_],
filter: T => Boolean = (t:T) => true, ifEqual: (T, T) => T = (t1:T,
t2:T) => t2) = {

   var best = null.asInstanceOf[T]
   var highest = null.asInstanceOf[Ordered[_]]
   for (e <- t if filter(e)) {
     val value = f(e)
     if (highest == null) {
       best = e
     } else if (value > highest) {
       best = e
       highest = value
     } else if (value == highest) {
       best = ifEqual(e, best)
     }
   }
   if (best == null) {
     None
   } else {
     Some(best -> highest)
   }
 }

value and highest are both Ordered[_], so why is there a problem?




Ruediger Keller
Joined: 2010-04-11,
User offline. Last seen 42 years 45 weeks ago.
Re: why does this not compile?

2010/11/26 HamsterofDeath :
> the core problem is:
> previously, i used a function T => Double to rate elements of a list and
> collect the highest one. i also returned the double. no problem
> now, if i want to allow any comparable type as a rating, the rating function
> must return "Comparable[X] AND X", meaning it must be something like Double,
> which is both Double and Comparable[Double]. in java, you could write that
> as void foo
>
> is this possible in scala?

Does this work?

[T <: Double with Comparable[Double]]

Regards,
Ruediger

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: why does this not compile?

On Friday November 26 2010, HamsterofDeath wrote:
> the core problem is:
> previously, i used a function T => Double to rate elements of a list
> and collect the highest one. i also returned the double. no problem
> now, if i want to allow any comparable type as a rating, the rating
> function must return "Comparable[X] AND X", meaning it must be
> something like Double, which is both Double and Comparable[Double].
> in java, you could write that as void foo
>
> is this possible in scala?

def foo[X <: A with B](...): Unit = ...

I had thought that linearization of A with B (vs. B with A) would make
make the two incompatible, but that is evidently not so. See below.

To wit:
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait A { def a: String = "A" }
defined trait A

scala> trait B { def b: String = "B" }
defined trait B

scala> val a1 = new A {}
a1: java.lang.Object with A = $anon$1@1a66c87

scala> val b1 = new B {}
b1: java.lang.Object with B = $anon$1@1d626a4

scala> val ab1 = new A with B {}
ab1: java.lang.Object with A with B = $anon$1@1bdcbb2

scala> val ba1 = new B with A {}
ba1: java.lang.Object with B with A = $anon$1@15ddf5

scala> def mab[E <: A with B](e: E): String = "mab:%s|%s".format(e.a, e.b)
mab: [E <: A with B](e: E)String

scala> def mba[E <: B with A](e: E): String = "mba:%s|%s".format(e.b, e.a)
mba: [E <: B with A](e: E)String

scala> mab(ab1)
res0: String = mab:A|B

scala> mba(ba1)
res1: String = mba:B|A

scala> mab(ba1)
res2: String = mab:A|B

scala> mba(ab1)
res3: String = mba:B|A

scala> mab(a1)
:10: error: inferred type arguments [java.lang.Object with A] do not conform to method mab's type parameter bounds [E <: A with B]
mab(a1)
^

scala> mab(b1)
:10: error: inferred type arguments [java.lang.Object with B] do not conform to method mab's type parameter bounds [E <: A with B]
mab(b1)
^

scala> mba(a1)
:10: error: inferred type arguments [java.lang.Object with A] do not conform to method mba's type parameter bounds [E <: B with A]
mba(a1)
^

scala> mba(b1)
:10: error: inferred type arguments [java.lang.Object with B] do not conform to method mba's type parameter bounds [E <: B with A]
mba(b1)
^
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-

Randall Schulz

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: why does this not compile?

that worked, but i cannot call the method anymore. if i use this definition
def findHighestAndRating[C <: AnyVal with Comparable[C]](f: T => C)

and try to use a function that returns java.lang.Integer, i get an error
about Integer not conforming the type parameter bounds. but Integer is
an AnyVal and implements Comparable[Integer] so i'd say it matches the
bound.

Am 26.11.2010 18:50, schrieb Randall R Schulz:
> On Friday November 26 2010, HamsterofDeath wrote:
>> the core problem is:
>> previously, i used a function T => Double to rate elements of a list
>> and collect the highest one. i also returned the double. no problem
>> now, if i want to allow any comparable type as a rating, the rating
>> function must return "Comparable[X] AND X", meaning it must be
>> something like Double, which is both Double and Comparable[Double].
>> in java, you could write that as void foo
>>
>> is this possible in scala?
> def foo[X <: A with B](...): Unit = ...
>
> I had thought that linearization of A with B (vs. B with A) would make
> make the two incompatible, but that is evidently not so. See below.
>
>
> To wit:
> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
> Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_22).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> trait A { def a: String = "A" }
> defined trait A
>
> scala> trait B { def b: String = "B" }
> defined trait B
>
>
> scala> val a1 = new A {}
> a1: java.lang.Object with A = $anon$1@1a66c87
>
> scala> val b1 = new B {}
> b1: java.lang.Object with B = $anon$1@1d626a4
>
>
> scala> val ab1 = new A with B {}
> ab1: java.lang.Object with A with B = $anon$1@1bdcbb2
>
> scala> val ba1 = new B with A {}
> ba1: java.lang.Object with B with A = $anon$1@15ddf5
>
>
> scala> def mab[E <: A with B](e: E): String = "mab:%s|%s".format(e.a, e.b)
> mab: [E <: A with B](e: E)String
>
> scala> def mba[E <: B with A](e: E): String = "mba:%s|%s".format(e.b, e.a)
> mba: [E <: B with A](e: E)String
>
>
> scala> mab(ab1)
> res0: String = mab:A|B
>
> scala> mba(ba1)
> res1: String = mba:B|A
>
> scala> mab(ba1)
> res2: String = mab:A|B
>
> scala> mba(ab1)
> res3: String = mba:B|A
>
>
> scala> mab(a1)
> :10: error: inferred type arguments [java.lang.Object with A] do not conform to method mab's type parameter bounds [E <: A with B]
> mab(a1)
> ^
>
> scala> mab(b1)
> :10: error: inferred type arguments [java.lang.Object with B] do not conform to method mab's type parameter bounds [E <: A with B]
> mab(b1)
> ^
>
> scala> mba(a1)
> :10: error: inferred type arguments [java.lang.Object with A] do not conform to method mba's type parameter bounds [E <: B with A]
> mba(a1)
> ^
>
> scala> mba(b1)
> :10: error: inferred type arguments [java.lang.Object with B] do not conform to method mba's type parameter bounds [E <: B with A]
> mba(b1)
> ^
> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>
>
> Randall Schulz
>

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: why does this not compile?
scala.Int conforms to AnyVal; java.lang.Integer doesn't.

On Fri, Nov 26, 2010 at 7:57 PM, HamsterofDeath <h-star@gmx.de> wrote:
that worked, but i cannot call the method anymore. if i use this definition
def findHighestAndRating[C <: AnyVal with Comparable[C]](f: T => C)

and try to use a function that returns java.lang.Integer, i get an error
about Integer not conforming the type parameter bounds. but Integer is
an AnyVal and implements Comparable[Integer] so i'd say it matches the
bound.
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: why does this not compile?

doesn't work with Any instead of AnyVal either

Am 26.11.2010 19:57, schrieb HamsterofDeath:
> that worked, but i cannot call the method anymore. if i use this definition
> def findHighestAndRating[C <: AnyVal with Comparable[C]](f: T => C)
>
> and try to use a function that returns java.lang.Integer, i get an error
> about Integer not conforming the type parameter bounds. but Integer is
> an AnyVal and implements Comparable[Integer] so i'd say it matches the
> bound.
>
>
> Am 26.11.2010 18:50, schrieb Randall R Schulz:
>> On Friday November 26 2010, HamsterofDeath wrote:
>>> the core problem is:
>>> previously, i used a function T => Double to rate elements of a list
>>> and collect the highest one. i also returned the double. no problem
>>> now, if i want to allow any comparable type as a rating, the rating
>>> function must return "Comparable[X] AND X", meaning it must be
>>> something like Double, which is both Double and Comparable[Double].
>>> in java, you could write that as void foo
>>>
>>> is this possible in scala?
>> def foo[X <: A with B](...): Unit = ...
>>
>> I had thought that linearization of A with B (vs. B with A) would make
>> make the two incompatible, but that is evidently not so. See below.
>>
>>
>> To wit:
>> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>> Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_22).
>> Type in expressions to have them evaluated.
>> Type :help for more information.
>>
>> scala> trait A { def a: String = "A" }
>> defined trait A
>>
>> scala> trait B { def b: String = "B" }
>> defined trait B
>>
>>
>> scala> val a1 = new A {}
>> a1: java.lang.Object with A = $anon$1@1a66c87
>>
>> scala> val b1 = new B {}
>> b1: java.lang.Object with B = $anon$1@1d626a4
>>
>>
>> scala> val ab1 = new A with B {}
>> ab1: java.lang.Object with A with B = $anon$1@1bdcbb2
>>
>> scala> val ba1 = new B with A {}
>> ba1: java.lang.Object with B with A = $anon$1@15ddf5
>>
>>
>> scala> def mab[E <: A with B](e: E): String = "mab:%s|%s".format(e.a, e.b)
>> mab: [E <: A with B](e: E)String
>>
>> scala> def mba[E <: B with A](e: E): String = "mba:%s|%s".format(e.b, e.a)
>> mba: [E <: B with A](e: E)String
>>
>>
>> scala> mab(ab1)
>> res0: String = mab:A|B
>>
>> scala> mba(ba1)
>> res1: String = mba:B|A
>>
>> scala> mab(ba1)
>> res2: String = mab:A|B
>>
>> scala> mba(ab1)
>> res3: String = mba:B|A
>>
>>
>> scala> mab(a1)
>> :10: error: inferred type arguments [java.lang.Object with A] do not conform to method mab's type parameter bounds [E <: A with B]
>> mab(a1)
>> ^
>>
>> scala> mab(b1)
>> :10: error: inferred type arguments [java.lang.Object with B] do not conform to method mab's type parameter bounds [E <: A with B]
>> mab(b1)
>> ^
>>
>> scala> mba(a1)
>> :10: error: inferred type arguments [java.lang.Object with A] do not conform to method mba's type parameter bounds [E <: B with A]
>> mba(a1)
>> ^
>>
>> scala> mba(b1)
>> :10: error: inferred type arguments [java.lang.Object with B] do not conform to method mba's type parameter bounds [E <: B with A]
>> mba(b1)
>> ^
>> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>>
>>
>> Randall Schulz
>>
>

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: why does this not compile?

i'm confused today.
of course Any with Comparable[C] accepts java.lang.Integer, but not
scala's Int.
any way to fix that?
sorry for all the chaotic mails

Am 26.11.2010 19:57, schrieb HamsterofDeath:
> that worked, but i cannot call the method anymore. if i use this definition
> def findHighestAndRating[C <: AnyVal with Comparable[C]](f: T => C)
>
> and try to use a function that returns java.lang.Integer, i get an error
> about Integer not conforming the type parameter bounds. but Integer is
> an AnyVal and implements Comparable[Integer] so i'd say it matches the
> bound.
>
>
> Am 26.11.2010 18:50, schrieb Randall R Schulz:
>> On Friday November 26 2010, HamsterofDeath wrote:
>>> the core problem is:
>>> previously, i used a function T => Double to rate elements of a list
>>> and collect the highest one. i also returned the double. no problem
>>> now, if i want to allow any comparable type as a rating, the rating
>>> function must return "Comparable[X] AND X", meaning it must be
>>> something like Double, which is both Double and Comparable[Double].
>>> in java, you could write that as void foo
>>>
>>> is this possible in scala?
>> def foo[X <: A with B](...): Unit = ...
>>
>> I had thought that linearization of A with B (vs. B with A) would make
>> make the two incompatible, but that is evidently not so. See below.
>>
>>
>> To wit:
>> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>> Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_22).
>> Type in expressions to have them evaluated.
>> Type :help for more information.
>>
>> scala> trait A { def a: String = "A" }
>> defined trait A
>>
>> scala> trait B { def b: String = "B" }
>> defined trait B
>>
>>
>> scala> val a1 = new A {}
>> a1: java.lang.Object with A = $anon$1@1a66c87
>>
>> scala> val b1 = new B {}
>> b1: java.lang.Object with B = $anon$1@1d626a4
>>
>>
>> scala> val ab1 = new A with B {}
>> ab1: java.lang.Object with A with B = $anon$1@1bdcbb2
>>
>> scala> val ba1 = new B with A {}
>> ba1: java.lang.Object with B with A = $anon$1@15ddf5
>>
>>
>> scala> def mab[E <: A with B](e: E): String = "mab:%s|%s".format(e.a, e.b)
>> mab: [E <: A with B](e: E)String
>>
>> scala> def mba[E <: B with A](e: E): String = "mba:%s|%s".format(e.b, e.a)
>> mba: [E <: B with A](e: E)String
>>
>>
>> scala> mab(ab1)
>> res0: String = mab:A|B
>>
>> scala> mba(ba1)
>> res1: String = mba:B|A
>>
>> scala> mab(ba1)
>> res2: String = mab:A|B
>>
>> scala> mba(ab1)
>> res3: String = mba:B|A
>>
>>
>> scala> mab(a1)
>> :10: error: inferred type arguments [java.lang.Object with A] do not conform to method mab's type parameter bounds [E <: A with B]
>> mab(a1)
>> ^
>>
>> scala> mab(b1)
>> :10: error: inferred type arguments [java.lang.Object with B] do not conform to method mab's type parameter bounds [E <: A with B]
>> mab(b1)
>> ^
>>
>> scala> mba(a1)
>> :10: error: inferred type arguments [java.lang.Object with A] do not conform to method mba's type parameter bounds [E <: B with A]
>> mba(a1)
>> ^
>>
>> scala> mba(b1)
>> :10: error: inferred type arguments [java.lang.Object with B] do not conform to method mba's type parameter bounds [E <: B with A]
>> mba(b1)
>> ^
>> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>>
>>
>> Randall Schulz
>>
>

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: why does this not compile?

i browsed around in scala's predef & friends and tried out that:
def findHighestAndRating[C](f: T => C, filter: T => Boolean = (T) =>
true, ifEqual: (T, T) => T = (t1, t2) => t2)(implicit ord: Ordering[C])

the compiler seems to be able to get an ordering on C from somewhere if
its an anyval, but it fails on java.lang.Integer.
same for implicit C => Ordered[C]

but implicit C => Comparable[C] works for both comparable classes like
java numbers and anyvals.
glory to the scala user list :D

however, the function C => Comparable[C] will convert each element that
is returned by the rating function into an comparable - even if it
already is one!

so, if i want to use this signature:
def findHighest[C](f: T => C, filter: T => Boolean = (T) => true,
ifEqual: (T, T) => T = (t1, t2) => t2)(implicit ordering: Ordering[C])

and use ordering.compare() to save some object allocations, i need to
put *something* into the class that can be implicitly used to order C
i made 4 orderings based on java.lang.integer, long, float and double.
too bad i cannot make a generic ordering that can handle Comparable[x]

*levelup on typesystem and implicits knowlege*

Am 26.11.2010 20:10, schrieb HamsterofDeath:
> i'm confused today.
> of course Any with Comparable[C] accepts java.lang.Integer, but not
> scala's Int.
> any way to fix that?
> sorry for all the chaotic mails
>
> Am 26.11.2010 19:57, schrieb HamsterofDeath:
>> that worked, but i cannot call the method anymore. if i use this definition
>> def findHighestAndRating[C <: AnyVal with Comparable[C]](f: T => C)
>>
>> and try to use a function that returns java.lang.Integer, i get an error
>> about Integer not conforming the type parameter bounds. but Integer is
>> an AnyVal and implements Comparable[Integer] so i'd say it matches the
>> bound.
>>
>>
>> Am 26.11.2010 18:50, schrieb Randall R Schulz:
>>> On Friday November 26 2010, HamsterofDeath wrote:
>>>> the core problem is:
>>>> previously, i used a function T => Double to rate elements of a list
>>>> and collect the highest one. i also returned the double. no problem
>>>> now, if i want to allow any comparable type as a rating, the rating
>>>> function must return "Comparable[X] AND X", meaning it must be
>>>> something like Double, which is both Double and Comparable[Double].
>>>> in java, you could write that as void foo
>>>>
>>>> is this possible in scala?
>>> def foo[X <: A with B](...): Unit = ...
>>>
>>> I had thought that linearization of A with B (vs. B with A) would make
>>> make the two incompatible, but that is evidently not so. See below.
>>>
>>>
>>> To wit:
>>> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>>> Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_22).
>>> Type in expressions to have them evaluated.
>>> Type :help for more information.
>>>
>>> scala> trait A { def a: String = "A" }
>>> defined trait A
>>>
>>> scala> trait B { def b: String = "B" }
>>> defined trait B
>>>
>>>
>>> scala> val a1 = new A {}
>>> a1: java.lang.Object with A = $anon$1@1a66c87
>>>
>>> scala> val b1 = new B {}
>>> b1: java.lang.Object with B = $anon$1@1d626a4
>>>
>>>
>>> scala> val ab1 = new A with B {}
>>> ab1: java.lang.Object with A with B = $anon$1@1bdcbb2
>>>
>>> scala> val ba1 = new B with A {}
>>> ba1: java.lang.Object with B with A = $anon$1@15ddf5
>>>
>>>
>>> scala> def mab[E <: A with B](e: E): String = "mab:%s|%s".format(e.a, e.b)
>>> mab: [E <: A with B](e: E)String
>>>
>>> scala> def mba[E <: B with A](e: E): String = "mba:%s|%s".format(e.b, e.a)
>>> mba: [E <: B with A](e: E)String
>>>
>>>
>>> scala> mab(ab1)
>>> res0: String = mab:A|B
>>>
>>> scala> mba(ba1)
>>> res1: String = mba:B|A
>>>
>>> scala> mab(ba1)
>>> res2: String = mab:A|B
>>>
>>> scala> mba(ab1)
>>> res3: String = mba:B|A
>>>
>>>
>>> scala> mab(a1)
>>> :10: error: inferred type arguments [java.lang.Object with A] do not conform to method mab's type parameter bounds [E <: A with B]
>>> mab(a1)
>>> ^
>>>
>>> scala> mab(b1)
>>> :10: error: inferred type arguments [java.lang.Object with B] do not conform to method mab's type parameter bounds [E <: A with B]
>>> mab(b1)
>>> ^
>>>
>>> scala> mba(a1)
>>> :10: error: inferred type arguments [java.lang.Object with A] do not conform to method mba's type parameter bounds [E <: B with A]
>>> mba(a1)
>>> ^
>>>
>>> scala> mba(b1)
>>> :10: error: inferred type arguments [java.lang.Object with B] do not conform to method mba's type parameter bounds [E <: B with A]
>>> mba(b1)
>>> ^
>>> -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
>>>
>>>
>>> Randall Schulz
>>>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland