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

Use of java.lang.Long in scala code

8 replies
Konstantinos Ka...
Joined: 2009-04-27,
User offline. Last seen 42 years 45 weeks ago.
Hi,

This might be silly ... Do you how to set a java.lang.Long variable to a var.

I need to use java.lang.Long  because of datanucleus-enhance bytecode instrumentation fails otherwise.

My code looks like:

package org.kkarad.liftplayground.model

import javax.jdo.annotations._

@PersistenceCapable{val identityType = IdentityType.APPLICATION}
class Candidate {

  @PrimaryKey
  @Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
  var id: java.lang.Long = -1

  @Persistent
  var content: String = ""
}

The compiler error is:
error: type mismatch;
 found   : Int(-1)
 required: java.lang.Long
  var id: java.lang.Long = -1
                            ^

Thank you in advance,

Kostas

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Use of java.lang.Long in scala code

scala> var lng: java.lang.Long = -1L
lng: java.lang.Long = -1

scala>




On Fri, Nov 20, 2009 at 4:05 PM, Konstantinos Karadamoglou <kkarad@googlemail.com> wrote:
Hi,

This might be silly ... Do you how to set a java.lang.Long variable to a var.

I need to use java.lang.Long  because of datanucleus-enhance bytecode instrumentation fails otherwise.

My code looks like:

package org.kkarad.liftplayground.model

import javax.jdo.annotations._

@PersistenceCapable{val identityType = IdentityType.APPLICATION}
class Candidate {

  @PrimaryKey
  @Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
  var id: java.lang.Long = -1

  @Persistent
  var content: String = ""
}

The compiler error is:
error: type mismatch;
 found   : Int(-1)
 required: java.lang.Long
  var id: java.lang.Long = -1
                            ^

Thank you in advance,

Kostas




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: Use of java.lang.Long in scala code

I think this version is a tiny bit more efficient, don't take my word
for it though. You may also prefer the look of the thing:

val lng = java.lang.Long.valueOf(-1L)

I've not thrown this one at my compiler either... :(

On Saturday, November 21, 2009, David Pollak
wrote:
>
> scala> var lng: java.lang.Long = -1L
> lng: java.lang.Long = -1
>
> scala>
>
>
>
>
> On Fri, Nov 20, 2009 at 4:05 PM, Konstantinos Karadamoglou > wrote:
> Hi,
>
> This might be silly ... Do you how to set a java.lang.Long variable to a var.
>
> I need to use java.lang.Long  because of datanucleus-enhance bytecode instrumentation fails otherwise.
>
> My code looks like:
>
> package org.kkarad.liftplayground.model
>
> import javax.jdo.annotations._
>
> @PersistenceCapable{val identityType = IdentityType.APPLICATION}
> class Candidate {
>
>   @PrimaryKey
>   @Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
>   var id: java.lang.Long = -1
>
>   @Persistent
>   var content: String = ""
> }
>
> The compiler error is:
> error: type mismatch;
>  found   : Int(-1)
>  required: java.lang.Long
>   var id: java.lang.Long = -1
>                             ^
>
> Thank you in advance,
>
> Kostas
>
>
>
> --
> Lift, the simply functional web framework http://liftweb.net
> Beginning Scala http://www.apress.com/book/view/1430219890
> Follow me: http://twitter.com/dpp
> Surf the harmonics
>

Konstantinos Ka...
Joined: 2009-04-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Use of java.lang.Long in scala code
Thank you

On Sat, Nov 21, 2009 at 12:30 AM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:
I think this version is a tiny bit more efficient, don't take my word
for it though.  You may also prefer the look of the thing:

val lng = java.lang.Long.valueOf(-1L)

I've not thrown this one at my compiler either...  :(


