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

how to know type of a variable in scala

16 replies
Bujji
Joined: 2010-04-14,
User offline. Last seen 42 years 45 weeks ago.
Hi I am new to scalahow to know datatype of a variable in scala ?

plz help me
ThanksBujji
Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: how to know type of a variable in scala
define "know"
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:
Hi I am new to scalahow to know datatype of a variable in scala ?

plz help me
ThanksBujji



--
Kevin Wright

mail/google talk: kev.lee.wright@googlemail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda

Amarjeet Singh
Joined: 2010-05-19,
User offline. Last seen 42 years 45 weeks ago.
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

Stefan Langer
Joined: 2009-10-23,
User offline. Last seen 42 years 45 weeks ago.
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

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
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:
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
Bujji
Joined: 2010-04-14,
User offline. Last seen 42 years 45 weeks ago.
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:
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

Bujji
Joined: 2010-04-14,
User offline. Last seen 42 years 45 weeks ago.
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:
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

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
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: 

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: how to know type of a variable in scala
On 5/19/2010 10:42 PM, Bujji wrote:
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
Bujji
Joined: 2010-04-14,
User offline. Last seen 42 years 45 weeks ago.
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:
 ~/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'>

Johannes Rudolph 2
Joined: 2010-02-12,
User offline. Last seen 42 years 45 weeks ago.
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?

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
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: 
>
>

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
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:
(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

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
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?

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
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

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
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
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
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:
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.

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