-
Notifications
You must be signed in to change notification settings - Fork 21
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
About Add Union Types #5
Comments
Hi and welcome! Here are my thoughts on this: Converting I think we can use For Also it might be easier to treat For function parameters I think we should use the same LLVM type as for return types, probably easier that way. Then we don't have to think about merging different instantiations of the same functions. When you have something working done, feel free to make a WIP pull request, so maybe I can give more concrete feedback on the implementation. |
In fact, I am more worried about another thing: for example: type T = { a:number, b: string} | {b: string, c: number}
declare const t: T;
if ('c' in t) {
// ...
} else {
// ...
} what is the expected memory layout for that? |
I think the memory layout would ideally be: %ab = type { double, %string }
%bc = type { %string, double }
%T = type { i8, i8* } When the first member is active, the But we can also initially implement it as Regarding I think ts-llvm will not allow more properties than is written in the type, so e.g. objects of type When However I don't think we need to support the |
Yes, that is the normally union type implement,but the ts are Of course we could limit some syntax such as all types for a union must have same members layout.
Actually,nearly all type guard have the same problem :XD |
@emlai For function parameters i think you are right, no need to consider about merging different instantiations of the same functions. Then, for
Then, how can I know this fact that if first elem is 0, If want to implemented like C, we need an extra layer middle-ir or high-ir, which as an intermediary to tell us how to merge different type to So, i suggest that when transform ts to llvm, as an initial implementation, we must save all types of information as much as possible for example
Marking the variable when
The Then we can use directly api such as After that, according to the actual effect to optimize by other intermediate implementations. Minimize the complexity and code coupling we need to face in the implementation process. Thinks :) |
I think we can get that information from the TypeScript's AST and type-checker: we ask it what My For your example where the type of a variable changes over time, e.g.: var one = undefined;
...
one = foo(); I don't know if the TypeScript compiler API allows getting the "full" type of the variable i.e. var one: undefined | string = undefined;
...
one = foo(); I personally don't see a need for a middle-IR or high-IR yet based on these examples.. |
Hi, i want to add union type. However, i rarely use js, so some concepts which i understand may be error.
I will describe my thoughts about union type and if you approve it, then i will commit pr when done.
Example
ts code
converted to llvm-ir
so, the first i32 means the real return type's index, and then 0 means undefined.
Thus, we can use
type { i32, [ 0 x i8 ] }
to indicateundefined
and usei32* null
to indicatenull
in ts.When union type is used on the parameter, i only get a certain type from parameter because function emitted when called, the type of parameter passed in at this time is ok. In this way, function will be emitted for each combination of parameter types. This can cause serious code bloat problem. Must do something to merge these function.
The text was updated successfully, but these errors were encountered: