Home > front end >  Distinguish function from extension
Distinguish function from extension

Time:11-17

I tried to write a funtion, which can be inserted in any expresion, in order to log the value:

val x = (2.debug()   3.debug()).debug("2 3")

But instead I wrote the following endless loop:

fun debug (message: String) {
    Log.d (R.string.app_name.toString(), message) }

fun <T> T.debug (tag: String = "value"): T {
    debug ("$tag: $this")
    return this 
}

My aim was to write a "normal" function (1st) and an extension function (2nd) and the extension function should call the normal function.

The problem in my code is: the extension function calls itself instead of the normal function. I do not understand this, because I did not specify an instance receiver in the extension function.

How to fix this?

CodePudding user response:

Given you have different param names in each function, you could change the second function to call the first one with named arguments:

fun <T> T.debug (tag: String = "value"): T {
    debug (message = "$tag: $this")
    return this
}

CodePudding user response:

I couldn't find a way to strip this out of extension method, so the best way out would be to create differently named wrapper:

fun <T> T.debug (tag: String = "value"): T {
    debugWrapper( "$tag: $this")
    return this
}

fun debug (message: String) {
    Log.d ("tag", message)
}

private fun debugWrapper (message: String) {
    debug(message)
}

I think you were looking to keep existing calls to debug and this delivers.

I've tried to look into decompiled code (minus the default parameter, for clarity) in hope to differentiate them by namespace:

public final class TestclassKt {
    public static final Object debug(Object $this$debug, @NotNull String tag) {
        Intrinsics.checkNotNullParameter(tag, "tag");
        debug($this$debug, tag   ": "   $this$debug);
        return $this$debug;
    }

    public static final void debug(@NotNull String message) {
        Intrinsics.checkNotNullParameter(message, "message");
        Log.d("tag", message);
    }
}

But the 2 methods are in same namespace and the only way to differentiate them is by arguments.

I think you've broken Kotlin.

  • Related