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

instanceof for lazy people

9 replies
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.

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?

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: instanceof for lazy people

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?
>
>
>

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
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?
>
>

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

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
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?
>>
>>
>

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
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?
>>
>>
>>
>>
>
>

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

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
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.

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

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: instanceof for lazy people
As in:
val numberOfKnownBugsInLatestReleaseCandidate = ...
:)


On 13 June 2010 18:32, Paul Phillips <paulp [at] improving [dot] 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 [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

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