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

The Archive Base Latest Questions

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

In the REPL, I define a function. Note the return type. scala> def next(i:

  • 0

In the REPL, I define a function. Note the return type.

scala> def next(i: List[String]) =  i.map {"0" + _} ::: i.reverse.map {"1" + _}
next: (i: List[String])List[java.lang.String]

And if I specify the return type as String

scala> def next(i: List[String]): List[String] = i.map {"0" + _} ::: i.reverse.map {"1" + _}
next: (i: List[String])List[String]

Why the difference? I can also specify the return type as List[Any], so I guess String is just a wrapper supertype to java.lang.String. Will this have any practical implications or can I safely not specify the return type?

  • 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-23T12:42:04+00:00Added an answer on May 23, 2026 at 12:42 pm

    This is a very good question! First, let me assure you that you can safely specify the return type.

    Now, let’s look into it… yes, when left to inference, Scala infers java.lang.String, instead of just String. So, if you look up “String” in the ScalaDoc, you won’t find anything, which seems to indicate it is not a Scala class either. Well, it has to come from someplace, though.

    Let’s consider what Scala imports by default. You can find it by yourself on the REPL:

    scala> :imports
     1) import java.lang._             (155 types, 160 terms)
     2) import scala._                 (801 types, 809 terms)
     3) import scala.Predef._          (16 types, 167 terms, 96 are implicit)
    

    The first two are packages — and, indeed, String can be found on java.lang! Is that it, then? Let’s check by instantiating something else from that package:

    scala> val s: StringBuffer = new StringBuffer
    s: java.lang.StringBuffer =
    
    scala> val s: String = new String
    s: String = ""
    

    So, that doesn’t seem to be it. Now, it can’t be inside the scala package, or it would have been found when looking up on the ScalaDoc. So let’s look inside scala.Predef, and there it is!

    type String = String
    

    That means String is an alias for java.lang.String (which was imported previously). That looks like a cyclic reference though, but if you check the source, you’ll see it is defined with the full path:

    type String        = java.lang.String
    

    Next, you might want to ask why? Well, I don’t have any idea, but I suspect it is to make such an important class a little less dependent on the JVM.

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

Sidebar

Related Questions

On REPL, if I define (def fits (map vector (take 10 (iterate inc 0))))
Using Scala's command line REPL: def foo(x: Int): Unit = {} def foo(x: String):
When I execute following code in scala REPL console: java.util.Collections.max(new java.util.ArrayList[String]()) NoSuchMethodError exception is
So in the Scala REPL, I can use the ctrl-{p,n,a,e} to do previous-, next-,
Is there something like python's interactive REPL mode, but for Java? So that I
I almost always have a Scala REPL session or two open, which makes it
I'm writing a toy interpreter with a REPL in Java. I'd like to generate
I need some very basic advice on how to define a working -main function
simple REPL test... def g(a:Int)(implicit b:Int) = {a+b} Why do neither of these attempted
In Scala REPL: val input = <outerTag xmlns=http://xyz> <innerTag> </innerTag> </outerTag> input\\@innerTag => <innerTag

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.