Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about unification parameter #130

Open
twizzlespark opened this issue Nov 6, 2023 · 1 comment
Open

Question about unification parameter #130

twizzlespark opened this issue Nov 6, 2023 · 1 comment
Assignees

Comments

@twizzlespark
Copy link

twizzlespark commented Nov 6, 2023

First of all, i'm really sorry to ask questions in issues tab. I'll be sure to remove this post as soon as i get an answer.
I'm a java web developer who is serious about learning functional programming, with hopes to one day apply it in production level codes.

I've been trying really hard to figure out how lambda library works, but i can't figure out what "unification parameter" is in the library is supposed to be, or what it is for.

The bit of code i am referring to:

 * @param <A> The type of the parameter
 * @param <F> The unification parameter

...

@FunctionalInterface
public interface Functor<A, F extends Functor<?, F>> {
...

I've been unable to figure this out with regular google search, but i am guessing that this is some sort of compiler trick to enforce strict compile time type checks.

Any explanation or any directions to where i should be looking would be greatly appreciated.
Thank you very much.

@7h3kk1d
Copy link
Member

7h3kk1d commented Nov 9, 2023

Hi @twizzlespark,

The parameter F is meant to be used as a recursively bound type parameter similar to T in Comparable<T> where Integer implements Comparable<Integer>. In this case it's the type that's actually implementing the functor interface so for example with Maybe:

public abstract class Maybe<T> implements Functor<T, Maybe<?>> {
   // Implementation here
}

The "unification parameter" is necessary to have a sensible return type for the fmap method on Functor. We want fmap to return a Maybe when called and not some other type of a functor. So on the interface it has:

    <B> Functor<B, F> fmap(Fn1<? super A, ? extends B> fn);

And in the case of Maybe F is Maybe so you can return a Maybe<B>. Please let me know if any of that doesn't make sense or if you have any other questions. Also, feel free to join the discord channel linked in the readme.

@7h3kk1d 7h3kk1d self-assigned this Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants