Skip to content

Commit f62124e

Browse files
Document the export { ... } statement (#321)
Updates the Imports page (renamed Imports and Exports) with an Exports section covering: opt-in semantics, syntax mirroring selective import, additive statements, re-exporting imports, and the order-dependent / no-rename / no-star rules. Pairs with malloydata/malloy#2837.
1 parent 6976de0 commit f62124e

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

src/documentation/language/imports.malloynb

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
>>>markdown
2-
# Imports
2+
# Imports and Exports
33

44
In order to reuse or extend a source from another file, you can include all the
55
exported sources from another file using `import "path/to/some/file.malloy"`.
@@ -31,4 +31,57 @@ The default is to import all objects from the referenced file. You can also use
3131
import { airports, spaceports is airports } from "airports.malloy"
3232

3333
run: airports -> { aggregate: airport_count is count() }
34-
run: spaceports -> { aggregate: spaceport_count is count() }
34+
run: spaceports -> { aggregate: spaceport_count is count() }
35+
>>>markdown
36+
37+
## Exports
38+
39+
By default, every `source:`, `query:`, `given:`, and `type:` declared in a file is exported — that is, visible to anyone who imports the file. If you'd rather keep some definitions local (for example, helper sources used only as scaffolding for the things you actually publish), add an `export { ... }` statement listing only the names you want to expose:
40+
41+
```malloy
42+
source: raw_flights is duckdb.table('flights.parquet')
43+
44+
source: flights is raw_flights extend {
45+
measure: flight_count is count()
46+
}
47+
48+
export { flights }
49+
```
50+
51+
After this, `flights` is exported but `raw_flights` is not. `raw_flights` is still usable inside the file — only its visibility from other files is affected.
52+
53+
The syntax intentionally mirrors selective import:
54+
55+
```malloy
56+
import { flights } from "flights.malloy"
57+
export { flights }
58+
```
59+
60+
`export` is opt-in. A file with no `export` statement behaves exactly as before: everything declared is exported.
61+
62+
Multiple `export` statements add together:
63+
64+
```malloy
65+
source: a is duckdb.table('a.parquet')
66+
export { a }
67+
68+
source: b is duckdb.table('b.parquet')
69+
export { b }
70+
// exports are now { a, b }
71+
```
72+
73+
You can re-export an imported name. This is the natural way to build a curated library on top of another:
74+
75+
```malloy
76+
import { customers is raw_customers } from "raw.malloy"
77+
source: orders is duckdb.table('orders.parquet')
78+
79+
export { customers, orders }
80+
```
81+
82+
### Rules
83+
84+
- An `export` statement must appear after the definition of each name it lists. Forward references are an error.
85+
- Only sources, queries, givens, and user types can be exported. Connections cannot.
86+
- There is no rename on export — rename at the definition site if you need a different public name.
87+
- There is no `export *`. The way to "export everything" is to leave the `export` statement out entirely.

src/table_of_contents.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
"link": "/language/sql_sources.malloynb"
193193
},
194194
{
195-
"title": "Imports",
195+
"title": "Imports and Exports",
196196
"link": "/language/imports.malloynb"
197197
},
198198
{

0 commit comments

Comments
 (0)