Skip to content

Union types #90

@rigolepe

Description

@rigolepe
  • A sub-type can override properties of its parent type with the following restrictions: 1) a required property in the parent type cannot be changed to optional in the sub-type, and 2) the type declaration of a defined property in the parent type can only be changed to a narrower type (a specialization of the parent type) in the sub-type.
  • type discriminiator support
    • discriminator? facet
      • Determines the concrete type of an individual object at runtime when, for example, payloads contain ambiguous types due to unions or inheritance. The value must match the name of one of the declared properties of a type. Unsupported practices are inline type declarations and using discriminator with non-scalar properties.
    • discriminatorValue? facet
      • Identifies the declaring type. Requires including a discriminator facet in the type declaration. A valid value is an actual value that might identify the type of an individual object and is unique in the hierarchy of the type. Inline type declarations are not supported.
    • object type name is the default discriminatorValue value
      • e.g.: Employee: # kind can equal Employee; default value for discriminatorValue
    • support ‘discriminator’ and ‘discriminatorValue’ by renaming our current convention and updating our documentation
    • Neither discriminator nor discriminatorValue can be defined for any inline type declarations or union types.
  • Union type support.
    • arrays in union type definitions are possible:
      • Manager[] | Admin[]
      • an array of Manager instances or an array of Admin instances.
    • or
      • (Manager | Admin)[]: an array whose members consist of Manager or Admin instances
    • or
      • Person | Animal[]
  • Multiple inheritance
    • type: [ Person, Employee ]
    • Implementation idea: In Java, each class can have an interface thet defines its getters and setters. We generate this interface as soon as we need to solve a multiple inheritance issue. Each of the combinations above becomes a new class that implements the interfaces of the classes it inherits from. In Scala, each class can have a corresponding trait that defines the properties that are expected (instead of the Java interface) and each combination case class simply extends from each of the traits of the classes it inherits from. In both cases, the actual instantiated object is one of the combined types. The signature specifies a new interface/trait that contains the intersection of the getters and setters / properties of all the types present, or an empty interface / trait if the intersection is empty. The name of this interface is as defined in the types definition (e.g. HomeAnimal in the example below) or ‘Anonymous’ for implicit types. Beware when multiple union types are defined, duplicates may occur, so this should be handled well in this case. And what about Manager[] | Admin[]? Remark, single inheritance in Java must still be the regular inheritance between Java objects. In Scala, case classes don’t inherit from each other, so we use the same approach as with multiple inheritance.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions