I just took the Kotlin badge test on LinkedIn. It ...
# announcements
s
I just took the Kotlin badge test on LinkedIn. It doesn’t tell me the answers so I don’t know for sure. There are a LOT of small wording problems in the quiz where it’s not clear if they are trying to gotcha you or not. i.e. They show you a function and call it
shouldEqual
, but the correct answer calls the function
shouldMatch
. There were lots of small little mistakes in the correct answers that make you question whether or not it’s the correct answer.
K 2
Also why does
Delegates.notNull()
exist when there is
lateinit
?
e
I haven't seen that test, but Delegates.notNull() existed before lateinit, and still has some uses: for example, primitives can't be lateinit, but can be Delegates.notNull()
plus one 1
s
The question was something like “If you want to have a variable that is set later, but can’t be null, what should you do?“. I wasn’t sure if
Delegates.notNull()
was a thing, and based on their previous mistakes I questioned whether they were referring to lateinit as a delegate. 😛 Obviously I missed that one.
Is lateinit just compiling down to the raw variable access with some inlined checks before the access?
oh - thats what’s happening, only they create functions instead of inlining the checks:
Copy code
public static String test;

   @NotNull
   public static final String getTest() {
      String var10000 = test;
      if (var10000 == null) {
         Intrinsics.throwUninitializedPropertyAccessException("test");
      }

      return var10000;
   }

   public static final void setTest(@NotNull String var0) {
      Intrinsics.checkNotNullParameter(var0, "<set-?>");
      test = var0;
   }
e
another important thing you can see with the lateinit var: it exposes the raw field so that Java dependency injection libraries can access it
btw, if it's a private member, it is raw field accesses
s
You have a lot of specialized lateinit knowledge. 😄 Why do you know all this off the top of your head?
e
regarding private raw field access, that happens for all properties, not just lateinit
regarding DI/backing field, that was part of the original design. https://youtrack.jetbrains.com/issue/KT-9067
s
I guess I knew that about properties too. I just haven’t thought about it for a few years. I also haven’t used that method of DI for 4 or so years now either.