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 8185265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:47:17+00:00 2026-06-07T01:47:17+00:00

When casting a String to the type Any , it will no more be

  • 0

When casting a String to the type Any, it will no more be considered automatically as String — why ? See the following example:

val str = "foo"
val strAsAny = "bar".asInstanceOf[Any]
def f1(x: String) = println(x.toString)
def f2(x: Any) = println(x.toString)

f1(str) // works, type exactly given
f2(str) // works, subtype of Any given

f1(strAsAny) // works not, but strAsAny.isInstanceOf[String] = true !
f2(strAsAny) // works, type exactly given

This confuses me a bit, because both values do still have the type String :

scala> str.isInstanceOf[String]
res4: Boolean = true

scala> strAsAny.isInstanceOf[String]
res5: Boolean = true

My guess is, that only type conversions “to get upwards” of the interhitance-chain will be done automatically. Is this correct ?

  • 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-07T01:47:19+00:00Added an answer on June 7, 2026 at 1:47 am

    You are conflating static types with dynamic types.

    A static type is what the compiler knows, and is something that exists at compile time.

    That is, when you do this:

    val strAsAny = "bar".asInstanceOf[Any]
    

    Then the compiler will know (or think) that strAsAny has type Any. It won’t know there’s a String stored in there.

    Mind you, avoid as much as possible asInstanceOf, since most valid uses of it can be replaced by type ascription ("bar" : Any) or pattern matching (case anyAsStr: String => "got a String"), both of which are safe. The operator asInstanceOf is not safe, because it tells the compiler to ignore what it knows, and believe what you are telling it, even when it knows it’s wrong.

    So, asInstanceOf is mostly a compile-time operator, though it also produces some code for the JVM to execute.

    Now, what the compiler thinks matter because it only let you call methods it knows can be called. So even though, at run time, strAsAny will contain a String, the compiler doesn’t know that. You told it to forget that "bar" is a String, and it did so. Therefore, you are not allowed to call String methods on it.

    On the other hand, a dynamic type is what is true at run time. In most cases, for a language like Scala, the compiler has no way of knowing what will be true or not at run time.

    And whether the compiler can know something or not, isInstanceOf is a run time operation. The compiler doesn’t have anything to say about what will happen.

    So, when you do this:

    strAsAny.isInstanceOf[String]
    

    The compiler doesn’t care — it just compiles the code. When you execute the code, then the JVM will verify what kind of value is stored inside strAsAny, and verify that it is, indeed, a String, and then return true.

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

Sidebar

Related Questions

Is there a way to avoid casting to a non-string type when reading data
I have a method below doing casting on a String according to the given
Say I have a simple object which supports implicit casting to System.String public sealed
I have the following casting problem when my data structure sSpecificData contains a field
Using the following code: Function GetSetting(Of T)(ByVal SettingName As String, ByRef DefaultVal As T)
I'm packing some classes into ptr_map with any typed value. class EventManager { ptr_map<string,
What would be the return type of the following method? ( T ) public
I need a variant type that holds instances of any user-defined class. So I
I need to use java-legacy code with the following method: public void doit(Map <String,
So, I have the following code: private readonly Dictionary<string, IGame> _gameLookup = new Dictionary<string,

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.