On Saturday, November 21, 2009, David Pollak
<feeder.of.the.bears@gmail.com> wrote:
>
> scala> var lng: java.lang.Long = -1L
> lng: java.lang.Long = -1
>
> scala>
>
>
>
>
> On Fri, Nov 20, 2009 at 4:05 PM, Konstantinos Karadamoglou <kkarad@googlemail.com <javascript:_e({}, 'cvml', 'kkarad@googlemail.com');>> wrote:
> Hi,
>
> This might be silly ... Do you how to set a java.lang.Long variable to a var.
>
> I need to use java.lang.Long  because of datanucleus-enhance bytecode instrumentation fails otherwise.
>
> My code looks like:
>
> package org.kkarad.liftplayground.model
>
> import javax.jdo.annotations._
>
> @PersistenceCapable{val identityType = IdentityType.APPLICATION}
> class Candidate {
>
>   @PrimaryKey
>   @Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
>   var id: java.lang.Long = -1
>
>   @Persistent
>   var content: String = ""
> }
>
> The compiler error is:
> error: type mismatch;
>  found   : Int(-1)
>  required: java.lang.Long
>   var id: java.lang.Long = -1
>                             ^
>
> Thank you in advance,
>
> Kostas
>
>
>
> --
> Lift, the simply functional web framework http://liftweb.net
> Beginning Scala http://www.apress.com/book/view/1430219890
> Follow me: http://twitter.com/dpp
> Surf the harmonics
>

Konstantinos Ka...
Joined: 2009-04-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Use of java.lang.Long in scala code
Another question is how can I convert scala.Long to java.lang.Long and java.lang.Long to scala.Long?

Thank you in advance.

On Sat, Nov 21, 2009 at 12:54 AM, Konstantinos Karadamoglou <kkarad@googlemail.com> wrote:
Thank you

On Sat, Nov 21, 2009 at 12:30 AM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:
I think this version is a tiny bit more efficient, don't take my word
for it though.  You may also prefer the look of the thing:

val lng = java.lang.Long.valueOf(-1L)

I've not thrown this one at my compiler either...  :(


On Saturday, November 21, 2009, David Pollak
<feeder.of.the.bears@gmail.com> wrote:
>
> scala> var lng: java.lang.Long = -1L
> lng: java.lang.Long = -1
>
> scala>
>
>
>
>
> On Fri, Nov 20, 2009 at 4:05 PM, Konstantinos Karadamoglou <kkarad@googlemail.com <javascript:_e({}, 'cvml', 'kkarad@googlemail.com');>> wrote:
> Hi,
>
> This might be silly ... Do you how to set a java.lang.Long variable to a var.
>
> I need to use java.lang.Long  because of datanucleus-enhance bytecode instrumentation fails otherwise.
>
> My code looks like:
>
> package org.kkarad.liftplayground.model
>
> import javax.jdo.annotations._
>
> @PersistenceCapable{val identityType = IdentityType.APPLICATION}
> class Candidate {
>
>   @PrimaryKey
>   @Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
>   var id: java.lang.Long = -1
>
>   @Persistent
>   var content: String = ""
> }
>
> The compiler error is:
> error: type mismatch;
>  found   : Int(-1)
>  required: java.lang.Long
>   var id: java.lang.Long = -1
>                             ^
>
> Thank you in advance,
>
> Kostas
>
>
>
> --
> Lift, the simply functional web framework http://liftweb.net
> Beginning Scala http://www.apress.com/book/view/1430219890
> Follow me: http://twitter.com/dpp
> Surf the harmonics
>


Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Use of java.lang.Long in scala code

java.lang.Long.valueOf(scala.Long)

someJavaLong.longValue

2009/11/21 Konstantinos Karadamoglou :
> Another question is how can I convert scala.Long to java.lang.Long and
> java.lang.Long to scala.Long?
>
> Thank you in advance.
>
> On Sat, Nov 21, 2009 at 12:54 AM, Konstantinos Karadamoglou
> wrote:
>>
>> Thank you
>>
>> On Sat, Nov 21, 2009 at 12:30 AM, Kevin Wright
>> wrote:
>>>
>>> I think this version is a tiny bit more efficient, don't take my word
>>> for it though.  You may also prefer the look of the thing:
>>>
>>> val lng = java.lang.Long.valueOf(-1L)
>>>
>>> I've not thrown this one at my compiler either...  :(
>>>
>>>
>>> On Saturday, November 21, 2009, David Pollak
>>> wrote:
>>> >
>>> > scala> var lng: java.lang.Long = -1L
>>> > lng: java.lang.Long = -1
>>> >
>>> > scala>
>>> >
>>> >
>>> >
>>> >
>>> > On Fri, Nov 20, 2009 at 4:05 PM, Konstantinos Karadamoglou
>>> > >> > 'kkarad@googlemail.com');>> wrote:
>>> > Hi,
>>> >
>>> > This might be silly ... Do you how to set a java.lang.Long variable to
>>> > a var.
>>> >
>>> > I need to use java.lang.Long  because of datanucleus-enhance bytecode
>>> > instrumentation fails otherwise.
>>> >
>>> > My code looks like:
>>> >
>>> > package org.kkarad.liftplayground.model
>>> >
>>> > import javax.jdo.annotations._
>>> >
>>> > @PersistenceCapable{val identityType = IdentityType.APPLICATION}
>>> > class Candidate {
>>> >
>>> >   @PrimaryKey
>>> >   @Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
>>> >   var id: java.lang.Long = -1
>>> >
>>> >   @Persistent
>>> >   var content: String = ""
>>> > }
>>> >
>>> > The compiler error is:
>>> > error: type mismatch;
>>> >  found   : Int(-1)
>>> >  required: java.lang.Long
>>> >   var id: java.lang.Long = -1
>>> >                             ^
>>> >
>>> > Thank you in advance,
>>> >
>>> > Kostas
>>> >
>>> >
>>> >
>>> > --
>>> > Lift, the simply functional web framework http://liftweb.net
>>> > Beginning Scala http://www.apress.com/book/view/1430219890
>>> > Follow me: http://twitter.com/dpp
>>> > Surf the harmonics
>>> >
>>
>
>

