- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
How to do Casting ?
Thu, 2009-04-02, 15:41
Hi,
in the code below I try to cast a value "MyConstr" to type "MyClass".
Running the code throws a ClassCastException. What am I doing wrong?
Thanks,
Steven
package ra
object Main {
class MyClass
case class MyConstr extends MyClass
def main(args: Array[String]) = {
val e = MyConstr
val f = e.asInstanceOf[MyClass]
println("hello")
}
}
Thu, 2009-04-02, 16:07
#2
Re: How to do Casting ?
Ok, found my mistake.
I think I confused "MyConstr" with "MyConstr ()".
Steven
Thu, 2009-04-02, 16:17
#3
Re: Re: How to do Casting ?
On Thu, 2009-04-02 at 08:04 -0700, Steven Obua wrote:
> Ok, found my mistake.
>
> I think I confused "MyConstr" with "MyConstr ()".
Note that case classes without a parameter lists have been deprecated.
As the compiler suggests, use case objects or case classes with () as
the parameter list.
Ismael
Thu, 2009-04-02, 16:27
#4
Re: How to do Casting ?
MyConstr refers to a singleton; the generated companion object MyClass.
Generally, if in doubt, add type annotations.
val e: MyConstr = MyConstr //will fail to compile, telling you the problem.
2009/4/2 Carsten Saager <csaager@gmail.com>
Generally, if in doubt, add type annotations.
val e: MyConstr = MyConstr //will fail to compile, telling you the problem.
2009/4/2 Carsten Saager <csaager@gmail.com>
try
val e = MyConstr()
- Carsten
On Thu, Apr 2, 2009 at 4:40 PM, Steven Obua <obua@me.com> wrote:
Hi,
in the code below I try to cast a value "MyConstr" to type "MyClass".
Running the code throws a ClassCastException. What am I doing wrong?
Thanks,
Steven
<code>
package ra
object Main {
class MyClass
case class MyConstr extends MyClass
def main(args: Array[String]) = {
val e = MyConstr
val f = e.asInstanceOf[MyClass]
println("hello")
}
}
</code>
--
View this message in context: http://www.nabble.com/How-to-do-Casting---tp22849464p22849464.html
Sent from the Scala mailing list archive at Nabble.com.
val e = MyConstr()
- Carsten
On Thu, Apr 2, 2009 at 4:40 PM, Steven Obua <obua@me.com> wrote: