Write an article about kotlin's type system, hope ...
# feed
j
Write an article about kotlin's type system, hope you'll like it. https://medium.com/@jintin/the-beauty-of-kotlin-type-system-25d0a187b130
K 3
❤️ 1
s
so far, so good. • in the section about Nothing you could add: ◦ that Notling is used for dead code analysis. The Java compiler has to hard-code all situations that can lead to dead code (e.g. after return or throw). In Kotlin every statement aften an expression • How about adding a section how
Unit
is better than
void
, e.g. because in Java you need two interfaces Runnable and Callable while in Kotlin Runnaable is made redundant by Callable<Unit> (in Java Callable<void> is not a thing). Of course this is a specialisation about how how every functional interface that returns something can also be used in a way that returns nothing in
k
@Stephan Schröder Well, there is
Callable<Void>
. Admittedly,
Void
is slightly different from
void
.
s
I know that there is uppercase
Void
, I just never used it -or seen it used- in my close to quarter century of using Java. How do you construct an instance of it? But as you said, it's not
void
, so Kotlin's type-sytem is indeed smoother in this respect.
k
How do you construct an instance of it?
You don't. The Java API documentation says "The
Void
class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void."
But as you said, it's not
void
, so Kotlin's type-sytem is indeed smoother in this respect.
Kotlin's
Unit
is not
void
either.
Unit
is an object. From Java's point of view,
Unit
is a class which has only one instance,
Unit.INSTANCE
. This is analogous to
Void
, which is a class that has no instances, so any Java method that returns a
Void
type can only return
null
, just as any Kotlin function that returns a
Unit
type can only return the instance
Unit
.
1
Also, the
Void
type is used in a few Java API methods; for example,
CompletableFuture.allOf
returns a
CompletableFuture<Void>
.
s
I understand that
Unit
isn't
void
, (though it "takes its place", there's probably a more correct formal way of stating this). I'm saying
Unit
allows a more ellegant typesystem than one that uses
void
.