Konstantinos Ka...
Joined: 2009-04-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Use of java.lang.Long in scala code
Thanks Ricky,

What about java.lang.Long to scala.Long ?

I tried a simple assignment but it fails.
scala> val x: Long = java.lang.Long.valueOf(1)
<console>:4: error: type mismatch;
 found   : java.lang.Long
 required: Long
       val x: Long = java.lang.Long.valueOf(1)

I am assuming these conversions are documented somewhere. Does anyone know where to find the relevant documentation?

Kostas

On Sat, Nov 21, 2009 at 8:36 PM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
java.lang.Long.valueOf(scala.Long)

someJavaLong.longValue

2009/11/21 Konstantinos Karadamoglou <kkarad@googlemail.com>:
> Another question is how can I convert scala.Long to java.lang.Long and
> java.lang.Long to scala.Long?
>
> Thank you in advance.
>
> On Sat, Nov 21, 2009 at 12:54 AM, Konstantinos Karadamoglou
> <kkarad@googlemail.com> wrote:
>>
>> Thank you
>>
>> On Sat, Nov 21, 2009 at 12:30 AM, Kevin Wright
>> <kev.lee.wright@googlemail.com> wrote:
>>>
>>> I think this version is a tiny bit more efficient, don't take my word
>>> for it though.  You may also prefer the look of the thing:
>>>
>>> val lng = java.lang.Long.valueOf(-1L)
>>>
>>> I've not thrown this one at my compiler either...  :(
>>>
>>>
>>> On Saturday, November 21, 2009, David Pollak
>>> <feeder.of.the.bears@gmail.com> wrote:
>>> >
>>> > scala> var lng: java.lang.Long = -1L
>>> > lng: java.lang.Long = -1
>>> >
>>> > scala>
>>> >
>>> >
>>> >
>>> >
>>> > On Fri, Nov 20, 2009 at 4:05 PM, Konstantinos Karadamoglou
>>> > <kkarad@googlemail.com <javascript:_e({}, 'cvml',
>>> > 'kkarad@googlemail.com');>> wrote:
>>> > Hi,
>>> >
>>> > This might be silly ... Do you how to set a java.lang.Long variable to
>>> > a var.
>>> >
>>> > I need to use java.lang.Long  because of datanucleus-enhance bytecode
>>> > instrumentation fails otherwise.
>>> >
>>> > My code looks like:
>>> >
>>> > package org.kkarad.liftplayground.model
>>> >
>>> > import javax.jdo.annotations._
>>> >
>>> > @PersistenceCapable{val identityType = IdentityType.APPLICATION}
>>> > class Candidate {
>>> >
>>> >   @PrimaryKey
>>> >   @Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
>>> >   var id: java.lang.Long = -1
>>> >
>>> >   @Persistent
>>> >   var content: String = ""
>>> > }
>>> >
>>> > The compiler error is:
>>> > error: type mismatch;
>>> >  found   : Int(-1)
>>> >  required: java.lang.Long
>>> >   var id: java.lang.Long = -1
>>> >                             ^
>>> >
>>> > Thank you in advance,
>>> >
>>> > Kostas
>>> >
>>> >
>>> >
>>> > --
>>> > Lift, the simply functional web framework http://liftweb.net
>>> > Beginning Scala http://www.apress.com/book/view/1430219890
>>> > Follow me: http://twitter.com/dpp
>>> > Surf the harmonics
>>> >
>>
>
>



