Import swift file in script

You can already import modules from source in script mode today, using some not-really-documented flags. I discovered it digging through the interpreter test suites a while back:

other_module.swift

public func importedFunction() {
  print("Imported function was called!")
}

script.swift

#!/usr/bin/swift -frontend -interpret -enable-source-import -I.

import other_module  // this imports other_module.swift

importedFunction()

Note that you need to add the current directory (or whatever directory your "modules" live in) to the import path with -I.

$ chmod +x ./script.swift
$ ./script.swift  
Imported function was called!

So I guess the question becomes, is this something that should be elevated to a more supported/documented feature? It would certainly improve the state of Swift as an easy-to-use scripting language.

39 Likes