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

6

29

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.

Hierarchize answered 10/1, 2015 at 3:30 Comment(0)
L
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

Ladylike answered 26/3, 2016 at 12:18 Comment(4)
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'Hierarchize
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"?Ladylike
Dynamically. Much like Ruby, Python, Perl, etc. let me require any arbitrary file path in the REPL at any time.Hierarchize
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.Ladylike
A
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.

Ambitious answered 12/1, 2016 at 4:49 Comment(2)
This is very helpful, thank you. I'll try your way until there's a positive answer to my question. Much obliged.Hierarchize
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?Tummy
C
2

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.

Cottontail answered 17/2, 2022 at 8:7 Comment(0)
M
1

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

#!/usr/bin/env xcrun swift
Monroemonroy answered 10/1, 2015 at 5:30 Comment(5)
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. -JoelHierarchize
Though overkill, you could build a dynamic library and load it into the REPL.Monroemonroy
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!Hierarchize
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.Monroemonroy
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.Insistence
G
1

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

Gurl answered 2/3, 2018 at 18:50 Comment(0)
B
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.

Boo answered 17/1, 2015 at 17:22 Comment(1)
Thanks, I'm looking for an answer using the specifications I gave in the question.Hierarchize

© 2022 - 2024 — McMap. All rights reserved.