pkdawson / imgui-godot

Dear ImGui plugin for Godot 4
MIT License
492 stars 28 forks source link

Modify the code to compile without warrnings with "Nullable reference types: Enabled" #39

Closed Muchaszewski closed 1 year ago

Muchaszewski commented 1 year ago

I like my projects to run with Nullable reference types: Enabled to allow me as a developer to notice obvious issues with nullable types.

Fixing 3-4 issues with the with lack of ? on some types, it shows 35 warrnings. I would love to have this fixed.

It doesn't break anything to use this in your project. It's just ignored when someone has this Disabled (as it is by default)

Example of the warning:

Project\addons\imgui-godot\ImGuiGodot\Internal\Fonts.cs(15,25): warning CS8618: Non-nullable property 'Font' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. 
pkdawson commented 1 year ago

I've thought about this, there are some difficulties because of how you frequently need to initialize member variables in _EnterTree or _Ready instead of a constructor. I remember seeing some solutions for this discussed in the Chickensoft Discord months ago, I'll see if I can dig that up.

Muchaszewski commented 1 year ago

You can mark all of those as nullable, and initialize them with null! to remove the warning like so:

FontFile Font? {get; init; } = null!;

but usually the ? is enough, and it doesn't require any further modifications.

I will see if I can do a PR for this tomorrow :)

pkdawson commented 1 year ago

Yeah, there's one or two things I might want to redesign, but for now I'm just suppressing most warnings with null!.

Don't worry about a PR, I'll push my fixes today.

pkdawson commented 1 year ago

Thanks for the report, I'll keep Nullable enabled in the project so I don't break it in the future.

Released in v4.0.4