Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Free-Tagless-Module Annotations: Change code These are some small changes to the implementation of the macro annotations `@tagless` and `@free`, as well as their `@module`. * We extract a shared `Clait` class, which represents the data from either a `Trait` or a `Class` in Scalameta. * We extract from the four annotations the logic to extract that `Clait` from the annotated source code. * We move to the `Clait` class the logic to generate or pick the kind-1 parametrer `F[_]`. * We extract a class of `ErrorMessages`, used almost identically in all macros. * We add some logic to the syntax `Ops` classes in `ScalametaUtil`.
- Loading branch information
Showing
9 changed files
with
387 additions
and
477 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
modules/core/shared/src/main/scala/freestyle/free/internal/Clait.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2017-2018 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package freestyle.free.internal | ||
|
||
import scala.collection.immutable.Seq | ||
import scala.meta._ | ||
import scala.meta.Defn.{Class, Trait, Object} | ||
|
||
/* A Clait in this context generalises the shared elements of a Class or a Trait | ||
*/ | ||
case class Clait( | ||
mods: Seq[Mod], | ||
name: Type.Name, | ||
tparams: Seq[Type.Param], | ||
ctor: Ctor.Primary, | ||
templ: Template | ||
) { | ||
|
||
import ScalametaUtil._ | ||
|
||
def toTrait: Trait = Trait(mods.filtered, name, tparams, ctor, templ) | ||
def toClass: Class = Class(mods.filtered, name, tparams, ctor, templ) | ||
|
||
val allTParams: Seq[Type.Param] = tparams.toList match { | ||
case headParam :: tail if headParam.isKind1 => tparams | ||
case _ => Type.fresh("FF$").paramK :: tparams.toList | ||
} | ||
val allTNames: Seq[Type.Name] = allTParams.map(_.toName) | ||
val headTParam: Type.Param = allTParams.head | ||
val headTName: Type.Name = allTNames.head | ||
val tailTParams: Seq[Type.Param] = allTParams.tail | ||
val tailTNames: Seq[Type.Name] = tailTParams.map(_.toName) | ||
|
||
|
||
def applyDef: Defn.Def = { | ||
val ev = Term.fresh("ev$") | ||
q"def apply[..$allTParams](implicit $ev: $name[..$allTNames]): $name[..$allTNames] = $ev" | ||
} | ||
|
||
} | ||
|
||
object Clait { | ||
|
||
def apply(cls: Class): Clait = Clait(cls.mods, cls.name, cls.tparams, cls.ctor, cls.templ) | ||
def apply(cls: Trait): Clait = Clait(cls.mods, cls.name, cls.tparams, cls.ctor, cls.templ) | ||
|
||
def parse(annotation: String, defn: Any): (Clait, Boolean) = { | ||
val errors = new ErrorMessages(annotation) | ||
import errors._ | ||
import ScalametaUtil.isAbstract | ||
|
||
defn match { | ||
case cls: Trait => (Clait(cls), true) | ||
case cls: Class if isAbstract(cls) => (Clait(cls), false) | ||
case c: Class /* ! isAbstract */ => abort(s"$invalid in ${c.name}. $abstractOnly") | ||
case Term.Block(Seq(_, c: Object)) => abort(s"$invalid in ${c.name}. $noCompanion") | ||
case _ => abort(s"$unexpected. $abstractOnly") | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
modules/core/shared/src/main/scala/freestyle/free/internal/Errors.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2017-2018 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package freestyle.free.internal | ||
|
||
class ErrorMessages(annotation: String){ | ||
|
||
// Messages of error | ||
val invalid = s"Invalid use of `$annotation`" | ||
val abstractOnly = s"The `$annotation` annotation can only be applied to a trait or an abstract class." | ||
val noCompanion = s"The trait or class annotated with `$annotation` must have no companion object." | ||
val onlyReqs = s"In a `$annotation`-annotated trait (or class), all abstract method declarations should be of type FS[_]" | ||
val nonEmpty = s"A `$annotation`-annotated trait or class must have at least one abstract method of type `FS[_]`" | ||
val unexpected = s"Unexpected tree encountered for the `$annotation` annotation." | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.