Some questions about: Extension Functions



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 = 1
           Module B:   fun Int = 3
           Module C (which has dependency to both A and B) :   
                   val x =   //   the result will be 1 ? or 3 ?  or  compile error ?


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



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.


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 as baz1


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

Thank you.