Problem casting Int to Double (easy!)

106 views
Skip to first unread message

Eric Fowler

unread,
Feb 28, 2015, 1:17:23 AM2/28/15
to scala...@googlegroups.com
I am going through Odersky's book, and hung up on:

class Queue[+T](private val leading:T)
{
 
def enqueue[U >: T](x:U) = new Queue[U](leading:U)
}

class StrangeIntQueue(private val leading:Int) extends Queue[Int](leading)
{
 
override def enqueue[Int](x:Int) = {
   
println( math.sqrt(x) )
   
super.enqueue(x).asInstanceOf[Queue[Int]]
 
}
}
The problematic code is: println( math.sqrt(x) )

Scala does not like that integer being passed to sqrt() [it expects a Double]. 
Taking x.asInstanceOf just gets a runtime exception. 

This is exactly as it is in the book. 

What is going on? Basically, I understand that the problem is type conversion, but how does anyone get past page 397?



Eric Fowler

unread,
Feb 28, 2015, 1:23:17 AM2/28/15
to scala...@googlegroups.com
Another, possibly related issue:

class StrangeIntQueue extends Queue[Int]

{
override def enqueue[Int](x:Int) = {
    println( x * x )
super.enqueue(x).asInstanceOf[Queue[Int]]
}
}


This has a problem with the '*' in 'x * x' ... complaint is 'Cannot resolve symbol '*''. 

Now *that* is fussy. 

What is happening?

Eric


Som Snytt

unread,
Feb 28, 2015, 1:36:27 AM2/28/15
to Eric Fowler, scala-user
Use -Xlint for the warning:

$ scala -Xlint
Welcome to Scala version 2.11.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :pa
// Entering paste mode (ctrl-D to finish)


class Queue[+T](private val leading:T)
{
  def enqueue[U >: T](x:U) = new Queue[U](leading:U)
}

class StrangeIntQueue(private val leading:Int) extends Queue[Int](leading)
{
  override def enqueue[Int](x:Int) = {
    println( math.sqrt(x) )
    super.enqueue(x).asInstanceOf[Queue[Int]]
  }
}

// Exiting paste mode, now interpreting.

<console>:15: error: type mismatch;
 found   : Int
 required: Double
           println( math.sqrt(x) )
                              ^
<console>:14: warning: a type was inferred to be `Any`; this may indicate a programming error.

         override def enqueue[Int](x:Int) = {
                                            ^
<console>:14: warning: type parameter Int defined in method enqueue shadows class Int defined in package scala. You may want to rename your type parameter, or possibly remove it.

         override def enqueue[Int](x:Int) = {
                              ^


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric Fowler

unread,
Feb 28, 2015, 11:46:34 PM2/28/15
to scala...@googlegroups.com, eric....@gmail.com
Well, that is informative, and I learned how to use a new tool. 

But I am puzzled as to why the Scala compiler thinks that when I write: 

class A[T] {}

class Foo[Int] extends A[Int]
{
 val n:Int

That I mean: 
"a class with a parameterized type which I hereby name 'Int' " and not:
"a class with a type parameter that is of type Int". 

???

Eric

Naftoli Gugenheim

unread,
Feb 28, 2015, 11:57:50 PM2/28/15
to Eric Fowler, scala...@googlegroups.com
On Sat, Feb 28, 2015 at 11:46 PM Eric Fowler <eric....@gmail.com> wrote:
Well, that is informative, and I learned how to use a new tool. 

But I am puzzled as to why the Scala compiler thinks that when I write: 

class A[T] {}

class Foo[Int] extends A[Int]
{
 val n:Int

That I mean: 
"a class with a parameterized type which I hereby name 'Int' " and not:
"a class with a type parameter that is of type Int".  

???

What would that even mean?

Thing of the analogue in value parameters. Can you have a function that takes a parameter with value 10?

def f(10) = ???

Parameters by definition are "holes" that are filled in at the call site.

Eric Fowler

unread,
Mar 1, 2015, 12:19:21 AM3/1/15
to scala...@googlegroups.com, eric....@gmail.com
Well, I think in C++. It is a handicap of mine: 

///C++ here
template<typename T>
class A<T>
{
public:
void foo(T t); /// give me a T, whatever that may be
};

class B : public A<int>
{
// in here, foo(T t) means foo(int t)
}


class C : public A<float>
{
//ditto, but float...
}

So why doesn't : 
class X extends A[Int] {}

... mean that I am declaring X to be a specialization of A, "templated" (as C++ folks say) on Int?

Back to the book. ... 

Eric

Vlad Patryshev

unread,
Mar 1, 2015, 12:26:35 AM3/1/15
to Eric Fowler, scala-user
This is a very good puzzle!

Hope you know the answer already.

Thanks,
-Vlad

On Fri, Feb 27, 2015 at 10:23 PM, Eric Fowler <eric....@gmail.com> wrote:

Naftoli Gugenheim

unread,
Mar 1, 2015, 1:37:31 AM3/1/15
to Eric Fowler, scala...@googlegroups.com
On Sun, Mar 1, 2015 at 12:19 AM Eric Fowler <eric....@gmail.com> wrote:
Well, I think in C++. It is a handicap of mine: 

///C++ here
template<typename T>
class A<T>
{
public:
void foo(T t); /// give me a T, whatever that may be
};

class B : public A<int>
{
// in here, foo(T t) means foo(int t)
}


class C : public A<float>
{
//ditto, but float...
}

So why doesn't : 
class X extends A[Int] {}

... mean that I am declaring X to be a specialization of A, "templated" (as C++ folks say) on Int?

It does! Here, you are at the "call site" so to speak of A.

Before you wrote

class Foo[Int] extends A[Int]

The method + value parameter analogy would be

def f(10) = g(10)

class X extends A[Int] {}

is good, it's analogous to

def f = g(10)

Som Snytt

unread,
Mar 1, 2015, 2:34:05 AM3/1/15
to Naftoli Gugenheim, Eric Fowler, scala-user

typelevel$ ./build/pack/bin/scala -Xexperimental
Welcome to Scala version 2.11.5-20141117-134839-c9b27319e4 (OpenJDK 64-Bit Server VM, Java 1.7.0_65).

Type in expressions to have them evaluated.
Type :help for more information.

scala> def f(x: 10.type) = x
f: (x: 10)Int

scala> f(42)
<console>:9: error: type mismatch;
 found   : Int(42)
 required: 10
              f(42)
                ^

scala> f(10)
res1: Int = 10

scala>


Naftoli Gugenheim

unread,
Mar 1, 2015, 2:47:52 AM3/1/15
to Som Snytt, Eric Fowler, scala-user
Okay okay I surrender
Reply all
Reply to author
Forward
0 new messages