Esa
08/12/2019, 12:10 PMdalexander
08/12/2019, 12:13 PMEsa
08/12/2019, 12:16 PMprivate sealed class
for now, as I only want to see it used in this file, and in this specific waydalexander
08/12/2019, 12:28 PMSoftware
doesn't automatically extend from Product
. You need to explicitly do that.Esa
08/12/2019, 12:28 PMConstructor of inner class can be called only with receiver of containing class
Not sure I follow what this meansdalexander
08/12/2019, 1:09 PMEsa
08/12/2019, 1:12 PMprivate sealed class Product(val a: Int) {
inner class Software(val b: String) : Product(a)
}
private fun test() {
val c = Product.Software("S", 1)
}
dalexander
08/12/2019, 1:16 PMclass software
Esa
08/12/2019, 1:17 PMa
invisible for the Software
dalexander
08/12/2019, 1:17 PMinner class
means that it's capturing all of the properties in the containing class. It's what a non-static inner class does in Java, if you're familiar with Java.a
it should be a parameter to Software
.inner class
then you need to do Product(1).Software(...)
I believe.Esa
08/12/2019, 1:18 PMProduct
has some values that does not really care whether it is software or not. I thought it would make sense to place these values on the Product
class, and not the subclassdalexander
08/12/2019, 1:21 PMinner class
syntax, then what you're actually doing it creating an instance of Product
then creating a Software
which extends Product
but has a different Product
as its outer class. This way is cleaner because the only Product
is the one that's the super-class of Software
.Esa
08/12/2019, 1:23 PMdalexander
08/12/2019, 1:24 PMProduct
so for example: val p = Product(1)
val s = p.Software("a")
(similar syntax exists in Java for this case, it's just rarely used).Esa
08/12/2019, 1:24 PMdalexander
08/12/2019, 1:25 PMEsa
08/12/2019, 1:26 PMa
through e
you have a lot of stuff to list in both the software
constructor and the : Product()
constructordalexander
08/12/2019, 1:30 PMProduct
an interface, and passing product as a parameter to your Hardware
or Software
classes, creating some kind of interface for Product
and then using class delegation... ie:
class Software(a: Int, product: Product): ProductInterface by product
That moves the code repetition to creating ProductInterface
rather than having more constructor parameters (assuming you have so many constructor parameters you're concerned). The problem is that maintaining ProductInterface
can become tedious depending on how many methods it has, and how often you add/remove methods.Jérémy Touati
08/12/2019, 5:24 PMProduct
that I do not want to see initialized by itself, but as one of its subclasses Hardware
or Software
. The two subclasses has unique values of their own, but the Product
class has a few shared values that both hardware/software-products will have"
didn't you simply describe an abstract Product
class with Hardware
and Software
concrete subclasses? i must be missing something though
like this:
https://pl.kotl.in/2JD_Qz4WF