zak.taccardi
01/09/2020, 4:27 PMtypealias
naming - which style do you prefer?
() -> Int
// 1
typealias ProvideNumber = () -> Int
// 2
typealias NumberProvider = () -> Int
Usage then becomes either provideNumber()
or `numberProvider()`/`numberProvider.invoke()`zak.taccardi
01/09/2020, 4:28 PMProvideNumber
because it’s a function that provides a number - functions are verbszak.taccardi
01/09/2020, 4:28 PMprovideNumber()
also reads betterdiesieben07
01/09/2020, 4:28 PMNumberProvider
. However I'd argue that that is not a useful typealias. () -> Int
is already Kotlin-Speak for NumberProvider
.zak.taccardi
01/09/2020, 4:29 PMdiesieben07
01/09/2020, 4:29 PMInt
) and the use () -> UserAge
(as an example)zak.taccardi
01/09/2020, 4:29 PMzak.taccardi
01/09/2020, 4:30 PM() -> UserAge
could become suspend () -> UserAge
and you would have to pass around that infodiesieben07
01/09/2020, 4:31 PMzak.taccardi
01/09/2020, 4:32 PMdiesieben07
01/09/2020, 4:33 PMXProvider
has over () -> X
Dominaezzz
01/09/2020, 4:39 PMNumberProvider
as a type name but provideNumber
as a variable/function name.zak.taccardi
01/09/2020, 6:29 PMval 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 errorszak.taccardi
01/09/2020, 6:30 PMI’ve actually done that, but construction seems a little weird. but definitely a third optionas a type name butNumberProvider
as a variable/function name.provideNumber
SomeClass(provideNumber = numberProvider)