Swift Repl: How to Save/Load the Repl State? (A.K.A. Suspend/Resume, Snapshot, Clone)

Can you save your Clojure REPL's state (or, effectivelly, can you program complex programs using REPL?)

most people work with the repl through an editor such ad Eclipse/Emacs/vim and that editor has the ability to save the repl, though without some diligence on the developers part this will likely be an incomplete record of what happened. Some of the state of the repl may have come from loading files etc which will be in a different state.

So the short answer is typically not.

Starting Swift REPL for iOS vs OSX

One way to do this is to create an iOS/OS X project and set a break point to interrupt the flow. Then, enter repl into the lldb console and enjoy.

For more on this one, watch WWDC 2014 session 409 - Introduction to LLDB and the Swift REPL.

repl

How can I mimic load-file behavior with use ?

special forms like use are well... special. you can't write functions that 'use' namespaces into the callers ns. But a simple macro can save you!

I did this in a project back before I had leiningen to load my repl properly

(defmacro load-all []
'(use
:reload-all
'com.cryptovide.modmath
'com.cryptovide.combine
'com.cryptovide.split
'com.cryptovide.encrypt
'com.cryptovide.misc
'com.cryptovide.decrypt
'com.cryptovide.modmathTest
'com.cryptovide.combineTest
'com.cryptovide.splitTest
'com.cryptovide.encryptTest
'com.cryptovide.miscTest
'com.cryptovide.decryptTest
'com.cryptovide.testlib
'com.cryptovide.gui
'com.cryptovide.checksum
'com.cryptovide.log))

When you call this, the reader runs the usees from the namespace that you call it in (for example it will use them into the repl's current ns

How can I mimic load-file behavior with use ?

special forms like use are well... special. you can't write functions that 'use' namespaces into the callers ns. But a simple macro can save you!

I did this in a project back before I had leiningen to load my repl properly

(defmacro load-all []
'(use
:reload-all
'com.cryptovide.modmath
'com.cryptovide.combine
'com.cryptovide.split
'com.cryptovide.encrypt
'com.cryptovide.misc
'com.cryptovide.decrypt
'com.cryptovide.modmathTest
'com.cryptovide.combineTest
'com.cryptovide.splitTest
'com.cryptovide.encryptTest
'com.cryptovide.miscTest
'com.cryptovide.decryptTest
'com.cryptovide.testlib
'com.cryptovide.gui
'com.cryptovide.checksum
'com.cryptovide.log))

When you call this, the reader runs the usees from the namespace that you call it in (for example it will use them into the repl's current ns

Swift compiler: Printing the AST of a Swift file with 3rd party imports

Looks like an alternative to printing the AST with the Swift compiler is to use SourceKit.

There's a CLI tool called SourceKitten which allows you to parse the structure of a Swift source file into JSON format, without needing to handle imports of 3rd party frameworks and other source files.

Input from the keyboard in command line application

I managed to figure it out without dropping down in to C:

My solution is as follows:

func input() -> String {
var keyboard = NSFileHandle.fileHandleWithStandardInput()
var inputData = keyboard.availableData
return NSString(data: inputData, encoding:NSUTF8StringEncoding)!
}

More recent versions of Xcode need an explicit typecast (works in Xcode 6.4):

func input() -> String {
var keyboard = NSFileHandle.fileHandleWithStandardInput()
var inputData = keyboard.availableData
return NSString(data: inputData, encoding:NSUTF8StringEncoding)! as String
}


Related Topics



Leave a reply



Submit