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

["Request"] Optic composition with standard Kotlin KPropertys #2418

Closed
magneticflux- opened this issue Jun 9, 2021 · 3 comments
Closed

["Request"] Optic composition with standard Kotlin KPropertys #2418

magneticflux- opened this issue Jun 9, 2021 · 3 comments

Comments

@magneticflux-
Copy link
Contributor

magneticflux- commented Jun 9, 2021

What version are you currently using?
0.13.2

What would you like to see?

Having extension functions similar to the existing ones like Getter<A,B> + Getter<B,C> -> Getter<A,C> would be super handy.

I found out that this code works (because Getter is a functional interface?):

fun example(getter: arrow.optics.Getter<T, V>) { }

fun main() {
    val prop: KProperty1<T, V> = TODO("External")
    example(prop)
}

So naturally now I want to be able to use typical optic compositions on the results.

This won't be able to replace the @optics annotation since it can't derive the equivalent Setter-type lenses, but it would make it easier to create optics on non-constructor parameters of data classes.

I am currently using this snippet:

operator fun <T, V, U> KProperty1<T, V>.plus(other: Getter<V, U>): Getter<T, U> {
    return Getter {
        other.get(this(it))
    }
}
@nomisRev
Copy link
Member

nomisRev commented Jun 9, 2021

Hey @magneticflux-,

That's a great idea, and a great addition to Optics! If you're up for adding your snippet in a small PR that'd be awesome.

(because Getter is a functional interface?):

Since Getter<S, A> is a functional interface it matches anything of the shape (source: S) -> A.
fun interface works like @FunctionalInterface in Java where you can pass anything of the same shape of the single abstract method.

@serras
Copy link
Member

serras commented Jul 29, 2022

@magneticflux- since some time we have an arrow-optics-reflect which allows turning any KProperty into a lens. If you have time to have a look and give feedback, that would be awesome!

@magneticflux-
Copy link
Contributor Author

Looks good, thank you!

Something like Scala's implicit conversions would be awesome, but unfortunately I don't think that's on the roadmap for Kotlin since it's a bit niche.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants