Q:

Kotlin optional generic parameter

Here is the problem I am trying to resolve, I am trying to use a void type as a generic type:
class Parent {
    private abstract class Item<out T>(val data: T)
    // This subclass should contain data
    private class ItemContent(val data: String): Item<String>(data)
    // This subclass doesn't contain data
    private class ItemNoContent: Item<Any?>(null)
}

Some base classes like ItemNoContent doesn't contain meaningful data so I make ItemNoContent extends Item(null). It works but I feel that the use of Any? and null is inappropriate here. Is there a more Kotlin way to solve this optional generic problem? 

A:

You can also use Item<Unit>(Unit) which represents a void value in Kotlin.

A:

Some base classes like ItemNoContent doesn't contain meaningful data

Then why extend a class which is supposed to have it? While Unit and null are both options, consider also
private abstract class Item<out T>
private abstract class ItemWithContent<out T>(val data: T) : Item<T>
...
// object may make more sense than class here
private object ItemNoContent : Item<Nothing>()

A:

I would tweak the inheritance like this:
abstract class Item

abstract class ItemWithContent<T>(val d: T): Item()

class ItemWithStringContent(d: String): ItemWithContent<String>(d)

class ItemWithNoContent: Item()

This way, there is not need to use Unit or Nothing.
Usage:
fun main(args: Array<String>){
    val t: Item = ItemWithStringContent("test")
    println((t as? ItemWithStringContent)?.d)
}

