Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typeof #4037

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Add typeof #4037

wants to merge 11 commits into from

Conversation

toots
Copy link
Member

@toots toots commented Jul 18, 2024

This PR adds a typeof <value> type annotation.

It has been proposed and scrapped several times but, recently, a pretty valid use-case popped up: #3760 (comment)

The idea being that json parsing is driven by the type but sometimes, describing a complex type is complicated and also hard to mainain.

Instead, with this PR, it is possible to simply use your value and then ask to parse according to its type.

For instance, if you are storing data in a ref and wants to fetch the latest saved value, you can do:

data = ref([])

try
  let json.parse (savedValue: typeof data()) = file.contents("/path/to/saved/data.json")
  data := savedValue
catch _ do
  ()
end

# Now use `data` and add value to it etc..

Technical notes

The typeof v type annotation has to be converted to a type. It is done via a lazy value. The term referred to is kept in the closure of the lazy value and typechecked when the type is forced.

typeof type values are stored in the same facility as type variables. This is a bit hacky but it makes it safer as they can be dereferenced just like regular type variables, thus being part of the existing type API flow without much required change.

@toots toots requested a review from smimram July 19, 2024 16:21
@toots toots marked this pull request as ready for review July 19, 2024 16:22
@toots
Copy link
Member Author

toots commented Jul 21, 2024

I'm gonna wait for @smimram's opinion on this. There's to rush to merge but I think that it would be a great user-friendly feature.

@gAlleb
Copy link
Contributor

gAlleb commented Jul 21, 2024

It would be a great user-friendly feature indeed) Thanks!

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