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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:49:38+00:00 2026-06-10T21:49:38+00:00

I spent all this time putting together a factory method in my companion object

  • 0

I spent all this time putting together a factory method in my companion object like so:

class Stuff(val a: Int, val b: Long) { this() = this(0,0L) }

object Stuff {
  def apply(a:Int, b:Int) = new Stuff(a, b.toLong)
}

But when just when I thought I was killing it I then went to compile and this didn’t work:

val widget = new Stuff(1,2)

What is going on!? I just made this!? Help!!!

  • 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-10T21:49:39+00:00Added an answer on June 10, 2026 at 9:49 pm

    Well young Scala coder, have no fear because the answer is simple. You are not using the factory correctly. See, this code will actually do what you want:

    val widget = Stuff(1,2)
    //makes Stuff(1, 2L)
    

    The issue here is your syntax. When you call new it instantiates a new class of Stuff. But apply is really syntactic sugar for widget.apply(1,2) and there’s not much else to it.

    You can also learn more about the apply sugar here: How does Scala's apply() method magic work?

    Keep coding young one.

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

Sidebar

Related Questions

I have spent all Friday and Saturday on this and I am running out
I've spent all morning searching and trying solutions to this problem with no success
I spent some time looking around, and all I could find is Jython. It's
I'm profiling some code, and cProfile reports that almost all the time is spent
I hear all this stuff about crazy ways to build iPhone apps using Ruby
I've spent a bit of time researching this on here and the mysql site
I'm having a really hard time with this one, EDIT: I'm putting this edit
I have spent a hell lot of time on this bug and I can't
I've spent a long time trying to figure out this issue, I need a
I have spent all day trying to solve this with no satisfying results. I

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.