1

I really like how Kotlin wraps up constructors with MyClass(val myValue:Double) to avoid all the excess constructor assignments.

Is there a similar way to shortcut things in a function call in a class? Something that bundles up

fun updateLocations(newX: Double, newY: Double) { x = newX y = newY 

into

fun updateLocations(magicupdate x, magicupdate y) { // internal x and y are already updated... 
1
  • 2
    There isn't special syntax for this, you'd just have to assign these values yourself, as shown in your question. Commented Sep 18, 2018 at 17:25

1 Answer 1

1

No, there is no such way to do this for functions. You have to explicitly assign values to the class level properties.

MyClass(val myValue:Double) 

this is a constructor and the compiler understands that we are assigning the properties to the class.

Whereas functions might not always have any side effect.

Class members should represent state of the class object. They are not temporary locations for method parameters (that's what method parameters are for).

Take a look at following links for side effects and pure functions.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.