Interface Type

From Resonite Wiki
(Redirected from Interface Types)

An interface type is a Reference Type that does not necessarily refer to a specific type that can be instantiated, but rather a contract of sorts. Any type that "implements" the interface must provide an implementation of whatever is defined within the interface or fall back to a default implementation on the interface itself. They are used to describe a specific common behavior that classes of a similar kind can implement to avoid code duplication. Types may implement multiple interfaces at once.

Within FrooxEngine, interfaces are extensively used for nearly every single type. For example, every Reference Type implements the IWorldElement interface. A common convention, and the convention used within FrooxEngine, is for all interfaces type names to start with a capital I.

Interface types have a sort of one-way allowance of direct usage. Objects can only be non-interface types, meaning a non-interface type must implement the interface before an object can be instantiated with the properties of the interface. However, a function or Field may request any kind of interface type directly, then rely on the shared behavior of the interface to call methods or access values on the object.

Shared Behavior

Because interfaces represent shared behavior across all implementations of the interface, they can be leveraged to provide generic behavior across many different, yet similar types.

For example, Source nodes retrieve the value of any object that implements the IValue<T> type matching the node. This allows for a double source to get the output of an AuthorityTimeBase, ValueMultiplexer<double>, or any double fields in general, despite these three instances strictly being separate types.

Casting

Objects of a specific type can be cast to and from any interface type they implement, just like with their potential derived type. This can be leveraged with the shared behavior property to make generic implementations for specific types.

For example, any component can be cast to an IComponent, but only components matching the requested type can be cast from an IComponent to the component type. You can cast an ObjectRoot to IComponent, but you can't cast the resulting IComponent reference into a Grabbable component.

See Also