Eric Ou
08/04/2022, 11:08 AMoperator fun usedAs(): SomeType
When the code is compiled, this method is automatically called if it's used as a parameter in that type.
That might be a bit confusing, let me elaborate.
operator fun String.usedAs(): URL = URL(this)
And then the user can use String in place of URL in method call and constructors. Then the compiler can desugar it to the method call.
Though since now you can't really have two methods with the same name, I would make it something more like
@Converter(URL::class)
fun asURL(): URL = //...
Fleshgrinder
08/04/2022, 11:12 AMinterface I
class A : I
class B : I
operator fun String.usedAs(): A = TODO()
operator fun String.usedAs(): B = TODO()
fun f(i: I) { TODO() }
fun main() {
f("") // A or B? ๐ค
}
into
and it often leads to the infamous turbo-fish. If anything, then it should work like Rust's impl.interface I
class A : I
class B : I
operator fun String.into(): A = TODO()
operator fun String.into(): B = TODO()
fun f(i: I) { TODO() }
fun a(a: A) { TODO() }
fun b(b: B) { TODO() }
fun main() {
f("".into()) // compile error
f("".into<A>()) // ๐
a("".into()) // ๐
b("".into()) // ๐
}
Eric Ou
08/04/2022, 11:14 AMFleshgrinder
08/04/2022, 11:15 AMEric Ou
08/04/2022, 11:16 AM.into()
part to be skipped entirely, like an implicit type coercion.Fleshgrinder
08/04/2022, 11:16 AMto*
methods that often end up being wild. This way Java interop could also be ensured. An into(): A
would turn to a toA()
in Java.Eric Ou
08/04/2022, 11:16 AMFleshgrinder
08/04/2022, 11:17 AMEric Ou
08/04/2022, 11:19 AMimplicitConversion {
somethingWithURL("<http://example.com|example.com>")
}