【问题标题】:Type macro are dead : how can i compute a type from a definition?类型宏已死:我如何从定义中计算类型?
【发布时间】:2013-10-23 08:48:41
【问题描述】:

类型宏已关闭。

但是,我有两个需要它们的重要用例。结果是我的应用程序的可扩展性严重丧失。 两者都是给定其他类型的类型的动态编译时生成。 基本上我想做类似的事情(显然不是scala代码,但我想你会明白的):

type T[U] = macro usecase1[U]

def usecase1[U]= U match {  
  case t if (t <:< Int) => String
  case ... => ...
}

第二个用例是:

type Remaining[A, B >: A] = macro ...

例如在哪里

class C
trait T1 extends C
trait T2 extends C

type Remaining[C with T1 with T2, T2] is assigned to "C with T1" at compile time
(so the macro would have generated the subclass list, and generated a new type from the list without T2) 

我没有用宏来做这件事,所以这是假设。我计划现在就这样做..直到我看到那个类型的宏已经死了。 无论如何,有没有人知道获得这些功能的诀窍? 谢谢

【问题讨论】:

  • 在您的第一个示例中,您不显示 用例,而是显示宏定义。如果没有实际的用例(需要T[U]),就很难评估。不过,它看起来,因为您在这里不需要宏,而只需要一些开箱即用的类型约束。
  • scala 中的类型非常灵活。请参阅 scalaz 和 shapeless (github.com/milessabin/shapeless‎) 了解高级类型用法。
  • @0__ 第二个用例是执行类似 link 的操作,但是有复杂的选项,例如“如果我插入选项 A,我无法插入 B;插入 C 等后 D 变为可插入”。我最终得到了类似链接中的内容,但是,我没有使用带有模式大小写的简短可读代码,而是最终详尽地编写了跨子类的所有树。第一个用例是自动转换。再一次,我可以用一个简单的算法来做到这一点,但我必须将所有案例写在不同的类中。
  • @Arseniy Zhizhelev 似乎没有做我想做的事(或者我不理解他们)。我知道scala中有两个不同的世界。已识别元素的世界和类型的世界。您可以在已识别元素的世界中编码,但只能在类型的世界中进行统计声明。我的梦想是在类型的世界中编码(仅使用类型),但这似乎是一个死梦..
  • @coo-lhobou Scala 中的类型级编程 (apocalisp.wordpress.com/2010/06/08/…) 文章表明类型系统非常强大,几乎可以进行通用编程!我看不到你想要达到的目标。你能解释一下没有宏的问题吗?

标签: scala types compilation scala-macros dynamic-compilation


【解决方案1】:

据我所知,第一个用例在某种程度上确实可以通过隐式实现。下面是一个示例:

scala> trait Bound[A] {
     |   type Type
     | }
defined trait Bound

scala> implicit val bound1 = new Bound[Int] { type Type = String }
bound1: Bound[Int]{type Type = String}

scala> implicit val bound2 = new Bound[String] { type Type = Double }
bound2: Bound[String]{type Type = Double}

scala> val tpe = implicitly[Bound[Int]]
tpe: Bound[Int] = $anon$1@2a6b3a99

scala> type U = tpe.Type
defined type alias U

然后:

scala> implicitly[U =:= String]
<console>:19: error: Cannot prove that U =:= String.
              implicitly[U =:= String]
                        ^

另一方面:

scala> implicitly[bound1.Type =:= String]
res0: =:=[bound1.Type,String] = <function1>

隐式解析似乎正在丢失一些类型。不知道为什么,以及如何解决这个问题。


对于第二个用例,HLists 立即浮现在脑海。比如:

scala> trait Remaining[A <: HList, B] { type Result = Remove[A, B] }
defined trait Remaining

scala> new Remaining[C :: T1 :: T2 :: HNil, T2] {}
res5: Remaining[shapeless.::[C,shapeless.::[T1,shapeless.::[T2,shapeless.HNil]]],T2] = $anon$1@3072e54b

不确定如何将生成的HList 组合成复合类型。类似(伪代码):

trait Remaining[A <: HList, B] {
  def produceType(
    implicit ev0 : Remove.Aux[A, B, C],
             ev1 : IsCons.Aux[C, H, T],
             ev2 : LeftFolder[T, H, (T1, T2) => T1 with T2]) = ev2
             //                     ^ Not real syntax, type-level function to combine/mix types
  val tpe = produceType
  type Result = tpe.Out
}

【讨论】:

  • 谢谢。我没有使用 scala 编译器的权限,但我猜 type U = tpe.type#Type 不会丢失信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多