Swift REPL:如何保存/加载 REPL 状态?(又名挂起/恢复、快照、克隆) [英] Swift REPL: how to save/load the REPL state? (a.k.a. suspend/resume, snapshot, clone)

查看:59
本文介绍了Swift REPL:如何保存/加载 REPL 状态?(又名挂起/恢复、快照、克隆)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swift REPL 中,有什么方法可以保留 REPL 状态?

In the Swift REPL, what is a way to preserve the REPL state?

  • 比如我想在 REPL 中做一堆工作,然后保存它,以便我稍后加载它.

  • For example, I want to do a bunch of work in the REPL, then save it, so I can load it later.

这个概念可能被命名为保存/加载、挂起/恢复、快照/克隆、序列化/反序列化等.

This concept might be named save/load, suspend/resume, snapshot/clone, serialize/deserialize, etc.

任何让我朝着这个方向前进的解决方案都会有所帮助,即使是这样的黑客:

Any solution that gets me toward this will help, even if it's a hack like these:

  • 记录所有历史行,然后在另一个 REPL 中重放它们.

  • Record all the history lines, then replay them in another REPL.

序列化所有对象,然后在另一个 REPL 中反序列化它们.

Serialize all the objects, then deserialize them in another REPL.

对 RAM 或 VM 进行快照,然后将其克隆到另一台机器上.

Snapshot the RAM or VM, then clone it to another machine.

保存全局状态的核心映像,稍后执行.

Save a core image of the global state, then execute it later.

我的目标是在一台机器上保存 REPL 工作,然后在另一台机器上加载它.

My goal is to save REPL work on one machine, then load it on another machine.

我只需要最终状态;我不需要堆栈、历史或句柄等.

I only need the final state; I don't need stacks, or history, or handles, etc.

XCode Playgrounds 有一个类似的功能,使用保存",将内容外化.

The XCode Playgrounds have a similar feature, using "Save", which externalizes content.

推荐答案

也许这对你有点帮助.

刚刚发现实际上 Swift REPL 将当前会话保存在一个文件中.

Just found out actually Swift REPL save current session in a file.

在 REPL 中输入 __FILE__,您将看到会话文件.

Type __FILE__ in REPL, you will see the session file.

  1> __FILE__
$R0: String = "/var/folders/6j/xs_0g88d55dgprjrwdws898w0000gn/T/lldb/3869/repl1.swift"

您可以查看文件内容,它会持续跟踪当前的 REPL 会话.我相信您可以构建一行 Swift 代码来将该文件复制到您的保存文件夹中,您必须在会话结束时运行该文件夹.

You can view the file content, it keeps tracking current REPL session. I am sure you can build an one line Swift code to copy that file into your save folder, which you have to run at end of your session.

顺便说一句,在那个临时文件夹中,repl.swift 实际上比 repl1.swift 更紧凑.您可能想要复制 repl.swift.

BTW, in that temp folder, the repl.swift is actually more compact than repl1.swift. You probably want to copy repl.swift.

这篇关于Swift REPL:如何保存/加载 REPL 状态?(又名挂起/恢复、快照、克隆)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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