Skip to content

SPICE-0020: Deferred, type-safe references#1354

Open
HT154 wants to merge 2 commits into
apple:mainfrom
HT154:reference-t
Open

SPICE-0020: Deferred, type-safe references#1354
HT154 wants to merge 2 commits into
apple:mainfrom
HT154:reference-t

Conversation

@HT154
Copy link
Copy Markdown
Contributor

@HT154 HT154 commented Dec 6, 2025

SPICE: apple/pkl-evolution#27

Resolves #912

@HT154 HT154 force-pushed the reference-t branch 4 times, most recently from 6bd5bfc to 14a7c24 Compare December 6, 2025 08:13
@HT154 HT154 changed the title Add Reference<T> SPICE-0020: Reference<T> Jan 8, 2026
@HT154 HT154 force-pushed the reference-t branch 4 times, most recently from d7663a8 to 09ac8d7 Compare February 9, 2026 17:53
@HT154 HT154 force-pushed the reference-t branch 9 times, most recently from c61ce90 to 0085962 Compare April 17, 2026 00:01
@HT154 HT154 force-pushed the reference-t branch 4 times, most recently from ce06670 to 98a918f Compare May 8, 2026 16:51
@HT154 HT154 changed the title SPICE-0020: Reference<T> SPICE-0020: Deferred, type-safe references May 14, 2026
@HT154 HT154 marked this pull request as ready for review May 15, 2026 19:10
Copy link
Copy Markdown
Member

@bioball bioball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a first pass!

Comment thread stdlib/ref.pkl Outdated
Comment thread stdlib/ref.pkl Outdated
}

/// Represents a property or subscript access as part of a [Reference].
external class Access {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] "AccessPart" feels like a better name for this.

Also, I don't think this needs to be an external class; it's helpful if users can new up these values and play around with them.

Suggested change
external class Access {
class AccessPart {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm not convinced re: naming. Each pkl.ref#Access corresponds to a qualified access or subscript [access].

Comment thread docs/modules/bindings-specification/pages/binary-encoding.adoc Outdated
Comment thread pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java Outdated
|`<value>`
|Root value
|link:{uri-messagepack-array}[array]
|Array of link:{uri-stdlib-ReferenceAccess}[property access] values
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also missing domain.

Food for thought: should this also include the referent type? This might be really useful for the language binding use-case. If we do, I'm not totally sure how that would work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were to include this, it would mean adding another slot to the table (not a big deal). It would also probably mean encoding the referent as an array of Class and TypeAlias (stdlib U?Int\d+ types only) values. Maybe a future enhancement?

Comment thread stdlib/ref.pkl Outdated
external property: String(key == null)?

/// If the access is access via subscript.
external isSubscript: Boolean
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a binary condition; it's either property or entry access. Do we need two booleans?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for ease/fluency of use: if a user is writing e.g. a function that transforms a Reference to a String, they may need to know whether each access is a subscript key or property; it may be necessary to check one way or the other and access.isSubscript is more meaningful than !access.isProperty (and vice versa).

Another alternative here is something like this:

type: "property" | "subscript"

But that makes usage sites pretty verbose!

Curious if you have thoughts or alternative suggestions here.

Comment thread stdlib/ref.pkl
Comment thread stdlib/ref.pkl Outdated
Comment thread stdlib/ref.pkl Outdated
Comment thread stdlib/ref.pkl
///
/// Limitations:
/// * Type constraints are ignored (erased) when referenced.
/// * Properties marked `external` may not be referenced.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should explain how to use it, complete with some examples.

We tend to provide really long doc comments when the use-case is advanced. For example, the doc comments within pkl:Project are quite long.

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.

Reference<T>: Type safe "deferred" references

3 participants