by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • May 20 13:09
  • May 15 15:27
  • May 15 15:26
  • Feb 06 17:55
    gneuvill synchronize #7
  • Feb 06 17:55

    gneuvill on j9

    Fix dep version + module-info (compare)

  • Feb 06 17:28
    gneuvill edited #95
  • Feb 06 17:28
    gneuvill edited #95
  • Feb 06 17:27
    gneuvill synchronize #95
  • Feb 06 17:27

    gneuvill on j9

    Fix dependencies versions for r… (compare)

  • Dec 03 2019 09:49
    jabrena commented #96
  • Dec 03 2019 09:21
    jbgi commented #96
  • Dec 03 2019 09:03
    jabrena commented #96
  • Dec 03 2019 09:00
    jabrena commented #96
  • Dec 03 2019 08:59
    jabrena commented #96
  • Dec 03 2019 08:56
    jabrena commented #96
  • Dec 03 2019 08:55
    jabrena commented #96
  • Dec 03 2019 08:54
    jabrena commented #96
  • Dec 03 2019 08:54
    jabrena commented #96
  • Dec 02 2019 21:57
    jbgi commented #96
H90
@h908714124
should I add one? I never bothered
I consider it public domain
free for anyone to use, sell or modify, for any purpose
Jean-Baptiste Giraudeau
@jbgi
that's not the default in most country when you don't specify anything. you can use MIT or BSD3 if you want the most liberal license
so here is the algo:
  public Optional<Map<TypeVariable, TypeMirror>> unify(TypeMirror from, TypeMirror to) {
    return unify0(from, to, new HashMap<>()).map(solution -> {
      // sanity check just in case there is something wrong:
      if (!Types.isSameType(resolve(from, tv -> get(tv, solution)), to)) {
        throw new RuntimeException("Error in unify algo");
      }
      return Collections.unmodifiableMap(solution);
    });
  }

  private Optional<HashMap<TypeVariable, TypeMirror>> unify0(TypeMirror from, TypeMirror to,
      HashMap<TypeVariable, TypeMirror> unified) {
    if (Types.isSameType(from, to)) {
      return Optional.of(unified);
    }
    if (asTypeElement(from).map(fromTE -> !asTypeElement(to).filter(fromTE::equals).isPresent()).orElse(false)) {
      return Optional.empty();
    }
    return asTypeVariable.visit(from)
        .map(tv -> Optional.ofNullable(unified.get(tv))
            .map(u -> Types.isSameType(u, to)
                ? Optional.of(unified)
                : Optional.<HashMap<TypeVariable, TypeMirror>>empty())
            .orElseGet(() -> {
              unified.put(tv, to);
              return Optional.of(unified);
            }))
        .orElseGet(() -> asDeclaredType(from).flatMap(fromDT -> asDeclaredType(to).flatMap(toDT -> traverseOptional(
            zip(fromDT.getTypeArguments(), toDT.getTypeArguments()),
            p2 -> p2.match((a1, a2) -> unify0(a1, a2, unified))).map(__ -> unified))));
  }
it will return tes type variable substitutions (if such a set exists) that can unify from with target type to
I think it does not handle wildcard though (T extends Something).
H90
@h908714124
sweet
got to run now. Will be back here next week. Bye
Jean-Baptiste Giraudeau
@jbgi
bye
TypeTool.isAssignable is the method I was looking for last week
H90
@h908714124
I was not sure if it's ok to use the typevars (TypeMirrors) as keys in the hashmap, so I used toString() on them
I get the feeling these typevars are not treated as first class citizens by the javax.model api
for example, you can't create them via Types or Elements
like you can create declared types..
Jean-Baptiste Giraudeau
@jbgi
yeah, that's true. Though for type variables part of a same type it appears to work for my purpose
probably a safer way would be to wrap the type variable in a container that implement hashCode with toString().hashCode() and equals with Types.isSameType.
H90
@h908714124
but how to do hashCode?
ok toString.. i see
well that might work
thanks
H90
@h908714124
Ok everybody, jbock 2.6.0 is released. I've slaved autistically for 4 weeks and I think it's very good now. Please check it out.
Jean-Baptiste Giraudeau
@jbgi
@h908714124 looks useful! well done ( https://twitter.com/jb9i/status/1055729954690875392)
H90
@h908714124
Thanks. I've opened a pull request to awesome-java. What are parser combinators?
oh I see. well, I'm fine with annotations for now
Martin
@mrt181_gitlab
hi, does anyone know how to configure gradle > 5.2 to generate the code from the annotations?
Martin
@mrt181_gitlab
nevermind, figured it out
dependencies {
    annotationProcessor("org.derive4j:derive4j:1.1.1")
    compileOnly("org.derive4j:derive4j:1.1.1")
}
tasks.withType(JavaCompile) {
    options.annotationProcessorPath = configurations.annotationProcessor
}
gneuvill
@gneuvill

Hi @jbgi ! Happy new year ! I saw you moved backed to Brittany so, welcome back I guess !

At work, we would need derive4j/derive4j-fj#7 to be merged and released (and the related functionaljava/functionaljava#392 and derive4j/hkt#34 prs).
I'm willing to do it if you're too busy but I don't know the procedure (and I guess I'd need some credentials somewhere - maven central ? jfrog ?) ; could you share some light on how to do this ?

