For `typealias` naming - which style do you prefer...
# announcements
z
For
typealias
naming - which style do you prefer?
Copy code
() -> Int
Copy code
// 1
typealias ProvideNumber = () -> Int
Copy code
// 2
typealias NumberProvider = () -> Int
Usage then becomes either
provideNumber()
or `numberProvider()`/`numberProvider.invoke()`
2️⃣ 16
1️⃣ 2
I’ve personally leaned towards
ProvideNumber
because it’s a function that provides a number - functions are verbs
provideNumber()
also reads better
d
From the two option I'd say
NumberProvider
. However I'd argue that that is not a useful typealias.
() -> Int
is already Kotlin-Speak for
NumberProvider
.
z
it’s an example
d
Better to use a meaningful type (instead of just
Int
) and the use
() -> UserAge
(as an example)
👍 1
z
correct, but that is a different topic
you would still use a typealias for the provider because
() -> UserAge
could become
suspend () -> UserAge
and you would have to pass around that info
d
You'd still have to touch (possibly) all call-sites because you'd have to now account for the suspension.
👆 1
z
sure, but that’s not relevant
d
I just don't know what advantage the name
XProvider
has over
() -> X
d
NumberProvider
as a type name but
provideNumber
as a variable/function name.
👍 1
5
z
@diesieben07
Copy code
val provideOne: () -> Int = { 1 }
val provideTwo: () -> Int = { 2 }
now lets say I change
() -> Int
to be
suspend () -> Int
. I have to update the type for both
provideOne
and
provideTwo
. Whereas if I used a
typealias
instead of
() -> Int
, I wouldn’t have to update the definition. Wherever you specify
() -> Int
again for what is the same type (
NumberProvider
) you are duplicating code (repeating yourself)
() -> Int
is effectively specifying 3 pieces of information: non-suspending function that takes no parameters that returns an
Int
. Which means when you have to update any of those 3 pieces, everywhere you specify
() -> Int
needs to be updated, instead of just updating the
typealias
and fixing any potential compile errors
NumberProvider
as a type name but
provideNumber
as a variable/function name.
I’ve actually done that, but construction seems a little weird. but definitely a third option
Copy code
SomeClass(provideNumber = numberProvider)