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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:39:25+00:00 2026-05-31T14:39:25+00:00

I have a method like this: def aMethod(param: String = asdf) = { …

  • 0

I have a method like this:

def aMethod(param: String = "asdf") = {
    ...
}

If the method is called as follows, then param is given the default value “asdf”:

aMethod() ...

But what I would like, is that if the method is called with null, then the default value would also be applied:

aMethod(null)  //inside the method, I can use `param` and it has the value "asdf".

Whats the best way to do this in Scala? I can think of pattern matching or a simple if statement.

  • 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-31T14:39:27+00:00Added an answer on May 31, 2026 at 2:39 pm

    Pattern matching

    def aMethod(param: String = null) {
        val paramOrDefault = param match {
            case null => "asdf"
            case s => s
        }
    }
    

    Option (implicitly)

    def aMethod(param: String = null) {
        val paramOrDefault = Option(param).getOrElse("asdf")
    }
    

    Option (explicitly)

    def aMethod(param: Option[String] = None) {
        val paramOrDefault = param getOrElse "asdf"
    }
    

    The last approach is actually the most idiomatic and readable once you get use to it.

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

Sidebar

Related Questions

Say, I have an XML String like this, <METHOD>foo</METHOD> <PARAM1>abc</PARAM1> <PARAM2>def</PARAM2> ... <PARAM99>ghi</PARAM99> <PARAM100>jkl</PARAM100>
Let's say that I have a method that looks like this: def raise_if_client_status_error(xml_resp) #
I have a controller method that is a little something like this: def suggestions
If I have a method like this: public void DoSomething(int Count, string[] Lines) {
I have wriiten a method like this public ArrayList<T> GetDoctorDetail(String name) { if (name!=null
I have a method that looks like this: public static String escape(String text) {
Let's say I have a method definition like this: def myMethod(a, b, c, d,
I have a method in a rails helper file like this def table_for(collection, *args)
i have a method in a model that looks like this: def self.super_data self.find(:all,
I'd like to have a method that is similar to this: def method_with_optional(..., user

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.