Thanks !

Erik Dreyer
@edreyer
Hi @jbgi . I'm having an interesting problem trying to get the Derive4j to generate the desired code. The source class is this:
    @Data(@Derive(withVisibility = Visibility.Smart))
    interface Price {
        <R> R match(@FieldNames("price") Function<Integer, R> price);

        @ExportAsPublic
        static Either<ValidationError, Price> create(Integer price) {
            return createInt("price", price, 0, 1_000_000)
                .map(validPrice -> PriceADT.price0(validPrice));
        }

    }
In the above, PriceADT.price0(...) is an unknown method
Instead, a public PriceADT.price() method is generated
(I have another annotation that uses the generated class pattern {ClassNsame}ADT that isn't shown)
What's weird is that it was working fine for a while, then after I added some additional @Data classes, it stopped generating the protected factory methods and started producing the default public ones instead. I haven't been able to figure out what causes the change in annotation processor behavior, but I wonder if there's some sort of "order of operations" problem occurring
Any insights you can provide would be most appreciated.
Erik Dreyer
@edreyer
What's weirder, is that I have other very similar classes that ARE generating the proper protected factory methods
Erik Dreyer
@edreyer
I figured it out. If the ADT is defined inside another class, and is not public, the annotation processor doesn't behave the same way.
Erik Dreyer
@edreyer
Out of curiosity, is there a current recommendation for JSON serialization of derive4j annotated classes?
Jean-Baptiste Giraudeau
@jbgi
@edreyer I think many people use jackson but you have to make the implementation class non private (cf. derive4j/derive4j#68 for how to do that). I started a compile only json codec but never had time to finish. But I think @gneuvill has also worked on a solution that is more mature.
Also you are right that if the visitor is defined in another class then behavior is different, due to the two class possibly not being par of same compilation unit. Their is a way to mitigate this with -parameters switch of javac (cf. "To ensure smooth extensibility across compilation unit (or even during incremental compilation), it is best to use the -parameters option of javac" in readme).
@gneuvill sorry, I have been away from gitter and github issue for some times. thanks for your email and your help. I'm keen to proceed with your PRs, except I'm a bit confused regarding dependency between PR, as your PRs kind-of all depends on each others via SNAPSHOT. Could you work on a path that void this (since what is merged on master should not depends on SNAPSHOTs)?
gneuvill
@gneuvill
@edreyer Hey ! This (https://github.com/gneuvill/derive4j-jackson) is the solution @jbgi alluded to. I'm not sure it's more mature ; let's call it a working sketch that seems to basically do what it was made for : generating jackson serializers and deserializers for derive4j annotated types. It certainly need some more tests and polishing though !
gneuvill
@gneuvill

@jbgi

sorry, I have been away from gitter and github issue for some times

No pb !

Could you work on a path that avoid this (since what is merged on master should not depends on SNAPSHOTs)?

Ok, regarding functionaljava, I think a 4.9 release could be made right away (no change of code, just better compat with jpms) ; it is needed in derive4j-examples

hkt is good to go as is also, I think (that would be a 0.10.0) ; also needed in derive4j-examples

Regarding derive4j, you're right, I've made a mess. I just pushed some changes to fix that and the order of releases should be first derive4j (1.2.0) and second derive4j-fj (0.3).

Let me know if there is any remaining problems !

Erik Dreyer
@edreyer
@gneuvill Thank! I'll take a look
Erik Dreyer
@edreyer
@gneuvill Is there anything that needs to be done to get derive4j-jackson to work? I haven't tried it yet, but wondering what config is required beyond just adding the dependency.
gneuvill
@gneuvill
@edreyer you just need to declare your data type like so :
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;

@Data(@Derive(@Instances(JsonSerializer.class, JsonDeserializer.class)))
interface ADT {
... usual derive4j stuff ...
}
@jbgi Hi ! Did you have a look at my updates ? Is there still something missing to merge my PRs ?
Erik Dreyer
@edreyer
Thanks!
Erik Dreyer
@edreyer
Just published an article on ADTs that highlights Derive4J: https://cargurus.dev/Algebraic-Data-Types-for-java-devs/