#Help me with kotlin

1 messages · Page 1 of 1 (latest)

fossil kite
#

So I just started out with kotlin. Until now I've learned many ways to make code "cleaner" and more efficient when writing kotlin compared to java.

#

But when it comes to this

#

How can I simplify this

#

Im using Gson to parse a json file into my own "RawEmbed" object

#

But I need to check if my variables are != null to successfully get an instance of the EmbedBuilder obj (JDA)

#

I mean a whole lot of if else statements work but it seems to bloated

#

for the big if code block at the bottom of the picture, author looks like this:

chrome jetty
#

Optional null?

fossil kite
#

wdym

chrome jetty
#

obj?.name

#

for instance

#

Also

#

Believe you can create a sequence

#

And then run sth if everything is not null or if one thing is null etc

#

Anyways, null sucks

fossil kite
#

yes but embed.setTitle(obj?.name) will fail. EmbedBuilder#setTitle will throw

#

im trying to not pass null to anything

chrome jetty
#

Well, wouldn’t you use embed?.title = obj?.name

fossil kite
#

wait that works?

chrome jetty
#

Haven’t used kotlin exceedingly much

#

But if I’m not totally wrong that may work

fossil kite
#

no, I believe thats just for kotlin -> kotlin

#

not java -> kotlin

chrome jetty
#

Oh yeah interop sucks ass then

fossil kite
#

there is only setTitle

chrome jetty
#

Guess you could technically make an extension function

#

Which operates safer

fossil kite
#

hmm example? doc?

#

not sure what you mean

chrome jetty
#

Google extension function

#

Anyhow have to go now

fossil kite
#

woah

#

thats sick

#

now you mean I would create extension functions to add the data

#

and check if the data is null there

#

by data I mean my own properties

#

not sure where I'm going with this

fossil kite
#

Ended up doing this

#

as all the fields are nullable anyways