- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
instanceof for lazy people
Sun, 2010-06-13, 14:17
hi,
because of extreme lazyness and a feeling of "no" when looking at lines
that look like:
if (a.isInstanceOf[X]) a.asInstanceOf[X].foo() else bar()
i invented:
def ifInstanceOf[T, X](func: (T) => X, orElse: X)(implicit m:
Manifest[T]) = {
if (m.erasure == a.getClass) {
func(a.asInstanceOf[T])
} else {
orElse
}
}
which can be used like this:
a.ifInstanceOf((x:X) => x.foo(), bar())
which is more pleasing to the programmer's eyes and brain.
what do you think?
Sun, 2010-06-13, 14:47
#2
Re: instanceof for lazy people
You may want to use:
M.erasure.isAssignableFrom(o.getClass)
Rather than:
m.erasure == o.getClass
One enforces a particular class, the other allows subclasses (like
isInstanceOf does)
- Josh
On Jun 13, 2010, at 9:17 AM, HamsterofDeath wrote:
> hi,
>
> because of extreme lazyness and a feeling of "no" when looking at
> lines
> that look like:
> if (a.isInstanceOf[X]) a.asInstanceOf[X].foo() else bar()
>
> i invented:
>
> def ifInstanceOf[T, X](func: (T) => X, orElse: X)(implicit m:
> Manifest[T]) = {
> if (m.erasure == a.getClass) {
> func(a.asInstanceOf[T])
> } else {
> orElse
> }
> }
>
> which can be used like this:
>
> a.ifInstanceOf((x:X) => x.foo(), bar())
>
> which is more pleasing to the programmer's eyes and brain.
> what do you think?
>
>
Sun, 2010-06-13, 14:57
#3
Re: instanceof for lazy people
It's more idiomatic in Scala to use a pattern match rather then the
isInstanceOf / asInstanceOf combo. There is a little bit more syntax
that your method, but it does give you a static check if isInstanceOf
would always return false.
scala> 1 match {
| case i: Int => i * 2
| case _ => 0
| }
res0: Int = 2.
scala> "1" match {
| case i: Int => i * 2
| case _ => 0
| }
:7: error: scrutinee is incompatible with pattern type;
found : Int
required: java.lang.String
case i: Int => i * 2
^
-jason
On Sun, Jun 13, 2010 at 3:17 PM, HamsterofDeath wrote:
> hi,
>
> because of extreme lazyness and a feeling of "no" when looking at lines
> that look like:
> if (a.isInstanceOf[X]) a.asInstanceOf[X].foo() else bar()
>
> i invented:
>
> def ifInstanceOf[T, X](func: (T) => X, orElse: X)(implicit m:
> Manifest[T]) = {
> if (m.erasure == a.getClass) {
> func(a.asInstanceOf[T])
> } else {
> orElse
> }
> }
>
> which can be used like this:
>
> a.ifInstanceOf((x:X) => x.foo(), bar())
>
> which is more pleasing to the programmer's eyes and brain.
> what do you think?
>
>
>
Sun, 2010-06-13, 15:07
#4
Re: instanceof for lazy people
i noticed that a few minutes after i sent my mail, but i couldn't turn
back the time like the prince of persia can :(
but while we're at it:
is there a reason to use
M.erasure.isAssignableFrom(o.getClass)
instead of
M.erasure.isInstance(o)?
using isInstance, null won't cause an exception
Josh Suereth schrieb:
> You may want to use:
>
> M.erasure.isAssignableFrom(o.getClass)
>
> Rather than:
>
> m.erasure == o.getClass
>
> One enforces a particular class, the other allows subclasses (like
> isInstanceOf does)
>
> - Josh
>
> On Jun 13, 2010, at 9:17 AM, HamsterofDeath wrote:
>
>> hi,
>>
>> because of extreme lazyness and a feeling of "no" when looking at lines
>> that look like:
>> if (a.isInstanceOf[X]) a.asInstanceOf[X].foo() else bar()
>>
>> i invented:
>>
>> def ifInstanceOf[T, X](func: (T) => X, orElse: X)(implicit m:
>> Manifest[T]) = {
>> if (m.erasure == a.getClass) {
>> func(a.asInstanceOf[T])
>> } else {
>> orElse
>> }
>> }
>>
>> which can be used like this:
>>
>> a.ifInstanceOf((x:X) => x.foo(), bar())
>>
>> which is more pleasing to the programmer's eyes and brain.
>> what do you think?
>>
>>
>
Sun, 2010-06-13, 18:17
#5
Re: instanceof for lazy people
i never thought of using a pattern matcher this way...
Jason Zaugg schrieb:
> It's more idiomatic in Scala to use a pattern match rather then the
> isInstanceOf / asInstanceOf combo. There is a little bit more syntax
> that your method, but it does give you a static check if isInstanceOf
> would always return false.
>
> scala> 1 match {
> | case i: Int => i * 2
> | case _ => 0
> | }
> res0: Int = 2.
>
> scala> "1" match {
> | case i: Int => i * 2
> | case _ => 0
> | }
> :7: error: scrutinee is incompatible with pattern type;
> found : Int
> required: java.lang.String
> case i: Int => i * 2
> ^
> -jason
>
> On Sun, Jun 13, 2010 at 3:17 PM, HamsterofDeath wrote:
>
>> hi,
>>
>> because of extreme lazyness and a feeling of "no" when looking at lines
>> that look like:
>> if (a.isInstanceOf[X]) a.asInstanceOf[X].foo() else bar()
>>
>> i invented:
>>
>> def ifInstanceOf[T, X](func: (T) => X, orElse: X)(implicit m:
>> Manifest[T]) = {
>> if (m.erasure == a.getClass) {
>> func(a.asInstanceOf[T])
>> } else {
>> orElse
>> }
>> }
>>
>> which can be used like this:
>>
>> a.ifInstanceOf((x:X) => x.foo(), bar())
>>
>> which is more pleasing to the programmer's eyes and brain.
>> what do you think?
>>
>>
>>
>>
>
>
Sun, 2010-06-13, 18:37
#6
Re: instanceof for lazy people
I've just added a related function to scalaz. Instead of throwing a
MatchError if the scrutinee doesn't match any of the patterns, it
returns the Zero element for the return type of the partial function.
scala> import scalaz._
import scalaz._
scala> import Scalaz._
import Scalaz._
scala> ("string": Any) matchOrZero { case s: String => s.toSeq }
res47: Seq[Char] = WrappedString(s, t, r, i, n, g)
scala> (0: Any) matchOrZero { case s: String => s.toSeq }
res48: Seq[Char] = List()
-jason
On Sun, Jun 13, 2010 at 7:15 PM, HamsterofDeath wrote:
> i never thought of using a pattern matcher this way...
>
> Jason Zaugg schrieb:
>> It's more idiomatic in Scala to use a pattern match rather then the
>> isInstanceOf / asInstanceOf combo. There is a little bit more syntax
>> that your method, but it does give you a static check if isInstanceOf
>> would always return false.
Sun, 2010-06-13, 18:37
#7
Re: instanceof for lazy people
On Sun, Jun 13, 2010 at 07:28:28PM +0200, Jason Zaugg wrote:
> I've just added a related function to scalaz. Instead of throwing a
> MatchError if the scrutinee doesn't match any of the patterns, it
> returns the Zero element for the return type of the partial function.
Sigh, Zero. That sounds nice.
Sun, 2010-06-13, 18:47
#8
Re: instanceof for lazy people
On Sunday June 13 2010, HamsterofDeath wrote:
> i never thought of using a pattern matcher this way...
I always thought the "typecase" pattern was the most common use of
Scala's pattern matching.
By the way, one other possibly significant difference between the match
and the HOF is that the HOF, of course, requires the compiler to
generate and instantiate the function supplied at the calling point.
Randall Schulz
Sun, 2010-06-13, 18:57
#9
Re: instanceof for lazy people
As in:
val numberOfKnownBugsInLatestReleaseCandidate = ...
:)
On 13 June 2010 18:32, Paul Phillips <paulp@improving.org> wrote:
--
Kevin Wright
mail/google talk: kev.lee.wright@gmail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda
val numberOfKnownBugsInLatestReleaseCandidate = ...
:)
On 13 June 2010 18:32, Paul Phillips <paulp@improving.org> wrote:
On Sun, Jun 13, 2010 at 07:28:28PM +0200, Jason Zaugg wrote:
> I've just added a related function to scalaz. Instead of throwing a
> MatchError if the scrutinee doesn't match any of the patterns, it
> returns the Zero element for the return type of the partial function.
Sigh, Zero. That sounds nice.
--
Paul Phillips | Christ died for our sins. Dare we make his martyrdom
Everyman | meaningless by not committing them?
Empiricist | -- Jules Feiffer
slap pi uphill! |----------* http://www.improving.org/paulp/ *----------
--
Kevin Wright
mail/google talk: kev.lee.wright@gmail.com
wave: kev.lee.wright@googlewave.com
skype: kev.lee.wright
twitter: @thecoda
whoops, it has to be:
m.erasure.isInstance(a), not m.erasure == a.getClass
HamsterofDeath schrieb:
> hi,
>
> because of extreme lazyness and a feeling of "no" when looking at lines
> that look like:
> if (a.isInstanceOf[X]) a.asInstanceOf[X].foo() else bar()
>
> i invented:
>
> def ifInstanceOf[T, X](func: (T) => X, orElse: X)(implicit m:
> Manifest[T]) = {
> if (m.erasure == a.getClass) {
> func(a.asInstanceOf[T])
> } else {
> orElse
> }
> }
>
> which can be used like this:
>
> a.ifInstanceOf((x:X) => x.foo(), bar())
>
> which is more pleasing to the programmer's eyes and brain.
> what do you think?
>
>
>