Java 8 annotation processor and framework for deriving algebraic data types constructors, pattern-matching, morphisms, (near future: optics and typeclasses)
jbgi on master
Improve Travis CI build Perform… Merge pull request #100 from Yu… (compare)
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))));
}
from
with target type to
T extends Something
).
hashCode
with toString().hashCode()
and equals
with Types.isSameType
.
dependencies {
annotationProcessor("org.derive4j:derive4j:1.1.1")
compileOnly("org.derive4j:derive4j:1.1.1")
}
tasks.withType(JavaCompile) {
options.annotationProcessorPath = configurations.annotationProcessor
}
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 !
@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));
}
}
PriceADT.price0(...)
is an unknown method
PriceADT.price()
method is generated
@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
-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).
@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 !
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
@Data(@Derive(@Instances(JsonSerializer.class, JsonDeserializer.class)))
interface ADT {
... usual derive4j stuff ...
}