-
Notifications
You must be signed in to change notification settings - Fork 79
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
Revisiting nil
semantics in Variant
and TypedArray
#576
Revisiting nil
semantics in Variant
and TypedArray
#576
Conversation
The Windows build is breaking due to a change in Arguments:
|
Oh, that's weird, probably an oversight during a merge-conflict resolution. |
Aha, green now |
Something weird happens to CI. |
This patch is catching a few mistakes in Godot for iPad, so that is very cool! Some problems that we currently face:
Some usability challengesI wonder if we could improve upon some of these. Many APIs in Godot of the form:
Are now a bit obnoxious to us, it used to be:
Now it needs to be:
I am scared of adding extension methods like "String(Variant?)" or variations because it would affect any place that passes a nil. But we could add it for some of the Godot-based types like "GDictionary", I have a patch here: I did cheat and on my large code base, I added those extensions, but I am afraid of unleashing this into the unsuspecting public. My current patch that I think is necessary: https://gist.github.com/migueldeicaza/afd7c892bd9faa908fe49c159ee29f81 |
I think something is broken on main branch. How do you feel about: extension Optional where Wrapped == Variant {
public func into<T: VariantStorable>(_ type: T.Type = T.self) -> T? {
if let self {
return T.init(self)
} else {
return nil
}
}
public func into<T: VariantStorable>(_ type: T.Type = T.self) -> T? where T: Object {
if let self {
return self.asObject()
} else {
return nil
}
}
}
func foo(_ variant: Variant?) {
if let string = variant.into(String.self) {
}
} |
Another observation: I think that we could annotate some APIs that we know would never pass a nil, like Node.getChildren and a handful of others, for usability purposes. |
This PR contains the following changes, all of them are related to
nil
-correctness.Variant
withgtype == .nil
should never surface in the user API. It will be always represented as Swiftnil
ofVariant?
. This allows direct integration of Swift nullability checks into Variant processing. We shouldn't have places where we haveVariant?.some
withGodot
nil
inside. This is accompanied with a lot of small code changes everywhere in the code to support this logic.Variant
will now beVariant?
.ObjectCollection
: TypedArray of objects can now work withnils
(just like it does in GDScript).Nil
builtin class is omitted from translation.Arguments
to streamline gracious failing when Godot calls are done into@Callable
or@Exported
with mismatching arguments. It shouldn't crash the game/editor anymore and print errors instead.MacroCallable
andMacroExported
for newer infrastructure and gracious failing if Godot calls into Swift-exported code with wrong arguments instead of crashing.