https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
a

Andrew Gazelka

05/14/2019, 10:02 PM
any reason Kotlin doesn't use
Integer
for
lateinit num: Int
since it uses
Integer
for
Int?
s

serebit

05/14/2019, 10:04 PM
Why not just set
num
to an arbitrary number by default, since it’ll be set later anyway?
r

Ruckus

05/14/2019, 10:04 PM
@Andrew Gazelka I believe it's to avoid the unexpected overhead of autoboxing, but don't quote me on that.
a

Andrew Gazelka

05/14/2019, 10:04 PM
@serebit why have nulls at all if they will just be set later anyway?
sounds like a strawman to me
s

serebit

05/14/2019, 10:05 PM
False equivalency. If you’re using lateinit, you’re making a guarantee that the value isn’t going to be used until it’s set.
What it’s set to before that point doesn’t matter, and ints have no overhead, so I don’t see why lateinit is necessary here. Using lateinit would also incur a permanent boxing overhead like Ruckus mentioned.
a

Andrew Gazelka

05/14/2019, 10:07 PM
this sounds like micro-optimization to me
r

Ruckus

05/14/2019, 10:07 PM
https://stackoverflow.com/a/49594199
Normally, properties declared as having a non-null type must be initialized in the constructor. However, fairly often this is not convenient. For example, properties can be initialized through dependency injection, or in the setup method of a unit test. In this case, you cannot supply a non-null initializer in the constructor, but you still want to avoid null checks when referencing the property inside the body of a class.
a

Andrew Gazelka

05/14/2019, 10:07 PM
esp since there is no way to make
lateinit Integer
it is forced
r

Ruckus

05/14/2019, 10:08 PM
In that case, I'm not sure.
s

serebit

05/14/2019, 10:08 PM
Correct, because there’s no reason why you’d need to. You can just set it to zero, or some other arbitrary number.
What’s your use case?
a

Andrew Gazelka

05/14/2019, 10:09 PM
@serebit so you are saying if all objects had a default value implementation we wouldn't need null?
I think
lateinit
is nice because if we do make a mistake and forget to initialize it properly, we won't get weird errors
similar to how returning
-1
from a function instead of
null
is kinda jank (and esp bad if the range of the fun is
[-inf,inf]
s

serebit

05/14/2019, 10:11 PM
I’m not sure why you’re putting words in my mouth, because that’s not what I’m saying.
a

Andrew Gazelka

05/14/2019, 10:12 PM
I don't see how I am putting words in your mouth. Maybe I am not paying attention closely enough...
or don't understand what you are trying to say
s

serebit

05/14/2019, 10:12 PM
The value can’t be set to null, so a placeholder value can be used before being set to the proper value. The placeholder value doesn’t matter, it’s never used, and it incurs no performance penalty. So what’s your use case? Why do you require lateinit, if using a default value that’s overwritten before the property is used works exactly the same as using lateinit would if it were supported?
Even if lateinit for ints were implemented, it would have to set the value to a placeholder, because null can’t be used here.
a

Andrew Gazelka

05/14/2019, 10:13 PM
because we might forget to initialize it and then the program would run without errors, even though it is not initialized
☝️🏻 2
c

Cody Engel

05/14/2019, 10:17 PM
Just a casual onlooker here, but yes, if you try to access a lateinit property before it is set the app should throw an exception. Setting a default value is not the same as lateinit. As for the actual question I’m not sure why it doesn’t use
Integer
in that case.
The running theory of @Ruckus sounds the most sane though. I would be curious about why it matters what Kotlin is doing behind the scenes (not as a jab, but truly curious about the usecase).
s

serebit

05/14/2019, 10:22 PM
In fairness to the language designers,
Int?
can and should be autoboxed because it might be null after being initialized. A lateinit
Int
would only be null pre-initialization, so it seems odd to box it for the entirety of the runtime.
k

karelpeeters

05/14/2019, 10:36 PM
Then add another boolean field to solve that. I find it incredibly frustrating that you can't do this as well.
c

Cody Engel

05/14/2019, 10:40 PM
But why is it frustrating? I’ve been using Kotlin for almost 3 years now and that
lateinit Int
never crossed my mind or bothered me. I assume Kotlin is going to do stuff under the hood, as long as my code works I’m not too concerned. If I had to get too nitty gritty with that stuff I’d imagine things like Kotlin/Native would be much more verbose and difficult.
k

karelpeeters

05/14/2019, 10:41 PM
Right, I don't care what happens in the background, it should just work.
👍 2
s

serebit

05/14/2019, 10:41 PM
It bothered me once, and when I realized that it didn’t matter and I could just use a default value instead, I immediately stopped caring
r

Ruckus

05/14/2019, 10:44 PM
@karelpeeters I can imagine a hidden Boolean could cause all sorts of problems, especially with things like reflection.
k

karelpeeters

05/14/2019, 10:44 PM
@serebit But then you lose runtime safety.
s

serebit

05/14/2019, 10:44 PM
If I forget to initialize it? Yes. So I didn’t forget to initialize it
k

karelpeeters

05/14/2019, 10:45 PM
@Ruckus Maybe, I'm not sure though. There are hidden variables generated all the time for eg. property delegation.
r

Ruckus

05/14/2019, 10:45 PM
@serebit I agree. Programming is way easier if you just remember to never make any mistakes :trollface:
:trollface: 2
k

karelpeeters

05/14/2019, 10:45 PM
@serebit That's what safety is for, to figure out when you've made a mistake.
s

serebit

05/14/2019, 10:46 PM
Lateinit is already a dodgy thing to be using and it’s only there for things that can’t be initialized where they stand. I just avoid using it in general, and when I have to, I’m careful about it. Maybe if I wanted to be sure, I’d give it a default value that would never be achieved in the program proper, and add a getter to ensure that it never returns that value
r

Ruckus

05/14/2019, 10:48 PM
it’s only there for things that can’t be initialized where they stand
Which happens quite often anytime you use a framework with custom life cycles
i

ilya.gorbunov

05/15/2019, 2:01 AM
Here's the discussion about how this sort of lateinit properties could be implemented: https://youtrack.jetbrains.com/issue/KT-15284
👍 1