- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
how to know type of a variable in scala
Wed, 2010-05-19, 09:20
Hi I am new to scalahow to know datatype of a variable in scala ?
plz help me
ThanksBujji
plz help me
ThanksBujji
Wed, 2010-05-19, 09:47
#2
Re: how to know type of a variable in scala
Do "reflect" on your question before you ask ;)
Regards
On Wed, May 19, 2010 at 4:20 AM, Bujji wrote:
> Hi
> I am new to scala
> how to know datatype of a variable in scala ?
>
> plz help me
> Thanks
> Bujji
Wed, 2010-05-19, 13:17
#3
Re: how to know type of a variable in scala
RTFC = Read the fine code
-Stefan
2010/5/19 Bujji :
> Hi
> I am new to scala
> how to know datatype of a variable in scala ?
>
> plz help me
> Thanks
> Bujji
Wed, 2010-05-19, 14:07
#4
Re: how to know type of a variable in scala
The type of the data or the type of the reference to that data?
On Wed, May 19, 2010 at 10:20 AM, Bujji <sivaits4u@gmail.com> wrote:
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
On Wed, May 19, 2010 at 10:20 AM, Bujji <sivaits4u@gmail.com> wrote:
Hi I am new to scalahow to know datatype of a variable in scala ?
plz help me
ThanksBujji
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Thu, 2010-05-20, 06:47
#5
Re: how to know type of a variable in scala
Dear all,Thanks for you answers.
what my intension is if i give some data in python to find the type of it.for ex:in python type("hello") -- will give: <type 'str'> type(123) -- will give: <type 'int'> type(12.3) -- will give: <type 'float'>
In scala how to find this ??????that is what my question and you people gave some different answers.....:)
please answer my question...
Thanks,Bujji
On Wed, May 19, 2010 at 6:28 PM, Viktor Klang <viktor.klang@gmail.com> wrote:
what my intension is if i give some data in python to find the type of it.for ex:in python type("hello") -- will give: <type 'str'> type(123) -- will give: <type 'int'> type(12.3) -- will give: <type 'float'>
In scala how to find this ??????that is what my question and you people gave some different answers.....:)
please answer my question...
Thanks,Bujji
On Wed, May 19, 2010 at 6:28 PM, Viktor Klang <viktor.klang@gmail.com> wrote:
The type of the data or the type of the reference to that data?
On Wed, May 19, 2010 at 10:20 AM, Bujji <sivaits4u@gmail.com> wrote:
Hi I am new to scalahow to know datatype of a variable in scala ?
plz help me
ThanksBujji
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Thu, 2010-05-20, 06:57
#6
Re: how to know type of a variable in scala
Dear all,Thanks for you answers.
what my intension is if i give some data in python to find the type of it.for ex:in python type("hello") -- will give: <type 'str'> type(123) -- will give: <type 'int'> type(12.3) -- will give: <type 'float'>
In scala how to find this ??????that is what my question and you people gave some different answers.....:)
please answer my question...
Thanks,Bujji
On Wed, May 19, 2010 at 6:28 PM, Viktor Klang <viktor.klang@gmail.com> wrote:
what my intension is if i give some data in python to find the type of it.for ex:in python type("hello") -- will give: <type 'str'> type(123) -- will give: <type 'int'> type(12.3) -- will give: <type 'float'>
In scala how to find this ??????that is what my question and you people gave some different answers.....:)
please answer my question...
Thanks,Bujji
On Wed, May 19, 2010 at 6:28 PM, Viktor Klang <viktor.klang@gmail.com> wrote:
The type of the data or the type of the reference to that data?
On Wed, May 19, 2010 at 10:20 AM, Bujji <sivaits4u@gmail.com> wrote:
Hi I am new to scalahow to know datatype of a variable in scala ?
plz help me
ThanksBujji
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org
Twttr: twitter.com/viktorklang
Thu, 2010-05-20, 07:07
#7
Re: how to know type of a variable in scala
Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM,
Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.
scala> "123"
res0: java.lang.String = 123
scala> 123
res1: Int = 123
scala> 12.3
res2: Double = 12.3
scala> "123".getClass.getSimpleName
res3: java.lang.String = String
scala> 123.asInstanceOf[AnyRef].getClass.getSimpleName
res5: java.lang.String = Integer
scala> 12.3.asInstanceOf[AnyRef].getClass.getSimpleName
res6: java.lang.String = Double
On Thu, May 20, 2010 at 7:42 AM, Bujji wrote:
> Dear all,
> Thanks for you answers.
> what my intension is if i give some data in python to find the type of it.
> for ex:
> in python
> type("hello") -- will give:
> type(123) -- will give:
> type(12.3) -- will give:
Thu, 2010-05-20, 07:17
#8
Re: how to know type of a variable in scala
On 5/19/2010 10:42 PM, Bujji wrote:
If your value is an AnyRef (aka a reference type, aka an instance of a class that extends java.lang.Object) you can call .getClass to get the java.lang.Class instance for that value.
If it's not, you can either:
- force the compiler to box the primitive values by casting them to AnyRef, as Jason suggested, or;
- since primitives don't have classes as such, buckle down and enumerate the types. Luckily pattern matching makes this relatively painless, there are only a handful of AnyVal types, and AnyVal is final, so it's not so onerous.
This technique was discussed quite recently:
http://scala-programming-language.1934581.n4.nabble.com/Reflection-on-an-argument-of-type-Any-tp2132983p2133142.html
-0xe1a
AANLkTileLCR2BFJzAGlFJkJntW7LmKOpIOQgUb1vJvXY [at] mail [dot] gmail [dot] com" type="cite"> for ex: in python type("hello") -- will give: <type 'str'> type(123) -- will give: <type 'int'> type(12.3) -- will give: <type 'float'>
If your value is an AnyRef (aka a reference type, aka an instance of a class that extends java.lang.Object) you can call .getClass to get the java.lang.Class instance for that value.
If it's not, you can either:
- force the compiler to box the primitive values by casting them to AnyRef, as Jason suggested, or;
- since primitives don't have classes as such, buckle down and enumerate the types. Luckily pattern matching makes this relatively painless, there are only a handful of AnyVal types, and AnyVal is final, so it's not so onerous.
This technique was discussed quite recently:
http://scala-programming-language.1934581.n4.nabble.com/Reflection-on-an-argument-of-type-Any-tp2132983p2133142.html
-0xe1a
Thu, 2010-05-20, 07:27
#9
Re: how to know type of a variable in scala
hi Viktor,Thanks for your reply.from your examples all will give java datatype of it. but not of scala.please create a variable using scala syntax and print the datatype of it.
like val a:Int=10and give me the type of it.not on command line.
ThanksBujji
On Thu, May 20, 2010 at 11:24 AM, Jason Zaugg <jzaugg@gmail.com> wrote:
like val a:Int=10and give me the type of it.not on command line.
ThanksBujji
On Thu, May 20, 2010 at 11:24 AM, Jason Zaugg <jzaugg@gmail.com> wrote:
~/code/a[master*]: scala
Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM,
Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.
scala> "123"
res0: java.lang.String = 123
scala> 123
res1: Int = 123
scala> 12.3
res2: Double = 12.3
scala> "123".getClass.getSimpleName
res3: java.lang.String = String
scala> 123.asInstanceOf[AnyRef].getClass.getSimpleName
res5: java.lang.String = Integer
scala> 12.3.asInstanceOf[AnyRef].getClass.getSimpleName
res6: java.lang.String = Double
On Thu, May 20, 2010 at 7:42 AM, Bujji <sivaits4u@gmail.com> wrote:
> Dear all,
> Thanks for you answers.
> what my intension is if i give some data in python to find the type of it.
> for ex:
> in python
> type("hello") -- will give: <type 'str'>
> type(123) -- will give: <type 'int'>
> type(12.3) -- will give: <type 'float'>
Thu, 2010-05-20, 08:07
#10
Re: how to know type of a variable in scala
On Thu, May 20, 2010 at 7:54 AM, Jason Zaugg wrote:
> ~/code/a[master*]: scala
Code A Master Star? Is there a rating agency for programmers?
Thu, 2010-05-20, 09:07
#11
Re: how to know type of a variable in scala
(Tested in Scala 2.8)
scala> def manOf[T: Manifest](t: T): Manifest[T] = manifest[T]
manOf: [T](t: T)(implicit evidence$1: Manifest[T])Manifest[T]
scala> manOf(1)
res0: Manifest[Int] = Int
scala> manOf("")
res1: Manifest[java.lang.String] = java.lang.String
scala> val m = manOf(List(1))
m: Manifest[List[Int]] = scala.collection.immutable.List[Int]
scala> m.erasure
res7: java.lang.Class[_] = class scala.collection.immutable.List
scala> m.typeArguments
res9: List[scala.reflect.Manifest[_]] = List(Int)
scala> val m2 = manOf(List(1, "string"))
m2: Manifest[List[Any]] = scala.collection.immutable.List[Any]
scala> m <:< m2
res10: Boolean = true
On Thu, May 20, 2010 at 8:07 AM, Bujji wrote:
> hi Viktor,
> Thanks for your reply.
> from your examples all will give java datatype of it. but not of scala.
> please create a variable using scala syntax and print the datatype of it.
> like
> val a:Int=10
> and give me the type of it.
> not on command line.
> Thanks
> Bujji
>
> On Thu, May 20, 2010 at 11:24 AM, Jason Zaugg wrote:
>>
>> ~/code/a[master*]: scala
>> Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM,
>> Java 1.6.0_17).
>> Type in expressions to have them evaluated.
>> Type :help for more information.
>>
>> scala> "123"
>> res0: java.lang.String = 123
>>
>> scala> 123
>> res1: Int = 123
>>
>> scala> 12.3
>> res2: Double = 12.3
>>
>> scala> "123".getClass.getSimpleName
>> res3: java.lang.String = String
>>
>> scala> 123.asInstanceOf[AnyRef].getClass.getSimpleName
>> res5: java.lang.String = Integer
>>
>> scala> 12.3.asInstanceOf[AnyRef].getClass.getSimpleName
>> res6: java.lang.String = Double
>>
>>
>> On Thu, May 20, 2010 at 7:42 AM, Bujji wrote:
>> > Dear all,
>> > Thanks for you answers.
>> > what my intension is if i give some data in python to find the type of
>> > it.
>> > for ex:
>> > in python
>> > type("hello") -- will give:
>> > type(123) -- will give:
>> > type(12.3) -- will give:
>
>
Thu, 2010-05-20, 09:37
#12
Re: how to know type of a variable in scala
and we wonder why people thing Scala is complicated!
What's wrong with getClass?
On 20 May 2010 08:59, Jason Zaugg <jzaugg@gmail.com> wrote:
--
Kevin Wright
mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda
What's wrong with getClass?
On 20 May 2010 08:59, Jason Zaugg <jzaugg@gmail.com> wrote:
(Tested in Scala 2.8)
scala> def manOf[T: Manifest](t: T): Manifest[T] = manifest[T]
manOf: [T](t: T)(implicit evidence$1: Manifest[T])Manifest[T]
scala> manOf(1)
res0: Manifest[Int] = Int
scala> manOf("")
res1: Manifest[java.lang.String] = java.lang.String
scala> val m = manOf(List(1))
m: Manifest[List[Int]] = scala.collection.immutable.List[Int]
scala> m.erasure
res7: java.lang.Class[_] = class scala.collection.immutable.List
scala> m.typeArguments
res9: List[scala.reflect.Manifest[_]] = List(Int)
scala> val m2 = manOf(List(1, "string"))
m2: Manifest[List[Any]] = scala.collection.immutable.List[Any]
scala> m <:< m2
res10: Boolean = true
On Thu, May 20, 2010 at 8:07 AM, Bujji <sivaits4u@gmail.com> wrote:
> hi Viktor,
> Thanks for your reply.
> from your examples all will give java datatype of it. but not of scala.
> please create a variable using scala syntax and print the datatype of it.
> like
> val a:Int=10
> and give me the type of it.
> not on command line.
> Thanks
> Bujji
>
> On Thu, May 20, 2010 at 11:24 AM, Jason Zaugg <jzaugg@gmail.com> wrote:
>>
>> ~/code/a[master*]: scala
>> Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM,
>> Java 1.6.0_17).
>> Type in expressions to have them evaluated.
>> Type :help for more information.
>>
>> scala> "123"
>> res0: java.lang.String = 123
>>
>> scala> 123
>> res1: Int = 123
>>
>> scala> 12.3
>> res2: Double = 12.3
>>
>> scala> "123".getClass.getSimpleName
>> res3: java.lang.String = String
>>
>> scala> 123.asInstanceOf[AnyRef].getClass.getSimpleName
>> res5: java.lang.String = Integer
>>
>> scala> 12.3.asInstanceOf[AnyRef].getClass.getSimpleName
>> res6: java.lang.String = Double
>>
>>
>> On Thu, May 20, 2010 at 7:42 AM, Bujji <sivaits4u@gmail.com> wrote:
>> > Dear all,
>> > Thanks for you answers.
>> > what my intension is if i give some data in python to find the type of
>> > it.
>> > for ex:
>> > in python
>> > type("hello") -- will give: <type 'str'>
>> > type(123) -- will give: <type 'int'>
>> > type(12.3) -- will give: <type 'float'>
>
>
--
Kevin Wright
mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda
Thu, 2010-05-20, 09:47
#13
Re: how to know type of a variable in scala
getClass if fine if you want the erased types. Manifests let you see
the type arguments, too.
-jason
On Thu, May 20, 2010 at 10:30 AM, Kevin Wright
wrote:
> and we wonder why people thing Scala is complicated!
> What's wrong with getClass?
Thu, 2010-05-20, 15:07
#14
Re: how to know type of a variable in scala
On Thursday May 20 2010, Kevin Wright wrote:
> and we wonder why people thing Scala is complicated!
>
> What's wrong with getClass?
[ What's wrong with trimming quoted material? ]
Scala types don't fit in Java classes, if you will. There's far more
information in a Scala type than in the class that represents it, if
any.
One pithy way of thinking of this is: A class is the run-time residue of
a Scala type.
Randall Schulz
Thu, 2010-05-20, 15:27
#15
Re: how to know type of a variable in scala
One pithy way of thinking of this is: A class is the run-time residue of
a Scala type.
"run-time residue"
I dread to think what would happen if b3ta or the oatmeal got their hands on that phrase :P
Thu, 2010-05-20, 15:57
#16
Re: how to know type of a variable in scala
Nothing wrong, but Bujji asked for something non-Javaish. Aside from the fact that manifest returns a "more correct" Scala type, if you need to get the type of something you are likely to be doing it the wrong way in first place. Not necessarily, by all means, but...
On Thu, May 20, 2010 at 5:30 AM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:
--
Daniel C. Sobral
I travel to the future all the time.
On Thu, May 20, 2010 at 5:30 AM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:
and we wonder why people thing Scala is complicated!
What's wrong with getClass?
On 20 May 2010 08:59, Jason Zaugg <jzaugg@gmail.com> wrote:
(Tested in Scala 2.8)
scala> def manOf[T: Manifest](t: T): Manifest[T] = manifest[T]
manOf: [T](t: T)(implicit evidence$1: Manifest[T])Manifest[T]
scala> manOf(1)
res0: Manifest[Int] = Int
scala> manOf("")
res1: Manifest[java.lang.String] = java.lang.String
scala> val m = manOf(List(1))
m: Manifest[List[Int]] = scala.collection.immutable.List[Int]
scala> m.erasure
res7: java.lang.Class[_] = class scala.collection.immutable.List
scala> m.typeArguments
res9: List[scala.reflect.Manifest[_]] = List(Int)
scala> val m2 = manOf(List(1, "string"))
m2: Manifest[List[Any]] = scala.collection.immutable.List[Any]
scala> m <:< m2
res10: Boolean = true
On Thu, May 20, 2010 at 8:07 AM, Bujji <sivaits4u@gmail.com> wrote:
> hi Viktor,
> Thanks for your reply.
> from your examples all will give java datatype of it. but not of scala.
> please create a variable using scala syntax and print the datatype of it.
> like
> val a:Int=10
> and give me the type of it.
> not on command line.
> Thanks
> Bujji
>
> On Thu, May 20, 2010 at 11:24 AM, Jason Zaugg <jzaugg@gmail.com> wrote:
>>
>> ~/code/a[master*]: scala
>> Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM,
>> Java 1.6.0_17).
>> Type in expressions to have them evaluated.
>> Type :help for more information.
>>
>> scala> "123"
>> res0: java.lang.String = 123
>>
>> scala> 123
>> res1: Int = 123
>>
>> scala> 12.3
>> res2: Double = 12.3
>>
>> scala> "123".getClass.getSimpleName
>> res3: java.lang.String = String
>>
>> scala> 123.asInstanceOf[AnyRef].getClass.getSimpleName
>> res5: java.lang.String = Integer
>>
>> scala> 12.3.asInstanceOf[AnyRef].getClass.getSimpleName
>> res6: java.lang.String = Double
>>
>>
>> On Thu, May 20, 2010 at 7:42 AM, Bujji <sivaits4u@gmail.com> wrote:
>> > Dear all,
>> > Thanks for you answers.
>> > what my intension is if i give some data in python to find the type of
>> > it.
>> > for ex:
>> > in python
>> > type("hello") -- will give: <type 'str'>
>> > type(123) -- will give: <type 'int'>
>> > type(12.3) -- will give: <type 'float'>
>
>
--
Kevin Wright
mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda
--
Daniel C. Sobral
I travel to the future all the time.
You just want to see this in your IDE?You need to pass a class to some function?You're interacting with an ORM?
Please ask a more specific question!
On 19 May 2010 09:20, Bujji <sivaits4u@gmail.com> wrote:
--
Kevin Wright
mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda