Last active
August 29, 2015 14:22
-
-
Save javierfernandes/a78750bf86bbfd3a451a to your computer and use it in GitHub Desktop.
A demonstration of different usages of Xtend extension methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main(args: Array<String>) { | |
| val i = 1 | |
| i.doSomething() // FINE | |
| val s = "string" | |
| s.doSomething() // FINE | |
| val col = linkedListOf(i, s) | |
| col forEach { it.doSomething() } // COMPILATION ERROR | |
| } | |
| fun Int.doSomething() { println("This is a List Extension") } | |
| fun String.doSomething() { println("This is a Set Extension") } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package uqbar | |
| class Extensions { | |
| def static void main(String[] args) { | |
| val col = newLinkedList(i, s) | |
| col.forEach [ it.doSomething() ] // <-- FINE | |
| } | |
| def static dispatch doSomething(Integer i) { | |
| println("This is an Integer Extension") | |
| } | |
| def static dispatch doSomething(String i) { | |
| println("This is a String Extension") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package uqbar | |
| class Extensions { | |
| // static extension ExtensionProvider = new PrintingExtensionProvider | |
| static extension ExtensionProvider = new NothingExtensionProvider | |
| def static void main(String[] args) { | |
| val i = 1 | |
| i.doSomething() // FINE | |
| val s = "string" | |
| s.doSomething() // FINE | |
| val col = newLinkedList(i, s) | |
| col.forEach [ it.doSomething() ] // FINE | |
| } | |
| } | |
| interface ExtensionProvider { | |
| def void doSomething(Object obj) | |
| } | |
| class PrintingExtensionProvider implements ExtensionProvider { | |
| override doSomething(Object obj) { | |
| println("This is an Extension") | |
| } | |
| } | |
| class NothingExtensionProvider implements ExtensionProvider { | |
| override doSomething(Object obj) { | |
| // does nothing | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package uqbar | |
| class Extensions { | |
| // static extension ExtensionProvider = new PrintingExtensionProvider | |
| static extension ExtensionProvider = new NothingExtensionProvider | |
| def static void main(String[] args) { | |
| val i = 1 | |
| i.doSomething() // FINE | |
| val s = "string" | |
| s.doSomething() // FINE | |
| val col = newLinkedList(i, s) | |
| col.forEach [ it.doSomething() ] // FINE | |
| } | |
| } | |
| interface ExtensionProvider { | |
| def void doSomething(Object obj) | |
| } | |
| class PrintingExtensionProvider implements ExtensionProvider { | |
| def dispatch doSomething(String obj) { << multiple dispatch here | |
| println("This is an String Extension") | |
| } | |
| def dispatch doSomething(Integer obj) { << multiple dispatch here | |
| println("This is an Integer Extension") | |
| } | |
| } | |
| class NothingExtensionProvider implements ExtensionProvider { | |
| override doSomething(Object obj) { | |
| // does nothing | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment