Skip to content

Unify Spore factory methods - #37

Open
jspenger wants to merge 1 commit into
phaller:mainfrom
jspenger:spore-apply-factory
Open

Unify Spore factory methods#37
jspenger wants to merge 1 commit into
phaller:mainfrom
jspenger:spore-apply-factory

Conversation

@jspenger

@jspenger jspenger commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

This PR unifies the Spore factory methods.

Before, the factory methods consisted of: Spore.apply(body), Spore.applyWithEnv(env)(body), Spore.applyWithCtx(env)(body).

These are replaced by Spore(captures...)(body), where captures is either an explicit list of the captured variables, or the symbol * for capturing all variables.

Examples:

A Spore with no captures

Spore() { (x: Int) => x + 12 }

A Spore with explicit captures

val y = 12
val z = 13
Spore(y, z) { (x: Int) => x + y + z }

A Spore with * capture all mode

val y = 12
val z = 13
Spore(*) { (x: Int) => x + y + z }

Invalid capturing causes a compiler error.

val y = 12
val z = 13
Spore(y) { (x: Int) => x + y + z }
//                             ^
// Error: Invalid capture of variable `z`.
// Add it to the capture list or use `*` to capture all by default.

@jspenger

Copy link
Copy Markdown
Contributor Author

Future improvement:
The API now consists of a single method. However, the new method's signature does not reflect what happens after macro expansion. For example, the signature hides using/implicit clauses. One option is to provide overloaded aliases with explicit signatures. Three cases would be useful: the case of an empty capture list; the case of one explicit capture and its evidence; and the case of the * capture all mode.

- Replace existing Spore factory methods: `Spore.apply(body)`, `Spore.applyWithEnv(env)(body)`, `Spore.applyWithCtx(env)(body)` with new uniform method: `Spore.apply(captures)(body)`, where `captures` is either an explicit list of the captured variables, or the symbol `*` for capturing all variables
- Example: `val y = 12; val z = 13; Spore(y, z) { (x: Int) => x + y + z }`
- Example: `Spore(*) { (x: Int) => x + y + z }`
- Update all test cases and samples to use new factory method(s)
- Update macros and refactoring
- Add inlined factory-alias tests
@jspenger
jspenger force-pushed the spore-apply-factory branch from 6ea4bd6 to e3d9a1f Compare April 15, 2026 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant