This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
Suggestion: Use Proxies to detect higher-order types. #57
Labels
discussion
For direction and implementation discussions
enhancement
New feature or request
help wanted
Extra attention is needed
Aim: Discover higher-order types using proxies that wrap and monitor values at run-time.
Example
It is not possible to get the type of
f
at the call tofoo
, you have to monitorf
as it gets called. Proxies can be used to implement 'type catching`. The same method can be used for objects. While it is possible to traverse objects when you encounter them there are some issues: 1) they might be very large, 2) extra traversals can cause side-effects, 3) it wont catch any updates to the object inside the function. Proxies can be used to lazily monitor object access.Background I did some research that involved using Proxies to monitor JavaScript libraries with TypeScript definition files. The are some clear similarities with what we did and TypeWiz. The main difference is that we didn't care about collecting and outputting types, only checking that the library matched the definition. The same technique could be used to collect types in general.
Implementation Here is a gist that shows the basic idea (I hacked this together, consider yourself warned!). The part that concerns proxies:
When we want to collect a type, if we encounter an object or function, we replace them with a proxy that recursively collects types when called or accessed. The
register
callback is used to store the type information as we collect it (the full gist has more details).Issues: Proxies are known to change the semantics of programs, they have a different identity to the object they wrap: this can affect equality tests. This shouldn't be a significant problem in this context because TypeWiz is not being run in production, and the types should still be valid even if different control flow paths are taken.
Conclusion I hope this is at least interesting, even if it doesn't get taken on board. The project seems really interesting so I'll be keeping an eye out regardless.
All criticism and discussion welcome.
The text was updated successfully, but these errors were encountered: