Skip to content

Ignoring arguments while pattern matching #73

@gneuvill

Description

@gneuvill

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions