Swift REPL: how to import, load, evaluate, or require a .swift file? Swift REPL: how to import, load, evaluate, or require a .swift file? swift swift

Swift REPL: how to import, load, evaluate, or require a .swift file?


You need to use -I, so if your modulename.swiftmodule file is in ~/mymodules than launch swift with

swift -I ~/mymodules

and then you will be able to import your module

import module name

Should be that easy


In swift,you can't import a typical *.swift file.

For Import Declaration can only be the following syntax:

“ ‌ import-declaration → attributesopt import import-kindopt import-path
import-kind → typealias| struct| class| enum| protocol| var| func
‌ import-path → import-path-identifier| import-path-identifier.import-path
‌ import-path-identifier → identifier| operator”

From: Apple Inc. “The Swift Programming Language (Swift 2)”。 iBooks.

which can be described as these formats:

import module
import import kind module.symbol name
import module.submodule

import head.swift is incompatible with import import-kind module.symbol-name


Usually compile the files you want to import as a framework.Then it can be regarded as a module. use -F framework_directory/ to specify 3rd-party frameworks' search path.

  1. Create a file. For example:

    // test.swiftimport headtestprint("Hello World")

  2. open your terminal

  3. goto the directory where you create the file.
  4. execute command line

    swift -F headtest test.swift

And done.


Simply insert the shebang line at the top of your script:

#!/usr/bin/env xcrun swift