dmytromitin / auxify Goto Github PK
View Code? Open in Web Editor NEWIntroduces macro/meta annotations @ aux, @ self, @ instance, @ apply, @ delegated, @ syntax and String-based type class LabelledGeneric
License: Apache License 2.0
Introduces macro/meta annotations @ aux, @ self, @ instance, @ apply, @ delegated, @ syntax and String-based type class LabelledGeneric
License: Apache License 2.0
.
https://scalacenter.github.io/scalafix/docs/users/installation.html
Scalafix | Scala Compiler | Scalameta
0.9.5 | 2.11.12 / 2.12.8 | 4.1.9
0.5.10 | 2.11.12 / 2.12.4 | 2.1.7
import com.github.dmytromitin.auxify.macros.{aux, instance}
/*@aux*/ @instance
trait Compare[T1, T2] {
type T
type Out = (T, T)
def apply(a: T1, b: T2): Out
}
object Compare {
type Aux[T1, T2, T0] = Compare[T1, T2] { type T = T0 }
// def instance[T1, T2, T0](f: (T1, T2) => (T0, T0)): Aux[T1, T2, T0] = new Compare[T1, T2] {
// override type T = T0
// override def apply(a: T1, b: T2): Out = f(a, b)
// }
implicit def reverseCompare[T1, T2](implicit ev: T2 => T1): Aux[T1, T2, T1] = instance((a, b) => (a, ev(b)))
implicit def directCompare[T1, T2](implicit ev: T1 => T2): Aux[T1, T2, T2] = instance((a, b) => (ev(a), b))
}
def compare[T1, T2](a: T1, b: T2)(implicit cmp: Compare[T1, T2]) = (compareImpl[cmp.T] _).tupled(cmp(a, b))
def compareImpl[T](a: T, b: T) = a == b
case class Foo(s: String)
case class Bar(s: String)
implicit def foo2bar(f: Foo): Bar = Bar(f.s)
compare(Foo("hello"), Bar("hello"))
Warning:scalac: {
abstract trait Compare[T1, T2] extends scala.AnyRef {
type T;
type Out = scala.Tuple2[T, T];
def apply(a: T1, b: T2): Out
};
object Compare extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
def instance[T1, T2, T0$macro$1](f$macro$2: _root_.scala.Function2[T1, T2, Out]): Compare[T1, T2] {
type T = T0$macro$1
} = {
final class $anon extends Compare[T1, T2] {
def <init>() = {
super.<init>();
()
};
override type T = T0$macro$1;
type Out = scala.Tuple2[T, T];
override def apply(a: T1, b: T2): Out = f$macro$2(a, b)
};
new $anon()
};
type Aux[T1, T2, T0] = Compare[T1, T2] {
type T = T0
};
implicit def reverseCompare[T1, T2](implicit ev: _root_.scala.Function1[T2, T1]): Aux[T1, T2, T1] = instance(((a, b) => scala.Tuple2(a, ev(b))));
implicit def directCompare[T1, T2](implicit ev: _root_.scala.Function1[T1, T2]): Aux[T1, T2, T2] = instance(((a, b) => scala.Tuple2(ev(a), b)))
};
()
}
Error:(11, 13) not found: type Out
/*@aux*/ @instance
What about objects?
According to commit history, last version is 0.9
but it seems last published version is 0.8
.
My build is failing because of...
[error] (core / update) sbt.librarymanagement.ResolveException: Error downloading org.typelevel:macro-compat_2.13.5:1.1.1
[error] Not found
[error] not found: /Users/alex/.ivy2/local/org.typelevel/macro-compat_2.13.5/1.1.1/ivys/ivy.xml
[error] not found: https://repo1.maven.org/maven2/org/typelevel/macro-compat_2.13.5/1.1.1/macro-compat_2.13.5-1.1.1.pom
... and it seems version 0.9
fixes this so it will be awesome to have it published ๐ธ .
Thanks in advance!
subj
Think whether for
trait B { self: C =>
}
type Self
should be
type Self >: self.type <: B with C { type This = self.This }
incorrectly handles bounds U20$macro$5[X >: Null] <: U1[X]
/*@aux*/ @instance /*@delegated*/
trait FlattenAndDeduplicate[C <: Coproduct] extends DepFn1[C]{
type Out <: Coproduct
// def apply(t: C): Out
}
object FlattenAndDeduplicate {
type Aux[C <: Coproduct, Out0 <: Coproduct] = FlattenAndDeduplicate[C] { type Out = Out0 }
// def instance[C <: Coproduct, Out0 <: Coproduct](f: C => Out0): Aux[C, Out0] = new FlattenAndDeduplicate[C] {
// override type Out = Out0
// override def apply(t: C): Out0 = f(t)
// }
object identityPoly extends Poly1 {
implicit def identityCase[A]: Case.Aux[A, A] = at(identity)
}
implicit def mkFlattenAndDeduplicate[C <: Coproduct, C_flatten <: Coproduct](implicit
flatMap: FlatMap.Aux[C, identityPoly.type, C_flatten],
dedup: Deduplicate[C_flatten],
): Aux[C, dedup.Out] = instance(c => dedup(flatMap(c)))
}
Error:(53, 39) no arguments allowed for nullary method instance: ()App165.FlattenAndDeduplicate[C]{type Out = Out0$macro$1}
): Aux[C, dedup.Out] = instance(c => dedup(flatMap(c)))
scalaVersion := "2.12.8"
libraryDependencies += "com.github.dmytromitin" %% "auxify-macros" % "0.6"
//addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full)
import com.github.dmytromitin.auxify.macros.{aux, instance}
@aux @instance
trait Compare[T1, T2] {
type T
type Out = (T, T)
def apply(a: T1, b: T2): Out
}
For macros it's impossible but for meta?
looses variance for class B[+T >: Null <: AnyRef : Ordering, ... -_[+_ <: AnyRef] <: AnyRef]
... type U4[+_ <: AnyRef] <: AnyRef
type Aux[T >: Null <: AnyRef ...
, tparam$meta$9[+_ <: AnyRef]...
type U4[tparam$meta$8 <: AnyRef] = U40$meta$6[tparam$meta$8
what to do with view and context bounds?
import com.github.dmytromitin.auxify.shapeless.stringToSymbol
import shapeless.lens
case class Test(id: String, calc: Long)
lens[Test] >> stringToSymbol("id")
Error: Expression {
import shapeless.syntax.singleton._;
com.github.dmytromitin.auxify.shapeless.`package`.stringToSymbolHlp[String("id"), Symbol @@ String("id")](SingletonOps.instance[String("id")](Witness.mkWitness[String("id")]("id".asInstanceOf[String("id")])).narrow)(shapeless.this.StringToSymbol.mkStringToSymbol[String("id")](Witness.mkWitness[String("id")]("id".asInstanceOf[String("id")])), Witness.mkWitness[Symbol with shapeless.tag.Tagged[String("id")]](scala.Symbol.apply("id").asInstanceOf[Symbol @@ String("id")].asInstanceOf[Symbol with shapeless.tag.Tagged[String("id")]]))
} does not evaluate to a constant or a stable reference value
val len1 = lens[Test] >> stringToSymbol("id")
https://stackoverflow.com/questions/50418767/shapeless-lenses-usage-with-a-string-definition/
add flag
https://stackoverflow.com/questions/62747743/in-scala-2-13-how-to-use-implicitlypath-dependent-type
Implement implicitSupertypeOf
, implicitSubtypeOf
(implicitly
), implicitExactTypeOf
, implicitStrictSupertypeOf
, implicitStrictSubtypeOf
.
? Implement existImplicit
, localImplicit
/nonLocalImplicit
, allImplicits
, secondImplicit
.
https://stackoverflow.com/questions/50526509/using-string-instead-of-symbol-in-labelledgeneric-etc
import shapeless.Witness
import shapeless.labelled.FieldType
import shapeless.syntax.singleton._
def syFldToStrFld[St <: String, Sy <: Symbol, T](syFld : FieldType[Sy,T])(implicit syWit : Witness.Aux[Sy]) : FieldType[St,T] =
(syWit.value.name : String) ->> (syFld : T)
[error] /home/travis/build/DmytroMitin/AUXify/shapeless/src/test/scala/com/github/dmytromitin/auxify/shapeless/FieldTypeTest.scala:10:23:
Expression (syWit.value.name: String) does not evaluate to a constant or a stable reference value
[error] (syWit.value.name : String) ->> (syFld : T)
[error] ^
[error] /home/travis/build/DmytroMitin/AUXify/shapeless/src/test/scala/com/github/dmytromitin/auxify/shapeless/FieldTypeTest.scala:10:33:
value ->> is not a member of String
[error] (syWit.value.name : String) ->> (syFld : T)
[error]
https://travis-ci.org/github/DmytroMitin/AUXify/jobs/703170252
https://travis-ci.org/github/DmytroMitin/AUXify/jobs/695723076
[error] /home/travis/build/DmytroMitin/AUXify/shapeless/src/test/scala/com/github/dmytromitin/auxify/shapeless/StringSymbolTest.scala:27:46: could not find implicit value for parameter witness: shapeless.Witness.Aux[S1]
[error] sameTyped[Witness.`"a"`.T](symbolToString('a))("a".narrow) should be (())
[error] ^
[error] one error found
Transforms
@poly
trait MyTypeclass[In] {
type Out
def foo(in: In): Out
}
into
trait MyTypeclass[In] {
type Out
def foo(in: In): Out
}
object MyTypeclass {
object myTypeclassPoly extends Poly1 {
implicit def cse(implicit mtc: MyTypeclass[In]): Case.Aux[In, mtc.Out] = at(in => mtc.foo(in))
}
}
.
Warning:scalac: {
abstract trait Monoid[A] extends scala.AnyRef {
def empty: A;
def combine(a: A, a1: A): A
};
object Monoid extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
def instance[A](f$macro$1: A, f$macro$2: _root_.scala.Function2[A, A, A]): Monoid[A] = {
final class $anon extends Monoid[A] {
def <init>() = {
super.<init>();
()
};
override def empty: A = f$macro$1;
override def combine(a: A, a1: A): A = f$macro$2(a, a1)
};
new $anon()
};
implicit val int: Monoid[Int] = instance(0, ((x$1, x$2) => x$1.$plus(x$2)));
implicit val str: Monoid[String] = instance("", ((x$3, x$4) => x$3.$plus(x$4)))
};
()
}
Block(List(ClassDef(Modifiers(ABSTRACT | INTERFACE | DEFAULTPARAM/TRAIT), TypeName("Monoid"), List(TypeDef(Modifiers(PARAM), TypeName("A"), List(), TypeBoundsTree(EmptyTree, EmptyTree))), Template(List(Select(Ident(scala), TypeName("AnyRef"))), noSelfType, List(DefDef(Modifiers(DEFERRED), TermName("empty"), List(), List(), Ident(TypeName("A")), EmptyTree), DefDef(Modifiers(DEFERRED), TermName("combine"), List(), List(List(ValDef(Modifiers(PARAM), TermName("a"), Ident(TypeName("A")), EmptyTree), ValDef(Modifiers(PARAM), TermName("a1"), Ident(TypeName("A")), EmptyTree))), Ident(TypeName("A")), EmptyTree)))), ModuleDef(Modifiers(), TermName("Monoid"), Template(List(Select(Ident(scala), TypeName("AnyRef"))), noSelfType, List(DefDef(Modifiers(), termNames.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(pendingSuperCall), Literal(Constant(())))), DefDef(Modifiers(), TermName("instance"), List(TypeDef(Modifiers(PARAM), TypeName("A"), List(), TypeBoundsTree(EmptyTree, EmptyTree))), List(List(ValDef(Modifiers(PARAM), TermName("f$macro$1"), Ident(TypeName("A")), EmptyTree), ValDef(Modifiers(PARAM), TermName("f$macro$2"), AppliedTypeTree(Select(Select(Ident(termNames.ROOTPKG), scala), TypeName("Function2")), List(Ident(TypeName("A")), Ident(TypeName("A")), Ident(TypeName("A")))), EmptyTree))), CompoundTypeTree(Template(List(AppliedTypeTree(Ident(TypeName("Monoid")), List(Ident(TypeName("A"))))), noSelfType, List())), Block(List(ClassDef(Modifiers(FINAL), TypeName("$anon"), List(), Template(List(AppliedTypeTree(Ident(TypeName("Monoid")), List(Ident(TypeName("A"))))), noSelfType, List(DefDef(Modifiers(), termNames.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(pendingSuperCall), Literal(Constant(())))), DefDef(Modifiers(OVERRIDE), TermName("empty"), List(), List(), Ident(TypeName("A")), Ident(TermName("f$macro$1"))), DefDef(Modifiers(OVERRIDE), TermName("combine"), List(), List(List(ValDef(Modifiers(PARAM), TermName("a"), Ident(TypeName("A")), EmptyTree), ValDef(Modifiers(PARAM), TermName("a1"), Ident(TypeName("A")), EmptyTree))), Ident(TypeName("A")), Apply(Ident(TermName("f$macro$2")), List(Ident(TermName("a")), Ident(TermName("a1"))))))))), Apply(Select(New(Ident(TypeName("$anon"))), termNames.CONSTRUCTOR), List()))), ValDef(Modifiers(IMPLICIT), TermName("int"), AppliedTypeTree(Ident(TypeName("Monoid")), List(Ident(TypeName("Int")))), Apply(Ident(TermName("instance")), List(Literal(Constant(0)), Function(List(ValDef(Modifiers(PARAM | SYNTHETIC), TermName("x$1"), TypeTree(), EmptyTree), ValDef(Modifiers(PARAM | SYNTHETIC), TermName("x$2"), TypeTree(), EmptyTree)), Apply(Select(Ident(TermName("x$1")), TermName("$plus")), List(Ident(TermName("x$2")))))))), ValDef(Modifiers(IMPLICIT), TermName("str"), AppliedTypeTree(Ident(TypeName("Monoid")), List(Ident(TypeName("String")))), Apply(Ident(TermName("instance")), List(Literal(Constant("")), Function(List(ValDef(Modifiers(PARAM | SYNTHETIC), TermName("x$3"), TypeTree(), EmptyTree), ValDef(Modifiers(PARAM | SYNTHETIC), TermName("x$4"), TypeTree(), EmptyTree)), Apply(Select(Ident(TermName("x$3")), TermName("$plus")), List(Ident(TermName("x$4")))))))))))), Literal(Constant(())))
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.