CARVIEW |
Select Language
HTTP/2 302
date: Wed, 23 Jul 2025 14:26:15 GMT
content-type: text/html; charset=utf-8
content-length: 0
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
access-control-allow-origin:
location: https://raw.githubusercontent.com/ThoughtWorksInc/import.scala/master/README.md
cache-control: no-cache
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
x-github-request-id: 8C6C:1A99C5:D3BB2B:FA6ACA:6880F107
HTTP/2 200
cache-control: max-age=300
content-security-policy: default-src 'none'; style-src 'unsafe-inline'; sandbox
content-type: text/plain; charset=utf-8
etag: W/"fe9ecfa0e1b47711b51dc56f6dac37559978d9665a1d2af2dceb30aa3195b275"
strict-transport-security: max-age=31536000
x-content-type-options: nosniff
x-frame-options: deny
x-xss-protection: 1; mode=block
x-github-request-id: 616A:181DD:15764F:30EEFC:6880F108
content-encoding: gzip
accept-ranges: bytes
date: Wed, 23 Jul 2025 14:26:16 GMT
via: 1.1 varnish
x-served-by: cache-bom-vanm7210043-BOM
x-cache: MISS
x-cache-hits: 0
x-timer: S1753280776.048802,VS0,VE267
vary: Authorization,Accept-Encoding
access-control-allow-origin: *
cross-origin-resource-policy: cross-origin
x-fastly-request-id: 304c5af2d5fb9aeff92e2fc2a0822edd097f7238
expires: Wed, 23 Jul 2025 14:31:16 GMT
source-age: 0
content-length: 2158
# Import.scala
[](https://travis-ci.org/ThoughtWorksInc/Import.scala)
**Import.scala** is a Scala compiler plugin that enables magic imports.
## Setup
```sbt
addCompilerPlugin("com.thoughtworks.import" %% "import" % "latest.release")
```
## Magic Imports
This plugin provides a set of magic imports that let you load additional
code into a Scala source file: these are imports which start with a `$`.
The syntax is similar to [magic imports in Ammonite](https://www.lihaoyi.com/Ammonite/#MagicImports).
### `import $file`
This lets you load code snippets into current source file. For
example given a small script defining one value we want
```scala
// MyScript.sc
val elite = 31337
```
We can load it into our current source file using:
```scala
import $file.MyScript
assert(MyScript.elite == 31337)
```
If the script is in a sub-folder, simply use:
```scala
import $file.myfolder.MyScript
```
Or if the script is in an *outer* folder,
```scala
import $file.`..`.MyScript
```
Or if you want to import the contents of the script in one go:
```scala
import $file.MyScript, MyScript._
assert(elite == 31337)
```
Or if you want to download the script from a website:
```scala
import $file.`https://gist.github.com/Atry/5dcb1414b804fd7679393cacac3c89fc/raw/5b1748ab6b45c00be0109686fdb25e85cde11ce0/include-example.sc`
assert(`https://gist.github.com/Atry/5dcb1414b804fd7679393cacac3c89fc/raw/5b1748ab6b45c00be0109686fdb25e85cde11ce0/include-example.sc`.i == 42)
```
Or if you prefer using dot as the path separator:
```scala
import $file.`https://gist.github.com`.Atry.`5dcb1414b804fd7679393cacac3c89fc`.raw.`5b1748ab6b45c00be0109686fdb25e85cde11ce0`.`include-example`
assert(`include-example`.i == 42)
```
While this is a trivial example, your `MyScript.sc` file can
contain anything you want, not just `val`s: function
`def`s, `class`es `object`s or
`trait`s, or `import`s from *other* scripts.
#### Cannot directly import from inside a Script
You cannot import things from "inside" that script in
one chain:
```scala
import $file.MyScript._
```
Rather, you must always import the script-object first, and then import
things from the script object after:
```scala
import $file.MyScript, MyScript._
```
#### Renamed-scripts and multiple-scripts
You can re-name scripts on-import if you find their names are
colliding:
```scala
import $file.{MyScript => FooBarScript}, FooBarScript._
```
Or re-name a URL:
```scala
import $file.{ `https://gist.github.com/Atry/5dcb1414b804fd7679393cacac3c89fc/raw/5b1748ab6b45c00be0109686fdb25e85cde11ce0/include-example.sc` => Renamed}
assert(Renamed.i == 42)
```
Or import multiple scripts at once
```scala
import $file.{MyScript, MyOtherScript}
```
These behave as you would expect imports to work. Note that when
importing multiple scripts, you have to name them explicitly and
cannot use wildcard `._` imports:
```scala
import $file._ // doesn't work
```
### `import $url`
`import $url` is an alias to `import $file`.
### `import $exec`
This is similar to `import $file`, except that it dumps the definitions and imports from the script into your current source file.
This is useful if you are using a script to hold a set of common imports.
```scala
import $exec.MyScript
assert(elite == 31337)
```
```scala
import $exec.`https://gist.github.com/Atry/5dcb1414b804fd7679393cacac3c89fc/raw/5b1748ab6b45c00be0109686fdb25e85cde11ce0/include-example.sc`
assert(i == 42)
```
## Acknowledgements
The examples in this README.md file is based on Li Haoyi's [Ammonite document](https://www.lihaoyi.com/Ammonite/#MagicImports) and copyright licensed under MIT. See the [Markdown source](https://github.com/ThoughtWorksInc/import.scala/raw/master/README.md).
