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

  • Home
  • SEARCH
  • 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 6547261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:48:30+00:00 2026-05-25T11:48:30+00:00

Apologies for the confusing title, didn’t know how to express it better. This is

  • 0

Apologies for the confusing title, didn’t know how to express it better. This is a follow-up from Overloading in Scala when type parameters are being used

Here’s the faulty code:

def parser[T](identifier: String, default: T, modifier: String => T): T = {
        val l = args.filter(_.toLowerCase.startsWith(identifier.toLowerCase))
        println("l:")
        println(l.isEmpty)
        if(l.isEmpty) default
        else modifier(l(0).drop(identifier.length).trim)
    }

val installation: String = parser("-installation", {throw new Error("Installation not specified")}, identity)

This code is in the main function, so args is the usual list of arguments. If I do pass in a -installation asdf argument what happens is that the error gets thrown before the program can even step into the parser method (This happens becase no “l: false” is printed in the console, so that code is never reached.) I believe the exception is thrown when the

{throw new Error("Installation not specified")}

block is evaluated. I guess Scala evaluates that block in order to know what value to pass to the method. Is there a way of deferring evaluation until it actually hits that bit of the code inside parser?

I’ve tried using a lazy value, something like:

lazy val error = {throw new Error("Installation not specified")}

val installation: String = parser("-installation", error , identity)

but this doesn’t work either.

  • 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-25T11:48:30+00:00Added an answer on May 25, 2026 at 11:48 am

    You need to pass default by-name, with => T, if you want it to be evaluated only if need be

    def parser[T](identifier: String, default : => T; modified: String => T): T = ...
    

    Otherwise, the arg of a function are evaluated when the function is called (call by value). Your lazy val simply causes error to be evaluated when you call parser passing it as argument.

    On the other hand, you can use lazy val inside a method, when you do not want a call by name argument to be evaluated each time it is referenced.

    def f(x: => T) {
      lazy val y = x
      // use y
    }
    

    In your case, it is not needed, as default is called at most once in parse.

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

Sidebar

Related Questions

Apologies for the rather confusing title, I'm finding it rather difficult to phrase this
My apologies if the title of this thread is a little confusing. What I'm
I could not think of a better way to title this question. my apologies
I didn't really know how to phrase the title of my questions, so my
My apologies if it is quite confusing. I have this two main interfaces. First
Confusing title...my apologies. What I've got is a table with two related rows. I
I apologize for the confusing post Title. This is what I mean: Lets say
I realize that the title may be somewhat confusing, so I apologize. Basically, this
Firstly: apologies if this is a duplicate post. Things got a bit confusing as
Apologies if this is too ignorant a question or has been asked before. A

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.