Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7848179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:06:35+00:00 2026-06-02T18:06:35+00:00

I don’t like Scala isInstanceOf and asInstanceOf methods – they are long and asInstanceOf

  • 0

I don’t like Scala isInstanceOf and asInstanceOf methods – they are long and asInstanceOf can throw exception so we need to use it in couple. Better way is to use pattern matching: Scala: How do I cast a variable? but for really simple operations it can be relatively long too. In C# we have ‘is’ and ‘as’ operators so I wanted to implement implicit definition with this in Scala. My code look like this:

scala> class TypeCast(x:Any){
     | def is[T](t:Class[T]) = t.isInstance(x) 
     | def as[T](t:Class[T]):Option[T] = if(t.isInstance(x)) Option(t.cast(x)) else None
     | }
defined class TypeCast

scala> implicit def TypeCastID(x:Any)=new TypeCast(x)
TypeCastID: (x: Any)TypeCast

scala>  123 as classOf[String]
res14: Option[String] = None

scala> "asd" as classOf[String]
res15: Option[String] = Some(asd)

It has one advantage – implement null-object pattern but also have disadvantages:

  • need to use classOf[T] operator – it’s too long

  • overhead connected with implicit def for such simple operation

so there is no practical reason to use it.
I would like to know is there any way to implement this without need to use classOf[T]?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:06:37+00:00Added an answer on June 2, 2026 at 6:06 pm

    Well you could shorten it down inside the def you made in the TypeCast class. So instead of feeding it a parameter you could just rely on the type. This would shorten it down a lot. As an example this could look something like:

    class TypeCast(x : Any) {
        def is[T : Manifest] = manifest.erasure.isInstance(x)
        def as[T : Manifest] : Option[T] = if (manifest.erasure.isInstance(x)) Some(x.asInstanceOf[T]) else None
    }
    

    Future calls could look like:

    scala> 123.as[String]
    res0: Option[String] = Some(123)
    scala> class A; class B extends A
    defined class A
    defined class B
    scala> new B
    res1: B
    scala> res1.is[Int]
    res2: Boolean = false
    scala> res1.as[Int]
    res3: Option[Int] = None
    

    Update: I added Manifests to avoid type-check errors

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't ask me why but i can't use this method because I need to
I don't like Jackson. I want to use ajax but with Google Gson. So
Don't ask me why but I need to do the following: string ClassName =
Don't know if anyone can help me with this or if it's even possible.
Don't be afraid to use any technical jargon or low-level explanations for things, please.
Don't they both have to convert to machine code at some point to execute
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't have much to say, just can get into the event handler. XAML: <Grid>
I don't know if this has been asked before, but what i'd like to
Don't know why I can't reply to people on here with only a small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.