--
Ricky Clarkson
Java and Scala Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@gmail.com
Google Wave: ricky.clarkson@googlewave.com

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: Use of java.lang.Long in scala code
Instead of looking for exaples in specific cases, it's easier to remember that scala.Long is directly equivalent to the long primitive in Java.When calling a Java function that needs a long, you supply a scala.Long, when a java function returns a long, you'll see a scala.Long
In the compiled bytecode, there's absolutely no difference between long an scala.Long, it's all a trick of the compiler...
So in answer to your question, to convert a java.lang.Long to a scala.Long, just call longValue on it.

On Sat, Nov 21, 2009 at 9:44 PM, Konstantinos Karadamoglou <kkarad@googlemail.com> wrote:
Thanks Ricky,

What about java.lang.Long to scala.Long ?

I tried a simple assignment but it fails.
scala> val x: Long = java.lang.Long.valueOf(1)
<console>:4: error: type mismatch;
 found   : java.lang.Long
 required: Long
       val x: Long = java.lang.Long.valueOf(1)

I am assuming these conversions are documented somewhere. Does anyone know where to find the relevant documentation?

Kostas

On Sat, Nov 21, 2009 at 8:36 PM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
java.lang.Long.valueOf(scala.Long)

someJavaLong.longValue

2009/11/21 Konstantinos Karadamoglou <kkarad@googlemail.com>:
> Another question is how can I convert scala.Long to java.lang.Long and
> java.lang.Long to scala.Long?
>
> Thank you in advance.
>
> On Sat, Nov 21, 2009 at 12:54 AM, Konstantinos Karadamoglou
> <kkarad@googlemail.com> wrote:
>>
>> Thank you
>>
>> On Sat, Nov 21, 2009 at 12:30 AM, Kevin Wright
>> <kev.lee.wright@googlemail.com> wrote:
>>>
>>> I think this version is a tiny bit more efficient, don't take my word
>>> for it though.  You may also prefer the look of the thing:
>>>
>>> val lng = java.lang.Long.valueOf(-1L)
>>>
>>> I've not thrown this one at my compiler either...  :(
>>>
>>>
>>> On Saturday, November 21, 2009, David Pollak
>>> <feeder.of.the.bears@gmail.com> wrote:
>>> >
>>> > scala> var lng: java.lang.Long = -1L
>>> > lng: java.lang.Long = -1
>>> >
>>> > scala>
>>> >
>>> >
>>> >
>>> >
>>> > On Fri, Nov 20, 2009 at 4:05 PM, Konstantinos Karadamoglou
>>> > <kkarad@googlemail.com <javascript:_e({}, 'cvml',
>>> > 'kkarad@googlemail.com');>> wrote:
>>> > Hi,
>>> >
>>> > This might be silly ... Do you how to set a java.lang.Long variable to
>>> > a var.
>>> >
>>> > I need to use java.lang.Long  because of datanucleus-enhance bytecode
>>> > instrumentation fails otherwise.
>>> >
>>> > My code looks like:
>>> >
>>> > package org.kkarad.liftplayground.model
>>> >
>>> > import javax.jdo.annotations._
>>> >
>>> > @PersistenceCapable{val identityType = IdentityType.APPLICATION}
>>> > class Candidate {
>>> >
>>> >   @PrimaryKey
>>> >   @Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
>>> >   var id: java.lang.Long = -1
>>> >
>>> >   @Persistent
>>> >   var content: String = ""
>>> > }
>>> >
>>> > The compiler error is:
>>> > error: type mismatch;
>>> >  found   : Int(-1)
>>> >  required: java.lang.Long
>>> >   var id: java.lang.Long = -1
>>> >                             ^
>>> >
>>> > Thank you in advance,
>>> >
>>> > Kostas
>>> >
>>> >
>>> >
>>> > --
>>> > Lift, the simply functional web framework http://liftweb.net
>>> > Beginning Scala http://www.apress.com/book/view/1430219890
>>> > Follow me: http://twitter.com/dpp
>>> > Surf the harmonics
>>> >
>>
>
>



