derive4j / hkt Goto Github PK
View Code? Open in Web Editor NEWHigher Kinded Type machinery for Java
License: BSD 3-Clause "New" or "Revised" License
Higher Kinded Type machinery for Java
License: BSD 3-Clause "New" or "Revised" License
Local classes are classes declared inside methods or value constructors : there seems to be no use case for lifting those to type constructors.
cf. https://github.com/scalaz/scalaz/blob/series/7.3.x/core/src/main/scala/scalaz/Leibniz.scala
It will probably be useful to represent the type equality between a type and its HK encoding.
Can wait after 1.0 release.
Not sure if this is the right place for this question.
But is there a way to easily handle using HKT for classes we can not extend/implement __, __2, etc. for. Without writing a bunch of wrapper classes.
For example, using HKT with Java 8's Stream class.
Including a presentation of use-cases, links to prior art and papers, projects that use this lib.
As discussed in #13 (comment).
Should open the way of aol/cyclops#228.
Ensure all imagined misusing of the hkt interfaces are catched by unit tests.
Since an annotation processor can be deactivated at compile time, it seems desirable to have at our disposal a fallback mechanism that would apply the very same logic as the one of the processor at runtime ; ideally, and in java terms, this should even happen at class loading time.
This would necessitates :
javax.lang.model
or java.lang.reflect
)static <A,Z> Try0<A,Z> asTry0(__<__<Try0.µ,A>,Z> hkt) {
return (Try0<A,Z>) hkt;
}
That doesn't compile. The Z
parameter is not within its bound : it should be Z extends Exception
.
Javadoc must be updated to reflect new valid encodings: ie. 1(a), 2 and 3 mentionned in #1 (comment)
release 0.9 ignore HK encoded classe that inherit from derive4j hkt interface through an alias interface.
Eg. nothing is generated for:
class HkTest<A> implements HigherKind1<HkTest<?>, A> {}
interface HigherKind1<TC extends HigherKind1<TC, ?>, T> extends __<TC, T> {}
(was testing how the encoding could be used in javaslang)
for now, this means disabling openjdk specific inspections when the api is not available,
Under Widows 10, I have the problem that the generated java files have the Windows default encoding, not UTF-8, which garbles the µ identifier in my case.
After trying every way to set an encoding in Maven or my IDE, I assume the annotation processor itself must take care of it. I think the relevant code location is GenCode:146
:
Filer.createSourceFile(genClassName).openWriter()
As far as I understand, openWriter()
simply wraps the underlying output stream with am OutputStreamWriter
without setting an encoding, which results in using the system default (and not the encoding set for javac
etc). I haven't tested it, but I would assume that using something like
new OutputStreamWriter(Filer.createSourceFile(genClassName).openOutputStream(), "UTF8")
could probably solve the problem. Of course instead of hard-coding, this could be also a parameter of the annotation processor (although I'm not sure in which scenarios would require another encoding)
This
__2<p, S, A> nasty() {
return new __2<p, S, A>() {};
}
compiles. It shouldn't.
That could be useful to enforce the use of a single representation of HKTs (i.e one of the three described here #1 (comment)) through a project
This means the _
classes at the root of https://github.com/DanielGronau/highj/tree/master/src
Should be renamed to __
or other name (hk
?) to avoid javac warning.
The annotation processor should check what @gneuvill described in functionaljava/functionaljava#126 (comment) to ensure acceptable type-safety.
Additionally it would also check that a narrow/coherce method exists with the right type signature.
@gneuvill Would you be up to the task?
Leibniz
is probably unnecessarily non-intuitive. In type theory this concept is usually more often refereed as "propositional equality" or "type equality" (as in "propositions as types").
I took the name from scalaz but thinking of it, the term "Leibniz equality" is maybe best left in the javadoc.
Also this would be more in line with Haskell built-in type equality (also it may be worth having a look at the combinators provided by this package).
@clinuxrulz, @gneuvill, WDYT? (originally, @clinuxrulz, I think you also used TypeEq
).
Alternative could be TypeEquals
(used in purescript).
Consider the Compose type from https://hackage.haskell.org/package/transformers-0.4.3.0/docs/Data-Functor-Compose.html
It is defined as newtype Compose f g a = Compose { getCompose :: f (g a) }
, and allows to treat nested types like "List of Maybe" as a single HKT.
I tried to translate it straight-forward to:
package org.highj.data.structural;
import org.derive4j.hkt.__;
public class Compose<F,G,A> implements __<Compose.µ<F,G>,A> {
public interface µ<F,G>{}
public Compose(__<F, __<G, A>> value) {
this.value = value;
}
private final __<F,__<G,A>> value;
public __<F, __<G, A>> get() {
return value;
}
}
However, HTK complains:
Error:(5, 8) java: org.derive4j.hkt.__ is not the correct interface to use. Given the number of type parameters, org.highj.data.structural.Compose should implements org.derive4j.hkt.__3<org.highj.data.structural.Compose.µ<F,G>, F, G, A>
.
Maybe I overlooked something, or there is another way to implement it, but I got stuck.
Because of the way java allow access of static method through instance those methods can be problematic because of potential conflicting "overrides".
No modifications of the processor is required.
public interface Try8<A, B, C, D, E, F, G, H, I, Z extends Exception>
extends __<__9<Try8.µ, A, B, C, D, E, F, G, H, I>, Z> {
enum µ {}
I f(A a, B b, C c, D d, E e, F f, G g, H h) throws Z;
}
This doesn't work. Got error: org.derive4j.hkt.__ is not the correct interface to use. Given the number of type parameters, fj.function.Try8 should extends org.derive4j.hkt.__10<µ, A, B, C, D, E, F, G, H, I, Z>
.
Obviously we don't have __10, but I thought the curried encoding would work...
Related to #14.
Let's assume we offer interfaces up to __n
(currently 5, soon 9).
There will be a day when someone would need to create a class with n + x
type parameters (x >= 1).
We cannot add new interface infinitely, so there must be a way to cope with this situation.
My take would be that if a class has more that 9 type parameters then the higher kinded interface will only expose the last 9 type parameters, and the first "non hkt" type parameters would need to be backed in the type constructor witness in one of the two following way:
class WildType<A, B, C, D, E, F> imlements __5<µ<A>, B, C, D, E, F> {
public static final class µ<A> {}
}
class WildType<A, B, C, D, E, F> imlements __5<__<µ, A>, B, C, D, E, F> {
public static final class µ {}
}
The later encoding is more consistent with the hierarchy and eventually allow to abstract over all type parameters, but the first one may be simpler for types with lots of type parameters.
We should check that a static 'narrow' method with correct signature (and implementation if possible) is present in the class (unless annotated @Data
).
The method could have another name. (or should it be normalized ?)
Hey guys,
I'd like to turn on this feature in cyclops-react (aol/cyclops#347), when I added the hkt project as a dependency and make cyclops-react Higher class extend org.derive4j.hkt.__ everything compiles just fine from the command line. Unfortunately, IntelliJ starts showing some strange errors instead.
Lombok's val will generally fail, but other errors seem unrelated and really strange. I've attached a screenshot where the line numbers of some spurious compile errors have been misattributed.
I'm not sure how easy or hard it would be to fix this (as the issue may well be on the IntelliJ side or Lomboks), but I thought you'd appreciate getting a report of it.
Thanks,
John
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.