Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP for generic representations of ADTs #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions modules/core/src/main/scala/iota/TList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,17 @@ object TList {
macro internal.TypeListMacros.materializeTListCompute[L, O]
}

trait Gen[A] {
type Repr <: TList
}

object Gen {
type Aux[A, R <: TList] = Gen[A] { type Repr = R }

def apply[A](implicit ev: Gen[A]): Gen.Aux[A, ev.Repr] = ev
implicit def materializeGen[A, R <: TList]: Aux[A, R] =
macro internal.TypeListMacros.materializeTListGen[A, R]
}


}
13 changes: 13 additions & 0 deletions modules/core/src/main/scala/iota/gen/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package iota

package object gen {

type Index[I <: SingletonInt]
type Name[N <: SingletonString]

type Meta[
I <: SingletonInt,
N <: SingletonString
] = Index[I] with Name[N]

}
39 changes: 38 additions & 1 deletion modules/core/src/main/scala/iota/internal/TypeListMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package iota
package internal

import scala.reflect.macros.blackbox.Context
import scala.reflect.macros.whitebox.Context

private[iota] final class TypeListMacros(val c: Context) {
import c.universe._
Expand Down Expand Up @@ -86,4 +86,41 @@ private[iota] final class TypeListMacros(val c: Context) {
q"new _root_.iota.KList.Compute[$L]{ override type Out = $tpe }", true)
}

def materializeTListGen[A, R <: TList](
implicit
evA: c.WeakTypeTag[A]
): c.Expr[TList.Gen.Aux[A, R]] = {

val A = evA.tpe

val aSym = A.typeSymbol

def fieldType(symbol: MethodSymbol): Type =
internal.refinedType(List(
symbol.returnType,
appliedType(
weakTypeOf[iota.gen.Meta[_, _]], List(
internal.constantType(Constant(0)),
internal.constantType(Constant(symbol.name.encodedName.toString))))
), NoSymbol)

val tpe = if (aSym.isClass) {
val aClassSym = aSym.asClass
if (aClassSym.isCaseClass) {
val accessors = A.decls
.collect { case m: MethodSymbol if m.isCaseAccessor => m.asMethod }
.toList
tb.buildTList(accessors.map(fieldType))
} else if (aClassSym.isSealed) {
NoType
} else {
NoType
}
} else
NoType

tb.foldAbort(Right(
q"new _root_.iota.TList.Gen[$A] { override type Repr = $tpe }"))
}

}
1 change: 1 addition & 0 deletions modules/core/src/main/scala/iota/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ package object iota {
//#-2.11

private[iota] type SingletonInt = Int with Singleton
private[iota] type SingletonString = String with Singleton
}
35 changes: 35 additions & 0 deletions modules/examples/src/main/scala/iotaexamples/foo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2016-2017 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 iotaexamples

import iota._

import debug.options.ShowTrees

object Foo extends App {

case class Dummy(
a: String,
b: Double,
c: Long)

val gen = TList.Gen[Dummy]

scala.Predef.println(gen)


}