-
Notifications
You must be signed in to change notification settings - Fork 48
Open
Description
It may happen that value constructors of an ADT take, say, five or more arguments. The pattern matching clause generated by derive4j can then become a bit ugly (necessitating a huge lambda expression) especially when you only need a few of all the arguments.
Would it be possible to generate "matchers" that would accept "tupled" lambdas ? (see the following example)
@Data(flavour = Flavour.FJ)
static abstract class Staff { // a dumb type
Staff() {}
interface Cases<R> {
R Common(String name1, String name2, String name3, String name4, Integer age, String adress);
R Admin(String name1, String name2, String name3, String name4, Integer age, String adress, String role);
}
abstract <R> R match(Cases<R> cases);
}
// current state of affairs
IO<Unit> test(Staff staff) {
return Staffs.caseOf(staff)
.Common((name1, name2, name3, name4, age, adress) -> IOFunctions.stdoutPrint(name3))
.Admin((name1, name2, name3, name4, age, adress, role) -> IOFunctions.stdoutPrint(role));
}
// proposition
IO<Unit> tupledLambda(Staff staff) {
return Staffs.caseOf(staff)
// alternative name to preserve type inference (not sure it would be necessary)
._Common(__ -> IOFunctions.stdoutPrint(__._3())) // '__' would be of type P6<String, String, String, String, Integer, String>
._Admin(__ -> IOFunctions.stdoutPrint(__._7())); // '__' would be of type P7<...>
}What do you think ?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels