28

In the Swift REPL, how to import (a.k.a. load, evaluate, require) a typical text *.swift file?

  • I want to use the code from this file: ~/src/Foo.swift

  • Syntax like this doesn't work: import ~/src/Foo.swift

For comparison:

  • An equivalent solution in the Swift REPL for a framework is: import Foundation

  • An equivalent solution in the Ruby REPL for a *.ruby file is: require "~/src/foo"

These are similar questions that are /not/ what I'm asking:

  • How to use/make a Swift command-line script, executable, module, etc.

  • How to use/make an XCode playground, project, library, framework, etc.

  • How to launch the REPL with a pre-existing list of files.

joelparkerhenderson
  • 34,808
  • 19
  • 98
  • 119

6 Answers6

6

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

rougeExciter
  • 7,435
  • 2
  • 21
  • 17
  • Thanks, I'm looking for a way to include .swift source code, from within the Swift REPL, and also without having any special launch. For comparison in Ruby we can read in source code and evaluate it like this: require '/foo/bar/mycode.rb' – joelparkerhenderson Mar 26 '16 at 16:06
  • Oh my mistake... sorry... and do you need to do this dynamically (i.e. loading a string and then 'requiring' that) or do you know the desired import files at "launch time"? – rougeExciter Mar 26 '16 at 16:26
  • Dynamically. Much like Ruby, Python, Perl, etc. let me require any arbitrary file path in the REPL at any time. – joelparkerhenderson Mar 26 '16 at 23:47
  • 1
    Then it's a clear no at this stage. Unless the file is compiled into a module that's then placed with the correct include and library paths. – rougeExciter Mar 26 '16 at 23:51
3

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.swift import headtest print("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.

AntiMoron
  • 1,286
  • 1
  • 11
  • 29
  • This is very helpful, thank you. I'll try your way until there's a positive answer to my question. Much obliged. – joelparkerhenderson Mar 26 '16 at 23:49
  • Could you write function that takes a path, compiles it, and imports it dynamically? Then just always include that function when you launch the repl? – DylanYoung Nov 21 '20 at 00:14
1

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

#!/usr/bin/env xcrun swift
Vatsal Manot
  • 17,695
  • 9
  • 44
  • 80
  • Thanks for the answer. What I'm looking for is a way to load the file's code into the REPL; I'm not seeking a way to make the file's code executable. I'll clarify my question. -Joel – joelparkerhenderson Jan 10 '15 at 06:38
  • Though overkill, you could build a dynamic library and load it into the REPL. – Vatsal Manot Jan 10 '15 at 06:41
  • Thanks Vatsal. Yes, that's a good idea and my fallback if no one here can come up with a solution that loads the plain text into the REPL. I'll clarify my question more. Thanks again! – joelparkerhenderson Jan 10 '15 at 06:57
  • I'm looking into this as we speak. But, if it's an option, consider building a proper framework (using Xcode), and then load it into the REPL. – Vatsal Manot Jan 10 '15 at 06:59
  • Might not have helped as a answer to this specific question but that is very neat to know about. Its a really nice option in some circumstances to be able to call all sorts of native code from a script this way. – jkp Mar 23 '21 at 11:58
1

You can copy/paste the source code into the repl and execute it. Not ideal, obviously, but sometimes useful.

Marchingband
  • 489
  • 1
  • 7
  • 14
1

Now Swift REPL supports packages. We can do this by the following steps:

  1. In Xcode, select menu File > New > Package. Choose a name for the package, for example MyLibrary.

  2. Copy your codes or .swift files to the Sources/MyLibrary/ directory in your package.

  3. Remember to make your interface public.

  4. In the command line, go to the package directory and run REPL

Like this

cd MyLibrary/
swift run --repl
  1. In the REPL, import your library

Like this

import MyLibrary

Now you can your codes in the REPL.

Cosyn
  • 4,404
  • 1
  • 33
  • 26
0

It looks like it's not possible to import file and get Xcode to use REPL for it using specifications you gave. I think you can still do it creating a proper framework, but it's not exactly what you was looking.

Hannes Karppila
  • 969
  • 2
  • 13
  • 31