Skip to content

Commit c697a4e

Browse files
committed
Allow "classes" to be typed when assigned null
1 parent 5f81c58 commit c697a4e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/gdcompiler/subcompilers/TypeCompiler.hx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ class TypeCompiler {
2424
this.main = main;
2525
}
2626

27+
function isGodotClass(classType: ClassType): Bool {
28+
return switch(classType.meta.extractExpressionsFromFirstMeta(":bindings_api_type")) {
29+
case [macro "class"]: true;
30+
case _ if(classType.superClass != null): isGodotClass(classType.superClass.t.get());
31+
case _: false;
32+
}
33+
}
34+
2735
public function compileClassName(classType: ClassType): String {
2836
return classType.getNameOrNativeName();
2937
}
@@ -90,6 +98,21 @@ class TypeCompiler {
9098
}
9199

92100
if(t.isNull()) {
101+
// Primitives, Arrays, Dictionaries, and copy-types (Vector2, etc.) cannot be assigned `null`.
102+
// The only way to handle these is to remain "untyped" at the moment.
103+
//
104+
// Object types are generated with `@:bindings_api_type("class")`, so those are safe to
105+
// type and assign `null`.
106+
final unwrappedType = Context.followWithAbstracts(t.unwrapNullTypeOrSelf(), true);
107+
switch(unwrappedType) {
108+
case TInst(clsRef, _): {
109+
if(isGodotClass(clsRef.get())) {
110+
return compileType(unwrappedType, errorPos, isExport);
111+
}
112+
}
113+
case _:
114+
}
115+
93116
return null;
94117
}
95118
// Ignore Null<T> and just compile as T

0 commit comments

Comments
 (0)