Scala DSL、对象和中缀表示法

发布于 2024-09-02 01:06:44 字数 243 浏览 10 评论 0原文

在 Scala 中,如果我想实现 DSL,有没有办法执行以下操作:

我有一个名为“Draw”的对象,其中包含函数 def draw(d:Drawable)

我该如何制作这样我就可以导入对象并在对象外部调用它,例如:

draw ball

如果球扩展了可绘制特征?问题是我想以一种中缀表示法使用绘制,但我不想通过表示它正在实现类/对象来限定函数绘制。

in Scala, if I want to implement a DSL, is there a way to do the following:

I have an Object called "Draw" which contains the function def draw(d:Drawable)

how can I make it so that I can import the Object and call it outside the object like:

draw ball

if ball extends the Drawable trait? The problem is that I want to use draw in a kind of infix notation, but I dont want to qualify the function draw by denoting it's implementing class/object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫﹏色ふ单纯 2024-09-09 01:06:44

你做不到。除了四个前缀运算符之外,在任何运算符表示法中,第一个标记都表示对象。

You can't do it. Aside from four prefix operators, in any operator notation the first token represents the object.

杀手六號 2024-09-09 01:06:44

我很快就尝试了一下,但使用一个对象完全可以使其工作。在那里,我必须使用绘制(球)而不是绘制球,如您所愿:


Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).

标量>特质 Drawable{def doSomething} 定义的特征 Drawable

scala>对象绘制 {
def draw(d:Drawable) = d.doSomething } 定义模块Draw

scala> val ball = new Drawable{def doSomething = println("做球")} 球:java.lang.Object with Drawable = $anon$1@3a4ba4d6

scala>导入绘图._ 导入Draw._scala

>抽球 :11: 错误: 对象 Draw 中缺少方法绘制的参数; 如果您想将其视为部分应用函数,请在此方法后面加上“_” 抽球 ^

标量>画(球) 做球

然而,通过将 Draw 定义为一个类,它确实起作用了:


scala> trait Drawable{def doSomething: Unit}
defined trait Drawable

标量>绘图类 {
def draw(d:Drawable) = d.doSomething } 定义类Draw

scala>

标量> val ball = new Drawable{def doSomething = println("做球")} 球:java.lang.Object with Drawable = $anon$1@36a06816

scala> val d = 新抽奖 d: Draw = Draw@7194f467

scala> d 画球 做球

我不完全确定为什么这与对象的工作方式不同,可能是一个错误,或者可能是指定的行为。不过我现在没有时间去查。

I quickly tried it out, but could quite make it work using an object. There I had to use draw(ball) instead of draw ball, as you wanted:


Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).

scala> trait Drawable{def doSomething} defined trait Drawable

scala> object Draw {
def draw(d:Drawable) = d.doSomething } defined module Draw

scala> val ball = new Drawable{def doSomething = println("doing ball")} ball: java.lang.Object with Drawable = $anon$1@3a4ba4d6

scala> import Draw._ import Draw._

scala> draw ball :11: error: missing arguments for method draw in object Draw; follow this method with `_' if you want to treat it as a partially applied function draw ball ^

scala> draw(ball) doing ball

However by defining Draw as a class, it did work:


scala> trait Drawable{def doSomething: Unit}
defined trait Drawable

scala> class Draw {
def draw(d:Drawable) = d.doSomething } defined class Draw

scala>

scala> val ball = new Drawable{def doSomething = println("doing ball")} ball: java.lang.Object with Drawable = $anon$1@36a06816

scala> val d = new Draw d: Draw = Draw@7194f467

scala> d draw ball doing ball

I'm not completely sure why this doesn't work the same way with an object, might be a bug or perhaps that's specified behaviour. However I didn't have the time to look it up at the moment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文