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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:53:26+00:00 2026-05-14T14:53:26+00:00

I have a class where I like to initialize my var by reading a

  • 0

I have a class where I like to initialize my var by reading a configfile, which produces intermediate objects/vals, which I would like to group and hide in a method.
Here is the bare minimum of the problem – I call the ctor with a param i, in reality a File to parse, and the init-method generates the String s, in reality more complicated than here, with a lot of intermediate objects being created:

class Foo (val i: Int) {

    var s : String;

    def init () {
        s = "" + i 
    }

    init ()
}

This will produce the error: class Foo needs to be abstract, since variable s is not defined. In this example it is easy to solve by setting the String to “”: var s = "";, but in reality the object is more complex than String, without an apropriate Null-implementation.

I know, I can use an Option, which works for more complicated things than String too:

var s : Option [String] = None

def init () {
    s = Some ("" + i) 
}

or I can dispense with my methodcall. Using an Option will force me to write Some over and over again, without much benefit, since there is no need for a None else than to initialize it that way I thought I could.

Is there another way to achieve my goal?

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

    Instead of creating separate methods for initialization, you should perform the initialization using the following way :

    class Foo(val i: Int) {
      var s: String = {
        var s0 = " "
        s0 += i
        // do some more stuff with s0
        s0
      }
    
      var dashedDate = {
        val dashed = new SimpleDateFormat("yy-MM-dd")
        dashed.format(updated)
      }
    
      // Initializing more than one field:
      var (x, y, z) = {
        var x0, y0, z0 = 0
        // some calculations
        (x0, y0, z0)
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lets say I have a class like so. var foodData = Class.create({ initialize: function()
I have an object like this: var someObj = Class.create ({ initialize: function(objName){ this.objName
I have something like the following class A def initialize @var = 0 end
I would like to initialize a class, then call an attribute which was set
I have a Rack application that looks like this: class Foo def initialize(app) @app
I have class like this below shown. which contains the shopping items where the
I have a code sample which look something like this. public class AdventureWorksRepository {
I have this little script: var moolang = new Class({ initialize: function(element) { this.el
In Ruby, when I do something like this: class Foo ... def initialize( var
I have a ruby class that is using the Singleton pattern. I would like

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.