``` inline fun Foo.notMyFunction(block: Bar.() -&g...
# announcements
a
Copy code
inline fun Foo.notMyFunction(block: Bar.() -> Unit) {
    bar.apply(block)
}

inline fun <R> Foo.myFunction(block: (Bar) -> R): R {
    notMyFunction {
        return let(block)
    }
    error("")
}
is there any reasonable way to implement
myFunction
given
notMyFunction
?
m
Only when using an interface which declares
notMyFunction
. But you can't use
inline
then.
d
return block()
insteas of let(block)?
a
let(block)
,
block(this)
, whatever, but mainly I’m wondering if it’s possible to get some value out of there without that
error("")
at the end reason was: using a function with similar signature in class initialization - can’t initialize properties inside the lambda, but could assign the property with result of
myFunction
m
If you want the compiler to know that the
notMyFunction
invocation never returns you have to tell it that its lambda is called exactly once.
Copy code
@UseExperimental(ExperimentalContracts::class)
inline fun Foo.notMyFunction(block: Bar.() -> Unit) {
	contract {
		callsInPlace(block, InvocationKind.EXACTLY_ONCE)
	}

	bar.apply(block)
}

inline fun <R> Foo.myFunction(block: (Bar) -> R): R {
	notMyFunction {
		return let(block)
	}
}
a
I guess it would be nice, but
notMyFunction
is not mine, so I’d have to request that feature. Still, even if it was added, would it be possible to get the value out of there, without defining
myFunction
? Assuming
Bar
has a
getBoolean
function:
Copy code
class Baz(foo: Foo) {
    val spam: Boolean = foo.notMyFunction {
        // some magic to make apply work like let and return:
        getBoolean("spam")
    } 
}
m
Copy code
class Baz(foo: Foo) {
	val spam = with(foo) { lateinit var magic: String; notMyFunction { magic = "spam" }; magic }
}
k
If it has the right contract this just works:
Copy code
@UseExperimental(kotlin.contracts.ExperimentalContracts::class)
inline fun Foo.notMyFunction(block: Bar.() -> Unit) {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }

    bar.apply(block)
}

fun main() {
    val foo = Foo()

    val spam: Boolean
    foo.notMyFunction {
        spam = getBoolean("spam")
    }
    
    println(spam)
}