Repository navigation
Interface
Home › Interfaces › Interface
Declares a contract: a list of methods and properties that a type promises to provide.
interface name [ ( of T [ as constraint ] [ , ... ] ) ]
[ inherits base_interface [ , base_interface ... ] ]
[ declare ] { sub | function | property } member ( parameters ) [ as type ]
...
end interface-
name: the interface's name. By convention it starts withI. -
T: a type parameter, making a generic interface. See Generic interfaces. -
base_interface: an interface this one includes. Any number, on oneinheritsline or several. -
member: a method or property every implementing type must supply.
An interface says what a type can do and nothing about how. It has no fields, no code and no constructor, and it is never created on its own. A type takes it on with an Implements line in its body and maps a method of its own to each member.
Every member is public and abstract, so a member declaration carries no virtual, abstract, static or access word (TKO0429), and declare is optional. Only member declarations and inherits lines may appear in the body (TKO0428). The inherits lines come first (TKO0435).
An interface is a type. A variable, parameter, result, field or array element can be of an interface type. Its value is a reference: the address of an object that implements the interface, and that type's dispatch table for it. It is 16 bytes, and copying it copies the reference, never the object. An object of any implementing type converts to it implicitly, and so does a pointer to one. Members are called with .:
interface INamed
function Name( ) as ustring
end interface
interface IShape
inherits INamed '' an IShape is an INamed too
function Area( ) as float64
property Scale( ) as float64 '' a getter...
property Scale( byval s as float64 ) '' ...and a setter
end interface
type Square
implements IShape
as float64 side = 2, k = 1
declare function Name( ) as ustring implements INamed.Name
declare function Area( ) as float64 implements IShape.Area
declare property Scale( ) as float64 implements IShape.Scale
declare property Scale( byval s as float64 ) implements IShape.Scale
end type
function Square.Name( ) as ustring
return "square"
end function
function Square.Area( ) as float64
return ( side * k ) ^ 2
end function
property Square.Scale( ) as float64
return k
end property
property Square.Scale( byval s as float64 )
k = s
end property
dim as Square sq
dim as IShape s = sq '' the object's address and Square's dispatch table
s.Scale = 3
print s.Name( ); " area"; s.Area( )
dim as INamed n = s '' widening to a base interface
print n.Name( ); ", size of an interface value:"; sizeof( IShape )square area 36
square, size of an interface value: 16
An interface value does not own its object: the object must outlive every interface value that refers to it. A temporary such as a function result therefore cannot be converted (TKO1012), and delete through an interface is refused (TKO1013). An interface itself cannot be created: new IShape is refused (TKO1007). A dim of an interface type declares an empty reference, equal to nullptr until something is assigned to it.
inherits makes the base's members part of this interface. A type that implements IShape above implements INamed too, and an IShape converts to an INamed implicitly. A member reached through two bases that both declare it must be called through one of them (TKO1010).
Properties are listed with their accessors: a getter, a setter, or both. Assigning to a property through the interface needs its setter in the contract (TKO0680), and compound assignment (s.Scale += 1) uses both.
interface IComparer( of T )
function Compare( byval a as T, byval b as T ) as int32
end interfaceEach instance, IComparer( of ustring ) or IComparer( of float64 ), is an interface of its own. A type implements an instance by naming it (implements IComparer( of ustring )), and maps a member with implements IComparer( of ustring ).Compare. A generic class can implement instances that use its own parameter: implements IComparer( of T ). The interface name alone, without type arguments, is an error (TKO0815), as is the wrong number of them (TKO0816).
A generic procedure that takes an IComparer( of T ) infers T from an argument that implements one instance of it. The Interfaces Tutorial builds a sort on this, and Generics covers constraints, of T as IComparable( of T ).
-
interfaceis recognised only where a declaration can start and the body ends inend interface; elsewhere it remains an ordinary name. - An interface value compares equal to another when both refer to the same object (
=,<>), and tonullptrwhen it refers to none. -
isandTryCastask what the object behind an interface value really is. - Interfaces are not COM interfaces. A COM interface is a binary layout, written as a
typethat extendsobjectwithabstractmethods.
Category: Interfaces Kind: keyword
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing