Interface Type

From Resonite Wiki
Revision as of 18:58, 17 August 2024 by Yosh (talk | contribs) (authoritytimebase is double.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

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