Skip to content

Add default keyword for generating a default value of any type#2683

Merged
mergify[bot] merged 5 commits intomainfrom
feature/byorgey/default
Feb 2, 2026
Merged

Add default keyword for generating a default value of any type#2683
mergify[bot] merged 5 commits intomainfrom
feature/byorgey/default

Conversation

@byorgey
Copy link
Member

@byorgey byorgey commented Feb 1, 2026

default must be applied to a type literal, i.e. default @Int or default @(Text -> Int) or whatever. It can also be used with type synonyms, as in tydef X = [... record type ...] end; default @X. It is replaced with a default value at compile time, so default never has to be interpreted at run time. Making it generate a default value at run time would be more flexible (it would allow e.g. higher-order uses) but getting access to a relevant context of type synonyms at runtime is tricky, so I figured static compile-time default value generation would be simpler.

default should support all inhabited types, including recursive types that have finite values.

Currently, default requires no special capability to use, but I could be convinced otherwise.

Closes #2192.

github-actions bot and others added 3 commits February 1, 2026 16:03
Copy link
Member

@xsebek xsebek left a comment

Choose a reason for hiding this comment

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

Nice 👍 I was surprised it tried the right branch as well.

-- (rec l. (Int * l) + Unit). Since we default to generating the left
-- side of a sum type, this will recurse forever generating inl (0,
-- inl (0, inl (0, ...))). To support recursive types we would have
-- to do more sophisticated analysis to find a finite value to pick.
Copy link
Member

Choose a reason for hiding this comment

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

Would a set of encountered types (?) in recursion do the trick? 🤔

It seems the only possible problem are loops and we should be able to detect them if types have Ord.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, good idea. Your comment was just what I needed to see how to make it work. It wasn't all that hard in the end, so I added it. 😄

@byorgey byorgey added the merge me Trigger the merge process of the Pull request. label Feb 2, 2026
@mergify mergify bot added the queued label Feb 2, 2026
mergify bot added a commit that referenced this pull request Feb 2, 2026
@mergify mergify bot merged commit 9fe1ee5 into main Feb 2, 2026
15 checks passed
@mergify
Copy link
Contributor

mergify bot commented Feb 2, 2026

Merge Queue Status

✅ The pull request has been merged at 31f923b

This pull request spent 14 seconds in the queue, including 2 seconds running CI.
The checks were run on draft #2685.

Required conditions to merge

@mergify mergify bot deleted the feature/byorgey/default branch February 2, 2026 18:05
@mergify mergify bot removed the queued label Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge me Trigger the merge process of the Pull request.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add default keyword for generating a default value of any type

2 participants