2.10 中 Scala REPL breakIf 的替代方案 [英] Alternative to Scala REPL breakIf in 2.10

查看:37
本文介绍了2.10 中 Scala REPL breakIf 的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这里关于使用breakIf 方法在 REPL 代码中用于交互式调试,但后来我发现了 thispostbreakbreakIf 在 Scala 2.10 中从 ILoop 中删除.不幸的是,该帖子没有解释为什么代码被删除.

我假设这些功能已被删除,因为有更好的方法来执行此操作.如果是这样的话,有人可以启发我吗?

解决方案

也许这个想法是你应该直接使用 ILoop ?据我所知,它不应该比以下复杂得多:

//在你想要 REPL 的任何地方插入下面的代码val repl = 新的 ILooprepl.settings = 新设置repl.in = SimpleReader()repl.createInterpreter()//绑定任何你想访问的局部变量repl.intp.bind("i", "Int", i)repl.intp.bind("e", "异常", e)//启动解释器,然后在你 :quit 后关闭它复制循环()repl.closeInterpreter()

与旧的 breakIf API 相比,这种方法消除了 if 条件(被包装到 => Boolean) 和 DebugParam/NamedParam(它们是仅用于填充 bind 参数的临时包装器).

这种方法还允许您根据需要指定Settings.例如,一些 REPL 错误可以通过 -Yrepl-sync 解决code> 但是 break 没有给你指定的方法.

I was reading here about using the breakIf method in the REPL code for interactive debugging, but then I found this post saying that break and breakIf were removed from ILoop in Scala 2.10. Unfortunately, that post doesn't explain why the code was removed.

I'm assuming that these functions were removed because there's a better way of doing this. If that's the case, could someone please enlighten me?

解决方案

Perhaps the idea is that you should just work with the ILoop directly? As far as I can tell, it shouldn't be much more complex than:

// insert the code below wherever you want a REPL
val repl = new ILoop
repl.settings = new Settings
repl.in = SimpleReader()
repl.createInterpreter()

// bind any local variables that you want to have access to
repl.intp.bind("i", "Int", i)
repl.intp.bind("e", "Exception", e)

// start the interpreter and then close it after you :quit
repl.loop()
repl.closeInterpreter()

Compared to the old breakIf API, this approach gets rid of an additional level of indirection for both the if condition (which was wrapped into a => Boolean) and the DebugParam/NamedParam (which were temporary wrappers used only to fill in the bind arguments).

This approach also allows you to specify your Settings as needed. For example, some REPL bugs can be worked around with -Yrepl-sync but break gave you no way of specifying that.

这篇关于2.10 中 Scala REPL breakIf 的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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