Some questions about: Extension Functions


#1

Hi,

Two questions regarding Extension Functions:

1 - we can’t access to object’s private or protected properties within extension function. Am I right?  Appart from that, What are the other differences between extension functions and normal functions that are defined within the class?

2 -  what happens if we define two extension functions with same name and signature in two different module, and use them in a third module?

           Module A:   fun Int.foo(): Int = 1
           Module B:   fun Int.foo(): Int = 3
           Module C (which has dependency to both A and B) :   
                   val x = 2.foo()   //   the result will be 1 ? or 3 ?  or  compile error ?


#2

1. Yes, you are right. 2. Compile time error: ambiguity


#3

@Andrey,

let’s Imagine module or library A is develped by Company, or team A, and library B is developed by company B.  Then a third company, or team, CANNOT be dependent to both libraries in the same time.  I know it’s less likely to happen but It can be a serious issue.


#4

Not exactly so: you can depend on both libraries, but you can not use both functions in the same file, unless you rename one of them on import:

import foo.bar.baz as baz1


#5

yeah, you are right. I didn't know, just like normal funcation, we can rename extension functions on import.

Thank you.