Style question


#1

I have a code style best practices question.

Let’s say I have the following code:

class ClassWithFairlyLongName {

	var data: Int = 2
	
	init {
		foo {
			this@ClassWithFairlyLongName.data = data
		}
	}
	
}

class Foo() {
	var data: Int = 3
}

fun foo(init: Foo.()->Unit): Foo {
	val f = Foo()
	f.init()
	return f
}

What do people do in this case? this@ClassWithFairlyLongName is rather long and ugly.
I’m not in love with val r = this before the receiver changes, and in the case I’m struggling with it doesn’t make sense to change the name of data.

-N


#2

Well, this is not really a problem that can be resolved in a nice way. Obviously you have to tell the compiler which property you are referencing. So my advice would be, not stacking functions with different receivers. Obviously this is not always possible but you could for example use also instead of apply and let instead of run.
In cases where I have to access receivers using @ multiple times I declare a temp variable, otherwise I would use @.
What I would definitely not do is change the name of the property, they have that name for a reasons and it should not depend on where the property is used.


#3

Can you change Foo declaration?

class ClassWithFairlyLongName {

	var data: Int = 2
	
	init {
		Foo(data = data)
	}
	
}

class Foo(var data: Int = 3)