r/javahelp • • Jun 24 '20

Workaround How to write a polymorphic visitor?

I have code like the following. Is there an easy way to make this work and work type safely?

I know I could pass on a witness but that is ugly.

public interface TermVisitor<B, C> {

    <Domain, Range> C lambda(Variable<Domain> binder, Term<Range> body);


    <A> C apply(Term<Function<A, B>> f, Term<A> x);


    C global(Global<B> global);


    C constant(Constant<B> constant);


    C local(Variable<B> binder);

}

public interface Term<A> {

    <C> C visit(TermVisitor<A, C> visitor); 

}

public record LambdaTerm(Variable<Domain> binder, Term<Range> body) implements Term<Function<A, B>> {

}
0 Upvotes

1 comment sorted by

2

u/tr4fik Jun 24 '20

Could you detail your problem ? And what LambdaTerm is ?