https://kotlinlang.org logo
#naming
Title
# naming
j

Jacob Rhoda

09/21/2023, 7:53 PM
Hello! Apologies if this is a stupid question. Is there guidance for how to name something that conflicts with a language name / type? For example, I’m writing some code that handles measurements and units, and converting between units. Do I just name something “Unit,” and resign myself to writing
kotlin.Unit
to disambiguate?
c

Chris Fillmore

09/21/2023, 11:58 PM
You have a unit that’s just called
Unit
?
What exactly does your code look like?
j

Jacob Rhoda

09/22/2023, 12:51 PM
Yeah, it describes a unit.
c

Chris Fillmore

09/22/2023, 12:55 PM
But that is
MeasurementUnit
, so there is no collision with
kotlin.Unit
. You’re all good
j

Jacob Rhoda

09/22/2023, 12:55 PM
Correct. That was the other name I came up with. Ideally I’d call it
Unit
, which is why I asked my question.
c

Chris Fillmore

09/22/2023, 12:56 PM
Ah ok
b

bod

09/22/2023, 12:57 PM
I think that's the right call. Calling it Unit is certainly possible but will make it difficult to use and that's the last thing you want for your library.
c

Chris Fillmore

09/22/2023, 12:57 PM
Many codebases will have their own
Result
type, for example, which collides with
kotlin.Result
But that’s a good point from Benoit. If you’re creating a library, you may be better off avoiding collisions
j

Jacob Rhoda

09/22/2023, 1:05 PM
Okay. I wasn’t sure if Kotlin had some convention or way to deal with interfering types. Sometimes there’s a convention, like prepending with a letter, or using some language feature to disambiguate.
c

CLOVIS

09/22/2023, 1:09 PM
Sometimes there’s a convention, like prepending with a letter, or using some language feature to disambiguate.
There is: it's package names and import aliases. But those are the user's responsibility, so if it's a type you use a lot, it's probably worth having a different name to avoid the hassle
b

bod

09/22/2023, 1:10 PM
if 100% of your users will rename your
Unit
with an import alias, you might as well do it for them 🙂
2
c

Chris Fillmore

09/22/2023, 1:10 PM
Agreed. Many times I have imported
kotlin.Result
by accident
s

Starr

10/01/2023, 4:55 PM
I've also seen
SIUnit
used for this problemspace
j

Jacob Rhoda

10/02/2023, 4:03 PM
I wouldn’t be against
SIUnit
, however it would also represent non-SI units…
j

Jonathan Olsson

11/29/2023, 6:41 AM
UnitDefinition, UnitModel, UnitBase
seems perhaps like decent alternatives as well in addition to
MeasurementUnit
.
👍 1