/kotlin_property_extensions

A small lib example, showing how to extend exisiting types with new fields/properties, with memory management adherence.

Primary LanguageKotlinMIT LicenseMIT

Kotlin Typ Extensions with Properties

Enables existing types to be extended with properties/fields instead of only with functions.

  • Uses a WeakIdentityHashMap to implement memory management for backing fields.
  • Uses delegation pattern to simply add fields to existing types.

Example

import java.io.InputStreamimport java.io.InputStream

var InputStream.tag by BackingField { "$it" }

val stream = InputStream.nullInputStream().also {
	it.tag = "Some tag"
}

println(stream.tag) // Prints: Some tag

// Reassign value
stream.tag = "$stream"