Taggable

interface Taggable<Self : Taggable<Self>>

A type for immutable objects that carry an arbitrary, named bag of metadata (their tags), attachable without polluting the type's own API with a case-by-case property for every possible piece of metadata.

This is used, for instance, by it.unibo.tuprolog.core.Term and it.unibo.tuprolog.core.Substitution (in the :core module) to let solvers and libraries stash solver-specific bookkeeping data (e.g. provenance information) onto terms and substitutions, without both those types and every one of their many implementations needing to know about it. Since implementations are expected to be immutable, "mutating" the tags of a Self does not happen in place: it always goes through replaceTags (see the addTag/addTags/setTag/setTags/clearTags extension functions in Taggables.kt for convenient, higher-level ways to derive a new, differently-tagged instance).

Type Parameters

Self

is the (CRTP) concrete type implementing this interface, i.e. the type returned by replaceTags

Properties

Link copied to clipboard
abstract val tags: Map<String, Any>

The (possibly empty) map of tags currently attached to this object, keyed by tag name.

Functions

Link copied to clipboard
open fun containsTag(name: String): Boolean

Whether a tag named name is currently attached to this object.

Link copied to clipboard
open fun <T : Any> getTag(name: String): T?

Retrieves the tag named name, cast to T, or null if no such tag is attached. The cast is unchecked (erased generics): if a tag named name is attached but is not actually an instance of T, this call will not fail, but a ClassCastException is likely to surface later, wherever the caller uses the returned value as a T.

Link copied to clipboard
abstract fun replaceTags(tags: Map<String, Any>): Self

Returns a new instance, otherwise equivalent to this one, whose tags are entirely replaced by tags (i.e. this is not a merge: any tag not present in tags is dropped).