Re: newbie questions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: newbie questions

62945

> The answer is in the interface of java.lang.Integer and seems to be in
> the intValue() method. So you should write
>
>   a = b.intValue()

this works fine !

> If you use this often you better define implicit conversion from the
> Java box types to Scala numerical types like (the name of the mehod
> doesn't matter)
>
>   implicit def integer2Int(x: java.lang.Integer): Int = x.intValue()
>
> and make sure it is visible at the point of use without qualification.

could you provide a detailed example ? your definition is accepted ok by the interpreter but i don't see how it can be used. I would expect to be able to define/extend "=" as a way to coerce other values between numeric types

a = b    // a: Int && b: Integer
a = d    // a: Int && d: Double
a = B    // a: Int && B: BigInt

why doesn't the following work

def x: int = 0
def x_= (z: Integer): int = z.intValue  // return type int or unit ?

it would be nice to write

a = b = d

ah, one more question: can you do bit operations on integers ?


Reply | Threaded
Open this post in threaded view
|

Re: newbie questions

Iulian Dragos-2
  >>If you use this often you better define implicit conversion from the
>>Java box types to Scala numerical types like (the name of the mehod
>>doesn't matter)
>>
>>  implicit def integer2Int(x: java.lang.Integer): Int = x.intValue()
>>
>>and make sure it is visible at the point of use without qualification.
>
>
> could you provide a detailed example ? your definition is accepted ok by the interpreter but i don't see how it can be used. I would expect to be able to define/extend "=" as a way to coerce other values between numeric types

Implicit definitions provide a way to instruct the compiler about
user-defined coercions. So the implicit def seen above says: "whenever
you need an Int, but you see a java.lang.Integer" apply this function
and use it's result. The compiler inserts these functions when needed,
provided they are available in the current scope (without
qualification). Here's a complete example:


object Main extends Application {
   var x: int = 0

   implicit def integer2Int(x: java.lang.Integer): Int = x.intValue();

   x = new java.lang.Integer(10); // assign an Integer to an Int
   Console.println(x);
}

Note that you don't need to override the setter for x.

This technique is quite powerful, so if you're interested I suggest you
have a look at http://scala.epfl.ch/intro/views.html and
http://scala.epfl.ch/intro/implicit.html

> a = b    // a: Int && b: Integer
> a = d    // a: Int && d: Double
> a = B    // a: Int && B: BigInt
>
> why doesn't the following work
>
> def x: int = 0
> def x_= (z: Integer): int = z.intValue  // return type int or unit ?

Here the setter is a 'do-nothing': it returns the intValue, but that is
not assigned to the field. Here's a modified example to use your technique:

object Main extends Application {
   private var _x: int = 0
   def x: Int = _x;
   def x_= (z: Integer): Unit = (_x = z.intValue)

   x = new java.lang.Integer(10);
   Console.println(x);
}

> it would be nice to write
>
> a = b = d

Indeed, but in Scala you can write that as

a, b = d

Here's an example:

   var x = 0;
   var a, b, c = { x = x + 1; x}

   Console.println("" + a + ", " + b + ", " + c);

That should print 1, 2, 3. The second line is expanded into 3 var
definitions, each getting a copy of the right-hand side expression.

> ah, one more question: can you do bit operations on integers ?

Yes. All operations available in Java are available in Scala (with the
same operators).

Iulian