GenericPath
GenericPath<F, A> is the escape hatch. It wraps any Kind<F, A> with
a Monad instance, letting you use Path operations on custom types.
- Creating GenericPath instances
- Working with custom monads
- Extraction patterns
- When to use (and when not to)
Creation
Monad<ListKind.Witness> listMonad = Instances.monadZero(list());
Kind<ListKind.Witness, Integer> listKind =
ListKindHelper.LIST.widen(List.of(1, 2, 3));
GenericPath<ListKind.Witness, Integer> listPath =
Path.generic(listKind, listMonad);
Core Operations
GenericPath<ListKind.Witness, Integer> numbers = Path.generic(listKind, listMonad);
GenericPath<ListKind.Witness, String> strings = numbers.map(n -> "n" + n);
GenericPath<ListKind.Witness, Integer> doubled = numbers.via(n ->
Path.generic(ListKindHelper.LIST.widen(List.of(n, n * 2)), listMonad));
Extraction
Kind<ListKind.Witness, Integer> kind = path.runKind();
List<Integer> list = ListKindHelper.LIST.narrow(kind);
When to Use
GenericPath is right when:
- You have a custom monad not covered by specific Path types
- Writing highly generic code across multiple monad types
- Experimenting with new effect types
When your library's users write the type in their own signatures, a Path class generated for your
effect reads better: for an effect Traced, TracedPath<A> rather than
GenericPath<TracedKind.Witness, A>.
Custom Paths with @PathSource compares the two.
GenericPath demonstrates the power of higher-kinded types in Java: write
your algorithm once, and it works with Maybe, Either, List, IO, or
any custom monad. This is the same abstraction power that makes libraries
like Cats and ZIO flexible in Scala, now available in Java.
- HKT Introduction: Higher-kinded type basics
- Extending: Creating custom types
- Custom Paths with
@PathSource: A named Path class generated for your own effect
Previous: OptionalPath
Next: Custom Paths with @PathSource