# Overloading using extension method

**URL:** <https://discuss.kotlinlang.org/t/overloading-using-extension-method/4990>\
**Category:** Uncategorized\
**Created:** [October 15, 2017, 10:00am UTC](https://discuss.kotlinlang.org/t/overloading-using-extension-method/4990 "2017-10-15T10:00:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![musketyr](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/musketyr/32/4209_2.png) [@musketyr](https://discuss.kotlinlang.org/u/musketyr)\
**Post date:** [October 15, 2017, 10:00am UTC](https://discuss.kotlinlang.org/t/overloading-using-extension-method/4990/1 "2017-10-15T10:00:11Z")

</div>

Hi,

I’m new to Kotlin and I am trying to implement Kotlin DSL for [http://spreadsheet.dsl.builders/](http://spreadsheet.dsl.builders/)

I hope it would be as easy as writing extension methods to current interfaces, e.g.

```java
public interface SpreadsheetBuilder {
    void build(Configurer<WorkbookDefinition> workbookDefinition);
}

```

will be extended with the proper receiver

```kotlin
fun SpreadsheetBuilder.build(workbookDefinition: WorkbookDefinition.(WorkbookDefinition) -> WorkbookDefinition) {
    return this.build {
        workbookDefinition(it, it)
    }
}

```

but I found that still the first method is resolved when I try to write

```kotlin
        PoiSpreadsheetBuilder.create(excel).build {
            sheet("test") {
                row {
                    cell { value("Hello World") }
                }
            }
        }

```

the original Java `build` method is resolved. Is this an intention? I would expect that method accepting function would have the preference to the method accepting SAM type. Any ideas how to fix this other than creating some stupid names such as `_build` or so?

I think more frameworks might take advantage of such a feature that SAM types will be replaced with functions with receivers

---

<div class="post-metadata">

**Author:** ![forinil2](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/forinil2/32/2465_2.png) [@forinil2](https://discuss.kotlinlang.org/u/forinil2)\
**Post date:** [October 16, 2017, 6:51am UTC](https://discuss.kotlinlang.org/t/overloading-using-extension-method/4990/2 "2017-10-16T06:51:44Z")

</div>

As explained here: [https://kotlinlang.org/docs/reference/extensions.html](https://kotlinlang.org/docs/reference/extensions.html) (section ‘Extensions are resolved statically’) , if a class has a member function, and an extension function is defined which has the same receiver type, the same name and is applicable to given arguments, the member always wins.

In other words when method `build (Configurer<WorkbookDefinition> workbookDefinition)` is called on a class implementing interface `SpreadsheetBuilder` (assuming it was earlier cast to type `SpreadsheetBuilder` - see the same page and section as before) Kotlin uses whatever implementation of member function that class has, rather than calling the extension function.

You should simply create your own implementation of the interface and make sure it’s returned by `create` method of `PoiSpreadsheetBuilder` class.

---

<div class="post-metadata">

**Author:** ![musketyr](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/musketyr/32/4209_2.png) [@musketyr](https://discuss.kotlinlang.org/u/musketyr)\
**Post date:** [October 16, 2017, 7:40am UTC](https://discuss.kotlinlang.org/t/overloading-using-extension-method/4990/3 "2017-10-16T07:40:26Z")

</div>

thanks, @forinil2. I’ve seen that documentation before but the method is not exactly the same. in my point of view, method accepting function with the receiver is more special than the one accepting `Configurer<WorkbookDefinition>`.

There is no point to create shadow interface structure just for single language for the framework. Developers still can use the framework the Java way but they won’t gain any bonus in less repetition.

---

<div class="post-metadata">

**Author:** ![musketyr](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/musketyr/32/4209_2.png) [@musketyr](https://discuss.kotlinlang.org/u/musketyr)\
**Post date:** [October 16, 2017, 8:01am UTC](https://discuss.kotlinlang.org/t/overloading-using-extension-method/4990/4 "2017-10-16T08:01:04Z")

</div>

I’ve resolved issue in the tracker but I have no clue what is the outcome:

[https://youtrack.jetbrains.com/issue/KT-12848](https://youtrack.jetbrains.com/issue/KT-12848)

I would be able to add annotation to that Java interface `Configurer`.
