Skip to content

Conversation

anderseknert
Copy link
Member

@anderseknert anderseknert commented Jun 22, 2025

Use a single generic entrypoint for obtaining interned terms regardless of type.

Use a single generic entrypoint for obtaining interned
terms regardless of type.

Signed-off-by: Anders Eknert <[email protected]>
return booleanTrueTerm
// Interned returns a possibly interned term for the given scalar value.
// If the value is not interned, a new term is created for that value.
func InternedTerm[T internable](v T) *Term {
Copy link
Contributor

@srenatus srenatus Jun 23, 2025

Choose a reason for hiding this comment

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

💭 Makes you wonder why we shouldn't define

func Term(v Value) *Term {
  return InternedTerm(v)
}

...and make non-interned terms the exceptional case.

Copy link
Member Author

Choose a reason for hiding this comment

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

That is tempting indeed. But it'd require even more aggressive disclaimers, as it'd be too easy to reach for Term in places where that term later is mutated, typically by having a Location added at some later point in time.

My original plan was to have an interned package mirroring the relevant functions of the ast one, which would make the difference very clear between e.g. an ast.StringTerm and an interned.StringTerm — and the ideal "design" for the current ast.InternedTerm would IMHO be interned.Term. But the circular dependency check sadly prevents that as long as we want to use interned terms also in the ast package :/

I think there could be some ways around that worth exploring — and given our existing disclaimers about not using interning outside of the OPA project, I wouldn't feel bad about making further changes here later. This seems like a pretty good middle-ground for the time being though.

Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

Thanks!

@anderseknert anderseknert merged commit 78a5ca2 into open-policy-agent:main Jun 23, 2025
31 checks passed
@anderseknert anderseknert deleted the better-interning branch June 23, 2025 09: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.

2 participants