m typo |
m authoritytimebase is double. |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
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 [[Type:IWorldElement|IWorldElement]] interface. A common convention, and the convention used within FrooxEngine, is for all interfaces type names to start with a capital <code>I</code>. | |||
== 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, [[ProtoFlux:Source|Source]] nodes retrieve the value of any object that implements the [[Type:IValue|IValue<T>]] type matching the node. This allows for a [[Type:double|double]] source to get the output of an [[Component:AuthorityTimeBase|AuthorityTimeBase]], [[Component:ValueMultiplexer|ValueMultiplexer<double>]], or any double fields in general, despite these three instances strictly being separate types. | |||
== Casting == | == 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|component]] can be cast to an [[Type:IComponent|IComponent]], but only components matching the requested type can be cast from an IComponent to the component type. You can cast an [[Component:ObjectRoot|ObjectRoot]] to IComponent, but you can't cast the resulting IComponent reference into a [[Component:Grabbable|Grabbable]] component. | |||
== | == See Also == | ||
* [https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface Microsoft documentation on C# interfaces] |
Latest revision as of 18:58, 17 August 2024
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
.
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.