kz
11/11/2019, 9:38 PMkz
11/11/2019, 9:38 PMBurkhard
11/11/2019, 9:50 PMwhen
expressions. This is one of the most powerful systems in kotlin and limiting this is a bad idea.
I don’t know about inner/nested classes. They are great for stuff like Map.Entry
and I don’t see any good argument why you should forbid them. I would argue that they lead to a cleaner namespace.
Othrewise one class per file is ok. Ok, maybe it’s overkill for 1 liner data classes but in the end it does not hurt. I would just make an exception for nested/sealed and private classes.
Private classes belong where they are used and sealed classes are just too good to ignore.Burkhard
11/11/2019, 9:54 PMkz
11/11/2019, 9:54 PMkz
11/11/2019, 9:54 PMkz
11/11/2019, 9:55 PMsealed
existing in C# simply as a final
class.karelpeeters
11/11/2019, 10:20 PMthanksforallthefish
11/12/2019, 7:31 AMkt
file conflates the definition of package imo.
I use multiple classes per file however, especially for class hierarchy and default interface implementation (and sealed classes as mentioned)
regarding your second point, that does not make any sense, with inner/nested classes you can use private classes to make your code more readable and prevent usage outside of where the class is neededkz
11/12/2019, 3:58 PMkz
11/12/2019, 4:04 PMpackage
already exists and multiple classes per file don't change what package those classes exist in. I'm not arguing that everything should be in one file but when you have a bunch of 1-line data classes that are very related (data binding a Rest API) and the file is well named, I don't see why those should pollute the screen space/file system by existing separately.Hullaballoonatic
11/19/2019, 12:07 AMclass Vector2(var a: Int = 0, var b: Int = 0) {
fun reversed() = Vector2(b, a)
}
every time you call Vector2::reversed
you are instantiating another object. You can't set the value at construction and forget about it because then you are not accounting for changes to a
and b
objectively the more space/time efficient way to accomplish this is to use inner classes:
https://pl.kotl.in/L-s2-E7Fe
now, i understand this is not the best single example, because it seems a bit convoluted, but this approach can be extremely effective.michaelsims
11/19/2019, 4:58 PMHullaballoonatic
11/19/2019, 6:50 PMBurkhard
11/20/2019, 3:57 AMVector2Reversed
class should still be in the same file, maybe even nested. I’m not sure if inline classes can be nested.Hullaballoonatic
11/20/2019, 4:38 AMVector2
using them?Burkhard
11/20/2019, 1:46 PMVector2
. My idea was to keep Vector2
the way it is, but instead of implementing Reversed
as an inner class you would use an inline class.
class Vector2Impl(override var a: Float, override var b: Float): Vector2
inline class Vector2Reversed(private val v: Vector2): Vector2 {
val a get() = v.b
val b get() = v.a
}
Well I guess there is one problem with them. They can’t extend classes only interfaces, so you would need to promote Vector2
to an interface. Not saying this is the best solution, but it is another option.ghedeon
11/21/2019, 6:37 PMkz
11/22/2019, 1:34 PMTillmann Berg
11/23/2019, 4:50 PMTillmann Berg
11/23/2019, 4:57 PMTillmann Berg
11/23/2019, 5:01 PMdarkmoon_uk
11/30/2019, 8:10 AM