Skip to content

ObservableBuffer.Change has no generic type parameters, while the majority of its children do #184

@RomanHargrave

Description

@RomanHargrave

ObservableBuffer.Change is the superclass of various case classes used to process changes to observable buffers. In order to match against case classes, and retain implicit type inference, adding a type parameter to Change appears to be necessary. As it stands right now, one needs to explicitly the parameter types of an arbitrary case class when applying a matcher in order for the compiler to assume the correct typing for that parameter or case class. Take this for instance:

changes.foreach {
    case Add(_, elements) => // do your stuff
}

elements will be assumed to be of the type Traversable[Any] by the compiler, even if the collection being observed is of type X, in which case it should be Traversable[X]. In order to get the expected behavior from the compiler, we must say that we specifically want Traversable[X] (note that this will also generate an annoying erasure warning):

changes.foreach {
    case Add(_, elements: Traversable[X]) => // do your stuff
}

and we cannot do

changes.foreach {
    case Add[X](_ elements) => // do your stuff
}

as that is actually invalid syntax.

If Change were to have a type parameter T, one could specify the type of Changes when adding a ChangeListener to a buffer:

onChange { (collection: ObservableBuffer[X], changes: Seq[Change[X]]) =>
    changes.foreach {
        case Add(_, elements) => // do your stuff with Traversable[X]
    }
}

As it stands right now, this would require is modifying the class signatures for operations to accept a type, and in the case of operation cases where type is unimportant (such as Reorder), the type Any may be specified when inheriting Change, or can be specified and implicitly inferred.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions