0

I am confused as to what the issue is here. I have an function:

def myFunc(activity:Activity):updatedActivity = {
    //var updatedActivity:Activity = activity <- This doesnt work either
    var updatedActivity:Activity = activity.copy()

    val params =  Params (
      "Attribute"
    )

    updatedActivity.parameters = params
    updatedActivity
}

This will not work, because reassigning an attribute updatedActivity.parameters = params throws a reassignment of val error, even though updatedActivity is a var. How can I update the activity parameter I passed in and return it?

1
  • 1
    updatedActivity may be a var, but if it's a case class then the attribute parameters is definitely a val, this is why you are getting the error. Commented Oct 26, 2017 at 16:31

2 Answers 2

4

updatedActivity may be a var but class Activity has a field called parameters which is a val. So it must be assigned during construction.

if its a case class, try this:

def myFunc(activity:Activity):updatedActivity = {   
    val params =  Params (
      "Attribute"
    )

    activity.copy(parameters = params)
}

If it is not a case class, it has to be passed as a regular parameter to the constructor, i.e.

new Activity(params)
Sign up to request clarification or add additional context in comments.

Comments

1

I'm assuming that Activity is a case class. Even though updatedActivity is a var, the underlying value (case class) is immutable, meaning that you can reassign updatedActivity, but cannot change the current value in place. Instead, you can just return:

updatedActvity.copy(parameters = params)

Comments

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.