Skip to content

Add typeof - #4037

Open
toots wants to merge 180 commits into
mainfrom
typeof
Open

Add typeof#4037
toots wants to merge 180 commits into
mainfrom
typeof

Conversation

@toots

@toots toots commented Jul 18, 2024

Copy link
Copy Markdown
Member

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

toots commented Jul 21, 2024

Copy link
Copy Markdown
Member Author

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

gAlleb commented Jul 21, 2024

Copy link
Copy Markdown
Contributor

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

@toots
toots changed the base branch from main to typeof-cleanup January 3, 2025 16:08
Base automatically changed from typeof-cleanup to main January 5, 2025 16:12
@toots
toots force-pushed the typeof branch 2 times, most recently from 399b705 to 17415ca Compare March 3, 2026 00:38
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.

7 participants