如何使用宏在 Dotty 中生成一个类? [英] How to generate a class in Dotty with macro?

查看:22
本文介绍了如何使用宏在 Dotty 中生成一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Dotty、Scala 3 中使用宏生成新类?

Is it possible to generate a new class with macro in Dotty, Scala 3 ?

Zlaja

推荐答案

目前在 Dotty 中只有(种类)def.目前没有(某种)宏注释,它可以生成一个新成员,新班级等

Currently in Dotty there is only (kind of) def macros. Currently there is no (kind of) macro annotations, which could generate a new member, new class etc.

对于新成员、新类等的生成,您可以使用

For generation of a new member, new class etc. you can use

  • Scalameta (without or with SemanticDB, Scalafix [see also] depending on whether such generation is just syntactic or semantic), which works at the time before compile time (source generation), or

a 编译器插件,其中在编译时工作.

a compiler plugin, which works at compile time.

让我提醒您,即使在 Scalac 中,生成新成员、新类等的能力也不是从一开始就出现的.此类功能(宏注释)作为 Macro Paradise 编译器插件出现在 Scalac 中.

Let me remind you that even in Scalac the ability to generate a new member, new class etc. also appeared not from the very beginning. Such functionality (macro annotations) appeared as Macro Paradise compiler plugin to Scalac.

我不能排除某个时候有人会为 Dotty 编写类似 Macro Paradise 之类的东西.现在还为时过早,现在只是 Dotty 的功能冻结,甚至语言语法(例如) 和标准库现在不断变化(还有 列表正在测试其与 Dotty 一起工作的能力的库,例如目前没有 Scalaz/Cats.

I can't exclude that somewhen somebody will write something like Macro Paradise for Dotty. It's too early for that, it's only feature-freeze for Dotty now, even language syntax (for example) and standard library keep changing now (there is also list of libraries that are testing their ability to work with Dotty, for example currently no Scalaz/Cats are there).

这篇关于如何使用宏在 Dotty 中生成一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