This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
forked from eclipse-archived/golo-lang
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support an explicit fallback method for augmentations
Failed dispatch can have a last chance to be resolved thanks to a normalized fallback method defined in augmentations. Such mecahism permits ease the implemententtion of dynamic methods, DSL or fluent APIs.
- Loading branch information
1 parent
aa2284f
commit daade3c
Showing
6 changed files
with
93 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
34 changes: 34 additions & 0 deletions
34
src/test/resources/for-execution/augmentations-with-fallback.golo
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module golotest.execution.AugmentationsWithFallback | ||
|
||
import java.util.stream.Collectors | ||
|
||
augment java.lang.String { | ||
function fallback = |this, name, args...| -> "Fallback for this " + this + " named " + name + " with args" + args: asList() | ||
} | ||
|
||
augment Fluent { | ||
function fallback = |this, name, args...| { | ||
return match { | ||
when args: length() is 1 then this: bindAt(name, args: get(0)) | ||
otherwise this: bindAt(name, args) | ||
} | ||
} | ||
} | ||
|
||
augment gololang.FunctionReference with Fluent | ||
|
||
function fallbackExists = -> "bouh" : casper(1,2,3) | ||
|
||
function fallbackDoesNotExists = -> java.util.ArrayList() : casper() | ||
|
||
function greet = |firstName, lastName| -> "Hello " + firstName + " " + lastName + "!" | ||
|
||
function fallbackOnAugmentedFunctionReference = -> ^greet: firstName("John"): lastName("Doe"): invoke() | ||
|
||
function fallbackOnAugmentedClosure = { | ||
let joiner = |delimiter, values...| -> values: asList(): stream(): | ||
map(|it| -> it: toString()): collect(Collectors.joining(delimiter)) | ||
|
||
return joiner: values(array[1,2,3]): delimiter("-"): invoke() | ||
} | ||
|
Empty file.