Implement duck typing #63
-
|
Hey! I continue my exploration of Typir by trying to understand how the class type works. typir/examples/lox/src/language/lox-type-checking.ts Lines 219 to 235 in 8a04e85 If I correctly understand, here:
For the first two, I see that you can specify Here are two scenarios I want to implement using, I believe, what it looks like duck typing: In this first scenario, I have two entities and one instance of entity B. I want to automatically infer the type of the value of In this second scenario, I want to check if B overrides all the attributes of A, and trigger an error otherwise. Honestly, there are perhaps several mixed questions in this post and I'm sorry about that, this is the first time I've tried to implement a type checker in a "non-naive" way! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Hello @theogiraudet, the short answer is, that Typir does not yet support duck typing (I call it "structurally typed classes"), but it is on our roadmap (#45). Some more detailed answers:
yes
yes
yes, we could call it "member access" as well
Yes, that is intended for cases, where declarations are used as values, like That is the current state and understanding. However, I am reworking the API for inference rules at the moment regarding ...
With other words: Breaking changes will occur here in following versions of Typir.
Your understanding is great! And I should add more comments here (and yes, we plan to provide some rough documentation for v0.2 as well 🙂):
Looking at the written program is often not enough to see, whether classes are nominally or structurally typed. Usually that is defined by the semantics of the language. As far as I understood your scenario 1, you could do the type checking also with nominally typed classes. But yes, in both cases you want to show a validation hint to the user, that In my eyes, you could validate the scenario 2 even without a type system, since you need to collect the properties of sub and super class and check, whether the sub class has a property with the same name of the super class. Or do I miss something? If you want to ensure, that the type of a sub-property is the same or a sub-type of the type of the super-property, than a type system is very helpful. Should Typir provide a default validation for checking the conformance of sub- and super-classes? I am not sure, how different the rules for sub-classes and their properties are in practise in different languages and how much logic is reusable here. |
Beta Was this translation helpful? Give feedback.
Hello @theogiraudet,
the short answer is, that Typir does not yet support duck typing (I call it "structurally typed classes"), but it is on our roadmap (#45).
Some more detailed answers:
yes
yes
yes, we could call it "member access" as well
Yes, that is intended for cases, whe…