2

i have a question about scala and logic in constructor. Lets say i have a following code:

class A(val x:Int) {...whatever...}

class B(val y:String) extends A(IntValueDerivedFrom_y)

Now, how would i derive some value from y and passed it to constructor of class A? I hope it's understandable what i ask about.

Thanks for the answers!

1 Answer 1

4

Not sure I understand. You may do

class B(val y: String) extends A(f(y))

f(y) stands for any expression where y appears. For instance, Integer.parseInt(y)

This is close to java code

class B extends A {
   public B(String y) {
       super(Integer.parseInt(y));
   }
}

Is that what you wanted?

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah that's it. I tried that approach before but the f in my case was defined inside the class itself which compiler couldnt find (obviously). I moved it to object b and it works properly now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.