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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:39:01+00:00 2026-05-24T08:39:01+00:00

I have the following code: class Parameterizable{ var map: Map[String, String] = new scala.collection.immutable.HashMap()

  • 0

I have the following code:

class Parameterizable{
  var map: Map[String, String] = new scala.collection.immutable.HashMap() 
  def put(entry: Tuple2[String, String]) = {
    map = map + entry; this
  }
}

class Query() extends Parameterizable{
  override def toString = {
    map.isEmpty match{
      case true => ""
      case false => "?" + map.map{case (key, value) => key + "=" + value}.mkString("&")
    }
  }
}

trait PageParameter extends Parameterizable{
  def page(page: Int) = put(("page" -> page.toString))
  def pageSize(pageSize: Int) = put(("pagesize" -> pageSize.toString))
}

trait DateParameter extends Parameterizable{
  def fromDate(date: java.util.Date) = put(("fromdate" -> (date.getTime()/1000L).toString()))
  def toDate(date: java.util.Date) = put(("todate" -> (date.getTime()/1000L).toString()))
}
//and other similar traits

I would like to do something like:

class ExtendedQuery extends Query with PageParameter with DateParameter
val query = new ExtendedQuery
query.page(4).pageSize(5).fromDate(new java.util.Date)

or:

query.and().page(4).and().pageSize(5).and().fromDate(new java.util.Date)

Is it possible in Scala?

  • 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-24T08:39:02+00:00Added an answer on May 24, 2026 at 8:39 am

    You can declare the methods as returning this.type and then return this from them:

    trait PageParameter extends Parameterizable{
      def page(page: Int) : this.type = { put(("page" -> page.toString)); this }
      def pageSize(pageSize: Int): this.type = { put(("pagesize" -> pageSize.toString)); this }
    }
    

    At the use-site, you can then chain the calls as you wanted. See this example:

    scala> trait Wibble {
     | def foo : this.type = { println("foo"); this }
     | }
    defined trait Wibble
    
    scala> trait Wobble extends Wibble {
     | def bar: this.type = { println("bar"); this }
     | }
    defined trait Wobble
    
    scala> trait Wubble extends Wibble {
     | def baz: this.type = { println("baz"); this }
     | }
    defined trait Wubble
    

    Now I can test it

    scala> new Wibble with Wobble with Wubble
    res0: java.lang.Object with Wibble with Wobble with Wubble = $anon$1@937e20
    
    scala> res0.bar.baz.foo
    bar
    baz
    foo
    res1: res0.type = $anon$1@937e20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
I have the following example code: class A(object): def __init__(self, id): self.myid = id
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
I have the following code in my class : sub new { my $class
I have following code class User attr_accessor :name end u = User.new u.name =
in c++ i have following code class Foobar{ public: Foobar * operator()(){ return new
I have following code public class TEST { public static void main(String arg[]){ try
I have following code: public class LCS1 { public static String lcs(String a, String
I have the following code class Program { static void Main(string[] args) { List<A>
I have the following code: class SuperClass { public static String getName() { return

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.