TypedValue

sealed class TypedValue

A type-safe wrapper for values that may be either enums or strings.

This sealed class enables storing enum values along with their type information, allowing runtime type-safe access and reconstruction of the original enum.

Usage

enum class OrderSteps { VALIDATE, PAYMENT, SHIP }

// Creating typed values
val enumValue = TypedValue.fromEnum(OrderSteps.VALIDATE)
val stringValue = TypedValue.fromString("custom-step")

// Type-safe access
val step: OrderSteps? = enumValue.getAs<OrderSteps>() // Returns VALIDATE
val wrong: FailureReason? = enumValue.getAs<FailureReason>() // Returns null

// String representation (always available)
val name: String = enumValue.stringValue // "VALIDATE"

Inheritors

Types

Link copied to clipboard
object Companion
Link copied to clipboard
data class EnumValue<E : Enum<E>>(val value: E, val enumClass: KClass<E>) : TypedValue

Wrapper for an enum value with its type token.

Link copied to clipboard
data class StringValue(val value: String) : TypedValue

Wrapper for a plain string value.

Properties

Link copied to clipboard
abstract val stringValue: String

String representation of the value. For enums, this is the enum's name property. For strings, this is the string itself.

Functions

Link copied to clipboard

Returns the enum value if this is an EnumValue, null otherwise. The type is erased to Enum<*> since the actual type is not known at compile time.

Link copied to clipboard
inline fun <E : Enum<E>> getAs(): E?

Attempts to retrieve the value as the specified enum type.

fun <E : Enum<E>> getAs(kClass: KClass<E>): E?

Attempts to retrieve the value as the specified enum type using KClass.

Link copied to clipboard
inline fun <E : Enum<E>> isType(): Boolean

Checks if this value is of the specified enum type.

fun <E : Enum<E>> isType(kClass: KClass<E>): Boolean

Checks if this value is of the specified enum type using KClass.