In a language that allows defining types in-language, how to store type computation results? #2009
Replies: 1 comment 2 replies
-
Hey @haukepribnow, Ideally, you would not eagerly compute the types of your documents. Instead, you can compute the types on the fly when they're needed. See as a larger example how it's implemented in the langium-lox example (without caching though). I'll think about creating a PR that adds caching soon, when I have some time this week.
I believe the documentation is a bit outdated there. However, what we often do is that we use the
The builtin caches are generally very useful when it comes to building (and caching) type system data. In our production grade projects, we usually build our type systems on a large |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working on a language in which the user can define their own types. Now I wonder: At what point should I run the type inference and what is the "right" place to store the type inference information that is most in line with the Langium architecture?
At this point, I'm primarily interested in making the inferred types available to the
ScopeProvider
so that it can provide a scope containing the members of a defined type in an expression. For example, my language supports the dot notation for accessing members like inperson.city.name.length
. Here, I would like to for example enable theScopeProvider
to provide the members ofperson
's type or ofperson.city
's type etc..With regards to implementing a type system in Langium, the documentation available on https://langium.org/docs/reference/document-lifecycle/ tells me:
So this does answer the "what point" part of my question already. But just to go sure, I would love to get feedback whether I got the documentation right. Here's how I interpreted the documentation:
I feel this might work. But is there any advise that I should consider before I commit to this approach?
Moving on to the "where to store" question:
I haven't found any documentation that explains how a program that uses Langium is supposed to "attach" information to AST nodes for later use. The only documentation that deals with storing data while processing is the one about caches.
Because using a cache for the use case of storing type data feels not quite right, I looked into Langium's codebase and explored the default implementations for some of the services. I found that the
DefaultIndexManager
has its ownMap
that stores the relationship between strings and AST nodes. To mirror that approach, I could also add a similarMap
as a field to myHfunct02TypeComputer
for storing the type of each AST node. I would have thisMap
get reset whenever the registeredonBuildPhase
callback fires. However, I wonder if the usage ofonBuildPhase
would suffice: Will the build phase be restarted also after a document got removed from the workspace? And will the callback therefore be called also in this case? (Because if it does not get fired in this case, then myMap
would contain incorrect information.)But taking a step back: Is this an approach that is really in line with Langium's architecture? Or does the architecture encourage users to store type information somewhere/somehow else?
Beta Was this translation helpful? Give feedback.
All reactions