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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:10:45+00:00 2026-06-04T05:10:45+00:00

Say I have a Scala class with default arguments class MyClass(p1: Int = 0,

  • 0

Say I have a Scala class with default arguments

class MyClass(p1: Int = 0, p2: Int = 1)

p1 and p2 comes from optional query string parameters in a Play app, e.g.,

http://mysite.com/blah?p1=10&p2=20 

In the Play controller action, I am trying to figure out a way to dynamically construct an argument lists when calling MyClass depending on what’s passed in the url. For example, there may be no arguments passed at all in which case I would call new MyClass, if p1 is passed, then I would call new MyClass(p1) where p1 is extracted from the url query parameter, you get the idea.

How can I do this without hard coding the list of arguments as I might add additional optional parameters in the future. Is it possible to construct a Map-like object with named arguments to pass as the arguments list or something like that?

  • 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-04T05:10:46+00:00Added an answer on June 4, 2026 at 5:10 am

    You could implement a secondary constructor that takes a Map. If you don’t want to repeat the defaults, then you can just have them as constants.

    val p1Default = 0
    val p2Default = 1
    
    class MyClass(p1: Int = p1Default, p2: Int = p2Default) {
      def this(m: Map[String, Int]) =
        this(m.getOrElse("p1", p1Default), m.getOrElse("p2", p2Default))
    }
    

    Usage:

    new MyClass(p1 = 5, p2 = 6)
    new MyClass(Map("p1" -> 5))
    new MyClass(p2 = 6)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say have this immutable record type: public class Record { public Record(int x,
Let's say I have this Hello.scala. object HelloWorld { def main(args: Array[String]) { println(Hello,
Say you define the following: class Person(name: String, age: Int) { def toXml =
Let's say I have a class that looks something like this: class Foo(Prop1:Int, Prop2:Int,
Say I have the following models: public class Item { public int Id{ get;
I have a class, say class AddElement{ int a,b,c; } With methods to set/get
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say I have the following generic class: public class Foo<T extends Bar> { //
Let's say I have a Java class abstract class Base { abstract void init();
Let's say that I have this: class Dependency1 { def methodD1 { } }

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.