Git Product home page Git Product logo

Comments (7)

Randgalt avatar Randgalt commented on July 17, 2024

This is what I have now. Is this the correct generated version?

// Auto generated from io.soabase.halva.comprehension.ReaderOptionalForFactory by Soabase io.soabase.halva.comprehension.MonadicFor annotation processor
package io.soabase.halva.comprehension;

import io.soabase.halva.any.AnyVal;
import java.lang.SuppressWarnings;
import java.util.function.Supplier;
import javax.annotation.Generated;

@Generated("io.soabase.halva.comprehension.MonadicFor")
public class ReaderOptionalFor<A> {
    private final MonadicForImpl<Reader> delegate;

    private ReaderOptionalFor(MonadicForImpl<Reader> delegate) {
        this.delegate = delegate;
    }

    public static <A> ReaderOptionalFor<A> start() {
        return new ReaderOptionalFor<A>(new MonadicForImpl<Reader>(new ReaderOptionalForFactory()));
    }

    public <MF_A, MF_B> ReaderOptionalFor<A> forComp(AnyVal<MF_B> any, Supplier<? extends Reader<MF_A, MF_B>> supplier) {
        delegate.forComp(any, supplier);
        return this;
    }

    @SuppressWarnings("unchecked")
    public <MF_A, MF_B> Reader<MF_A, MF_B> yield(Supplier<MF_B> supplier) {
        return delegate.yield(supplier);
    }

    public <R> ReaderOptionalFor<A> letComp(AnyVal<R> any, Supplier<R> supplier) {
        delegate.letComp(any, supplier);
        return this;
    }
}

from soabase-halva.

Alex-At-Home avatar Alex-At-Home commented on July 17, 2024

Yep that looks right, awesome thanks!

from soabase-halva.

Alex-At-Home avatar Alex-At-Home commented on July 17, 2024

Ah no sorry that was wrong, if needs to look like this: https://github.com/Alex-At-Home/halva-scalaz-examples/blob/master/src/test/java/person/alexp/halva/examples/ReaderOptionalFor2.java

ie if you specify a (single) inner type (and any number of template types) instead of leaving it untyped (eg Optional and A here) then you're declaring that you're composing Monads

from soabase-halva.

Alex-At-Home avatar Alex-At-Home commented on July 17, 2024

Hmm interesting, it appears that now if you have >1 generic type in your monad then you can't use the untyped version, eg

  • public class OptionalForFactory implements MonadicForWrapper<Optional> { is fine because Optional only takes one type
  • public class ReaderOptionalForFactory<A> implements MonadicForWrapper<Reader<A, Optional>> { is fine because both types are specified
  • public class ReaderForFactory implements MonadicForWrapper<Reader> { now errors

The reason we specified no types was that the "monadic type" can change via the flatMap/for comprehension, eg Reader<String, Int> -> flatMap(a -> a.toString()) -> Reader<String, String>.

So you'd have to do something like:

  • public class ReaderForFactory<A> implements MonadicForWrapper<Reader<A, ?>> { - which is a bit nasty because it becomes quite tricky to persuade the flatMap to compile - the old untyped expression was attractive even though it wasn't super type safe because it was nice and easy to override the flatMap/map, vs having to wrestle with Java type inference.
    • (you don't get any additional type safety after writing the for wrapper)

So I think it should be possible just to revert to the old logic for untyped multi-generic-type monads?

from soabase-halva.

Alex-At-Home avatar Alex-At-Home commented on July 17, 2024

If there is a way if making the `MonadicForWrapper fully type safe then I am all for it :)

eg you could try to do

  • public class OptionalForFactory<T> implements MonadicForWrapper<Optional<T>> {
  • public class ReaderForFactory<A, T> implements MonadicForWrapper<Reader<A, T>> {
  • public class ReaderOptionalForFactory<A, T> implements MonadicForWrapper<Reader<A, Optional<T>>> {

and then the forComp would become (eg):

  • ReaderForFactory<A, T2> forComp(Any<T2>, Supplier<T2>) (etc)

I think that will work fine, and it was just more trouble than it was worth in my example code that inspired your annotations, because I wasn't auto generating code or whatever

Or it works fine as is, with the 2 lines commented out from the last commit (I stuck a message in the linked commit) :)

from soabase-halva.

Randgalt avatar Randgalt commented on July 17, 2024

The behavior is now optional. Use @MonadicFor(applyParentTypeParameter = true) to get the enhanced behavior. This will allow you to choose how the generation is done. Let me know if this solves the issue.

from soabase-halva.

Randgalt avatar Randgalt commented on July 17, 2024

I'm going to close this as the original issue is dealt with. If you want more functionality open another issue.

from soabase-halva.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.