Replies: 4 comments 13 replies
-
I don't really see how lateinit has better semantics than a null! . Am I missing somrthing? Name is obviously more indicative of an intent but I wouldn't call it "semantics". If you simply want something similar name-wise you can introduce a helper along the lines of public static T LateInit<T>(string hint) => default(T);
class Foo() : ScriptObject
{
string bar = LateInit(nameof(this.Setup));
} Or something like that to give you a small hint where the thing is actually initialized. |
Beta Was this translation helpful? Give feedback.
-
In Kotlin you don’t have fields. What you see is actually a property. I am not familiar with this library, why doesn’t source generator work here? |
Beta Was this translation helpful? Give feedback.
-
After some more investigation I feel like I would try to have a custom |
Beta Was this translation helpful? Give feedback.
-
Why you don't use Lazy? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Related: #2452, #7081 (and maybe #3963)
Late initialization is an unavoidable topic especially when C# is used as a script language of some framework and engine, where some initial values cannot be set until some lifecycle (
Setup()
,Ready()
or whatsoever), or those values are set by external sources. The issue is that current NRT checking cannot recognize those patterns, and would give annoying warnings about them.new
ing an empty one as a placeholder isn't always ideal in these cases, as the properties or fields requiring late initialization may involve heavy computation.Currently, for properties I can, verbosely, have (
T : class
)But for fields, this isn't the case. The only thing I can do is
to make the NRT checking happy.
Also, in the specific framework I use (Godot), it use an attribute
[Export]
(and related source generation underlying it) to expose a value to the editor, which means that having a wrapper (likeLateInit<T>
) for that would break the source generation and thus is not a solution. On the other hand, having each property requiring late initialization written as above would be tedious and, in my opinion, is no better thannull!
compared to the verbosity it brings.Kotlin has
lateinit
which has better semantic meaning thannull!
. I wonder if C# could have something similar.Beta Was this translation helpful? Give feedback.
All reactions