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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:00:37+00:00 2026-05-24T05:00:37+00:00

In a Scala class, I can conveniently declare the return type of a method

  • 0

In a Scala class, I can conveniently declare the return type of a method to be this.type to guarantee that it will return the same object it is called on:

class Foo {
  def bar: this.type = this
}

Is there a way I can similarly specify that a method that accepts a given AnyRef param will return that reference exactly? The following snippet does not provide this guarantee, I could return any instance of A:

def processAndReturn[A <: AnyRef](obj: A): A = {
  // work with obj
  obj
}
  • 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-24T05:00:37+00:00Added an answer on May 24, 2026 at 5:00 am

    huitseeker’s suggestion is actually valid with the -Ydependent-method-types option:

    $ scala -Ydependent-method-types
    Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
    
    scala>   def processAndReturn[A <: AnyRef](obj: A): obj.type = {
         |       // work with obj
         |       obj
         |   }
    processAndReturn: [A <: AnyRef](obj: A)obj.type
    
    scala> processAndReturn("hi")
    res1: java.lang.String = hi
    

    From this thread, I gather that experimental dependent method types have been around for a while, and there’s even a published paper using them, but the implementation may be buggy. There has been some discussion about reworking Scala’s type system foundations (see Adriaan Moor’s slides on Dependent Object Types). If it works out, I imagine that dependent method types (among other things) will be completely kosher in future versions of Scala.

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

Sidebar

Related Questions

I need a builder library that can be called from Scala and Java. Easy
I noticed that I can extend a class and at the same time pass
While working with a Java class in Scala, I noticed that Scala can't multiply
Following on from this question , can someone explain the following in Scala: class
As far as I can tell, Scala has definitions for the Enumeration Value class
I have a class that I would like to use in a scala.collection.mutable.PriorityQueue, but
I've just seen this case class in the Scala actors package: case class !
Let's say I have this Hello.scala. object HelloWorld { def main(args: Array[String]) { println(Hello,
I'm having trouble extending a base class that extends Ordered[Base]. My derived class can't
I'm using Java's java.util.Date class in Scala and want to compare a Date object

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.