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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:19:04+00:00 2026-05-13T12:19:04+00:00

There’s not much info in the spec on what type ascription is, and there

  • 0

There’s not much info in the spec on what type ascription is, and there certainly isn’t anything in there about the purpose for it. Other than “making passing varargs work”, what would I use type ascription for? Below is some scala REPL for the syntax and effects of using it.

scala> val s = "Dave"
s: java.lang.String = Dave

scala> val p = s:Object
p: java.lang.Object = Dave

scala> p.length
<console>:7: error: value length is not a member of java.lang.Object
       p.length
         ^
scala> p.getClass
res10: java.lang.Class[_ <: java.lang.Object] = class java.lang.String

scala> s.getClass
res11: java.lang.Class[_ <: java.lang.Object] = class java.lang.String

scala> p.asInstanceOf[String].length
res9: Int = 4
  • 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-05-13T12:19:04+00:00Added an answer on May 13, 2026 at 12:19 pm

    Type ascription is just telling the compiler what type you expect out of an expression, from all possible valid types.

    A type is valid if it respects existing constraints, such as variance and type declarations, and it is either one of the types the expression it applies to “is a“, or there’s a conversion that applies in scope.

    So, java.lang.String extends java.lang.Object, therefore any String is also an Object. In your example you declared you want the expression s to be treated as an Object, not a String. Since there is no constraints preventing that and the desired type is one of the types s is a, it works.

    Now, why would you want that? Consider this:

    scala> val s = "Dave"
    s: java.lang.String = Dave
    
    scala> val p = s: Object
    p: java.lang.Object = Dave
    
    scala> val ss = scala.collection.mutable.Set(s)
    ss: scala.collection.mutable.Set[java.lang.String] = Set(Dave)
    
    scala> val ps = scala.collection.mutable.Set(p)
    ps: scala.collection.mutable.Set[java.lang.Object] = Set(Dave)
    
    scala> ss += Nil
    <console>:7: error: type mismatch;
     found   : scala.collection.immutable.Nil.type (with underlying type object Nil)
     required: java.lang.String
           ss += Nil
                 ^
    
    scala> ps += Nil
    res3: ps.type = Set(List(), Dave)
    

    You could also have fixed this by type ascripting s at ss declaration, or you could have declared ss‘s type to be Set[AnyRef].

    However, type declarations achieve the same thing only as long as you are assigning a value to an identifier. Which one can always do, of course, if one doesn’t care about littering the code with one-shot identifiers. For example, the following does not compile:

    def prefixesOf(s: String) = s.foldLeft(Nil) { 
      case (head :: tail, char) => (head + char) :: head :: tail
      case (lst, char) => char.toString :: lst
    }
    

    But this does:

    def prefixesOf(s: String) = s.foldLeft(Nil: List[String]) { 
      case (head :: tail, char) => (head + char) :: head :: tail
      case (lst, char) => char.toString :: lst
    }
    

    It would be silly to use an identifier here in place of Nil. And though I could just write List[String]() instead, that isn’t always an option. Consider this, for instance:

    def firstVowel(s: String) = s.foldLeft(None: Option[Char]) { 
      case (None, char) => if ("aeiou" contains char.toLower) Some(char) else None
      case (vowel, _) => vowel
    }
    

    For the reference, this is what Scala 2.7 spec (march 15, 2009 draft) has to say about type ascription:

    Expr1 ::= ...
            | PostfixExpr Ascription
    
    Ascription ::= ‘:’ InfixType
                 | ‘:’ Annotation {Annotation}
                 | ‘:’ ‘_’ ‘*’
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are nice SO question and answers about this issue, but these options didn't
There was a similar question posted asking in general about alternatives to Qt. Well,
There is any way to set the generic type (T) of class in the
There are are 150 000 records in my comments table and about 1000 new
There are many 3D UI engines in SourceForge and other opensource forum, I would
I know there's a lot of other questions out there that deal with this
There is about 2000 lines of this, so manually would probably take more work
There is a <input type=checkbox name=option2 id=checkbox_3 value=3 /> I tried this code <script
There is a code in my Sinatra app - if flash[:error] .alert.alert-error %button.close{:type =>
There is a moment in my app, that I need to force to show

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.