--
Ricky Clarkson
Java and Scala Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@gmail.com
Google Wave: ricky.clarkson@googlewave.com


Konstantinos Ka...
Joined: 2009-04-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Use of java.lang.Long in scala code
Thanks Kevin

On Sat, Nov 21, 2009 at 10:00 PM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:
Instead of looking for exaples in specific cases, it's easier to remember that scala.Long is directly equivalent to the long primitive in Java.When calling a Java function that needs a long, you supply a scala.Long, when a java function returns a long, you'll see a scala.Long
In the compiled bytecode, there's absolutely no difference between long an scala.Long, it's all a trick of the compiler...
So in answer to your question, to convert a java.lang.Long to a scala.Long, just call longValue on it.

On Sat, Nov 21, 2009 at 9:44 PM, Konstantinos Karadamoglou <kkarad@googlemail.com> wrote:
Thanks Ricky,

What about java.lang.Long to scala.Long ?

I tried a simple assignment but it fails.
scala> val x: Long = java.lang.Long.valueOf(1)
<console>:4: error: type mismatch;
 found   : java.lang.Long
 required: Long
       val x: Long = java.lang.Long.valueOf(1)

I am assuming these conversions are documented somewhere. Does anyone know where to find the relevant documentation?

Kostas

On Sat, Nov 21, 2009 at 8:36 PM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
java.lang.Long.valueOf(scala.Long)

someJavaLong.longValue

2009/11/21 Konstantinos Karadamoglou <kkarad@googlemail.com>:
> Another question is how can I convert scala.Long to java.lang.Long and
> java.lang.Long to scala.Long?
>
> Thank you in advance.
>
> On Sat, Nov 21, 2009 at 12:54 AM, Konstantinos Karadamoglou
> <kkarad@googlemail.com> wrote:
>>
>> Thank you
>>
>> On Sat, Nov 21, 2009 at 12:30 AM, Kevin Wright
>> <kev.lee.wright@googlemail.com> wrote:
>>>
>>> I think this version is a tiny bit more efficient, don't take my word
>>> for it though.  You may also prefer the look of the thing:
>>>
>>> val lng = java.lang.Long.valueOf(-1L)
>>>
>>> I've not thrown this one at my compiler either...  :(
>>>
>>>
>>> On Saturday, November 21, 2009, David Pollak
>>> <feeder.of.the.bears@gmail.com> wrote:
>>> >
>>> > scala> var lng: java.lang.Long = -1L
>>> > lng: java.lang.Long = -1
>>> >
>>> > scala>
>>> >
>>> >
>>> >
>>> >
>>> > On Fri, Nov 20, 2009 at 4:05 PM, Konstantinos Karadamoglou
>>> > <kkarad@googlemail.com <javascript:_e({}, 'cvml',
>>> > 'kkarad@googlemail.com');>> wrote:
>>> > Hi,
>>> >
>>> > This might be silly ... Do you how to set a java.lang.Long variable to
>>> > a var.
>>> >
>>> > I need to use java.lang.Long  because of datanucleus-enhance bytecode
>>> > instrumentation fails otherwise.
>>> >
>>> > My code looks like:
>>> >
>>> > package org.kkarad.liftplayground.model
>>> >
>>> > import javax.jdo.annotations._
>>> >
>>> > @PersistenceCapable{val identityType = IdentityType.APPLICATION}
>>> > class Candidate {
>>> >
>>> >   @PrimaryKey
>>> >   @Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
>>> >   var id: java.lang.Long = -1
>>> >
>>> >   @Persistent
>>> >   var content: String = ""
>>> > }
>>> >
>>> > The compiler error is:
>>> > error: type mismatch;
>>> >  found   : Int(-1)
>>> >  required: java.lang.Long
>>> >   var id: java.lang.Long = -1
>>> >                             ^
>>> >
>>> > Thank you in advance,
>>> >
>>> > Kostas
>>> >
>>> >
>>> >
>>> > --
>>> > Lift, the simply functional web framework http://liftweb.net
>>> > Beginning Scala http://www.apress.com/book/view/1430219890
>>> > Follow me: http://twitter.com/dpp
>>> > Surf the harmonics
>>> >
>>
>
>



--
Ricky Clarkson
Java and Scala Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@gmail.com
Google Wave: ricky.clarkson@googlewave.com



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