Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new @NoLens annotation #886

Merged
merged 3 commits into from
Sep 9, 2024
Merged

Conversation

Lysander
Copy link
Collaborator

@Lysander Lysander commented Sep 9, 2024

The newly introduced support for lenses generation for sealed classes or interfaces (see #876) must allow to configure whether some property should be picked for lens generation or not. This heavily depends on the implementation of those. The default case is to pick a property, but sometimes a property will not be implemented as constructor property inside the child data class. For such cases it is now possible to mark such properties with the @NoLens annotation inside the sealed type. Such marked properties will get ignored by the lens generator, so no delegating lens will be created.

Beware that this annotation is not evaluated inside the constructor of data classes!

Imagine the following example to see @NoLens in action:

@Lenses
sealed class Framework {
	// Ignore this property for delegating lens generation.
	// The property is considered to be constant for all objects,
	//  see data class below
	@NoLens
	val ignore: String

	abstract val foo: String

	companion object
}

data class Fritz2 (
	override val foo: String,
) : Framework {
	// not part of the "data", so not possible to change at copy!
	// Because of that, we cannot define any valid lens in the sealed base,
	//  so we must mark it to exclude it for lens creation!
	override val ignore: String = "Fritz2"
}

The newly introduced support for lenses generation for sealed classes or interfaces (see #876) must allow to configure whether some property should be picked for lens generation or not. This heavily depends on the implementation of those. The default case is to pick a property, but sometimes a property will not be implemented as constructor property inside the child data class. For such cases it is now possible to mark such properties with the `@NoLens` annotation inside the sealed type. Such marked properties will get ignored by the lens generator, so no delegating lens will be created.

Beware that this annotation is not evaluated inside the constructor of data classes!

Imagine the following example to see `@NoLens` in action:
```kotlin
@lenses
sealed class Framework {
	// Ignore this property for delegating lens generation.
	// The property is considered to be constant for all objects,
	//  see data class below
	@nolens
	val ignore: String

	abstract val foo: String

	companion object
}

data class Fritz2 (
	override val foo: String,
) : Framework {
	// not part of the "data", so not possible to change at copy!
	// Because of that, we cannot define any valid lens in the sealed base,
	//  so we must mark it to exclude it for lens creation!
	override val ignore: String = "Fritz2"
}
```
@Lysander Lysander added the enhancement New feature or request label Sep 9, 2024
@Lysander Lysander added this to the 1.0-RC19 milestone Sep 9, 2024
@Lysander Lysander requested a review from haukesomm September 9, 2024 08:17
@Lysander Lysander self-assigned this Sep 9, 2024
@Lysander Lysander merged commit 6d6aab4 into master Sep 9, 2024
2 checks passed
@Lysander Lysander deleted the chausknecht/no-lens-annotation branch September 9, 2024 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants