Created
April 12, 2023 05:53
-
-
Save benjiman/3244a88bc53045d79242621c483636c7 to your computer and use it in GitHub Desktop.
Named Parameters Example
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 example; | |
public class NamedParamsExample { | |
public static void main(String... args) { | |
foo("Bob", 29); | |
foo(new FooParams(){{name="Bob"; age=30; }}); | |
foo(Foo.name("Bob").age(31)); | |
} | |
static void foo(String name, Integer age) { | |
System.out.println(name + " " + age); | |
} | |
public static class FooParams{String name; Integer age; } | |
static void foo(FooParams params) { | |
foo(params.name, params.age); | |
} | |
interface Foo { static Foo name(String named) { return aged -> new FooParams(){{name=named; age=aged;}}; } FooParams age(Integer aged); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment