Mixins
Why Mixins and Roles?
Just like contexts, mixins and roles is a concept to enhance the possibilities for modeling dynamic systems. In classical object-oriented programming (OOP), a person and an employee, both would be represented as a class. But there is a natural difference: while a person is a person for their entire life, a person might become an employee and quit the job multiple times during lifetime. Classical OOP is too restricted to sufficiently represent those dynamics. Roles and mixins is a concept to deal with that.
Mixins
Contexts.Mixin
— TypeMixin
Abstract supertype for all mixin types. Mixins allow context-dependent extension of types.
Concrete mixins should be defined with the @mixin
macro.
Contexts.@newMixin
— Macro@newMixin(context, mixin, attributes)
Macro to define a new Mixin type for a context, with specified attributes. Creates a mutable struct subtype of Mixin and registers it for the context.
Arguments:
context
: The context to associate the mixin withmixin
: The mixin type (with contextual type, e.g. MyMixin<<MyType)attributes
: Fields for the mixin struct
Contexts.addMixin
— MethodaddMixin(context, contextualType, mixinNameSymbol)
Adds a mixin within a context for a given contextual type to the manager.
Arguments:
context
: ContextcontextualType
: Type to associate the mixin withmixinNameSymbol
: Symbol of the mixin type
Example: addMixin(MyContext, MyType, :MyMixin)
Contexts.assignMixin
— MethodassignMixin(context::Context, pair::Pair)
Assigns a mixin to a type in a given context.
Arguments:
context
: Contextpair
: Pair of (type, mixin)
Example: assignMixin(ctx, MyType => MyMixin())
Contexts.disassignMixin
— MethoddisassignMixin(context::Context, pair::Pair)
Removes a mixin assignment from a type in a given context.
Arguments:
context
: Contextpair
: Pair of (type, mixin)
Example: disassignMixin(ctx, MyType => MyMixin())
Contexts.hasMixin
— MethodhasMixin(context::Context, obj, mixin::Type)
Checks if an object has a mixin of the given type in a context.
Arguments:
context
: Contextobj
: Objectmixin
: Mixin type
Returns true or false.
Example: hasMixin(MyContext, obj, MyMixin)
Contexts.getMixins
— FunctiongetMixins()
Returns all mixin definitions for all contexts.
Example: getMixins()
getMixins(type)
Returns all mixin instances for a given type.
Arguments:
type
: Type
Example: getMixins(MyType)
getMixins(context::Context, type)
Returns all mixin instances for a type in a context.
Arguments:
context
: Contexttype
: Type
Example: getMixins(MyContext, MyType)
Contexts.getObjectsOfMixin
— MethodgetObjectsOfMixin(context::Context, mixin::Type)
Returns all objects in a context that have a mixin of the given type.
Arguments:
context
: Contextmixin
: Mixin type
Example: getObjectsOfMixin(MyContext, MyMixin)
Mixins allow you to extend types with context-dependent behavior. Use @newMixin
to define mixin types, and assignMixin
/disassignMixin
to manage mixin assignments in contexts.
See also: assignMixin
, getMixins