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.MixinType
Mixin

Abstract supertype for all mixin types. Mixins allow context-dependent extension of types.

Concrete mixins should be defined with the @mixin macro.

source
Contexts.@newMixinMacro
@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 with
  • mixin: The mixin type (with contextual type, e.g. MyMixin<<MyType)
  • attributes: Fields for the mixin struct
source
Contexts.addMixinMethod
addMixin(context, contextualType, mixinNameSymbol)

Adds a mixin within a context for a given contextual type to the manager.

Arguments:

  • context: Context
  • contextualType: Type to associate the mixin with
  • mixinNameSymbol: Symbol of the mixin type

Example: addMixin(MyContext, MyType, :MyMixin)

source
Contexts.assignMixinMethod
assignMixin(context::Context, pair::Pair)

Assigns a mixin to a type in a given context.

Arguments:

  • context: Context
  • pair: Pair of (type, mixin)

Example: assignMixin(ctx, MyType => MyMixin())

source
Contexts.disassignMixinMethod
disassignMixin(context::Context, pair::Pair)

Removes a mixin assignment from a type in a given context.

Arguments:

  • context: Context
  • pair: Pair of (type, mixin)

Example: disassignMixin(ctx, MyType => MyMixin())

source
Contexts.hasMixinMethod
hasMixin(context::Context, obj, mixin::Type)

Checks if an object has a mixin of the given type in a context.

Arguments:

  • context: Context
  • obj: Object
  • mixin: Mixin type

Returns true or false.

Example: hasMixin(MyContext, obj, MyMixin)

source
Contexts.getMixinsFunction
getMixins()

Returns all mixin definitions for all contexts.

Example: getMixins()

source
getMixins(type)

Returns all mixin instances for a given type.

Arguments:

  • type: Type

Example: getMixins(MyType)

source
getMixins(context::Context, type)

Returns all mixin instances for a type in a context.

Arguments:

  • context: Context
  • type: Type

Example: getMixins(MyContext, MyType)

source
Contexts.getObjectsOfMixinMethod
getObjectsOfMixin(context::Context, mixin::Type)

Returns all objects in a context that have a mixin of the given type.

Arguments:

  • context: Context
  • mixin: Mixin type

Example: getObjectsOfMixin(MyContext, MyMixin)

source

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