Repository navigation
Implements
Home › Interfaces › Implements
Lists the interfaces a type or class takes on, and says which interface member each method provides.
type name '' or: class name
implements interface [ , interface ... ]
declare method_signature implements interface.member [ , interface.member ... ]
...
end type-
interface: an interface, or an instance of a generic one:IComparer( of ustring ). -
member: a method or property of that interface, or of an interface it inherits.
implements is written in two places, and each one says something different.
An implements line in the body lists interfaces. It goes at the top of the body, after a class's inherits line and before any member (TKO0435). A type may list any number of interfaces, on one line or several, but each only once (TKO1006). Listing an interface also takes on every interface it inherits. Writing implements on the header, type T implements I, is an error that says where it belongs (TKO0436).
An implements clause on a method's declaration maps that method to an interface member. Mappings are explicit: a public method named Close does not implement IFile.Close until its declaration says implements IFile.Close. Then:
-
The method's name does not have to match the member's.
FileNamecan implementIFile.Name. - Same-named members of different interfaces are different members, and each gets its own method.
-
One method can provide several members:
implements IFile.Close, IItem.Close. - A private method can implement a member. It is then reachable through the interface and nowhere else.
interface IFile
function Name( ) as ustring '' the file's name
sub Close( )
end interface
interface IItem
function Name( ) as ustring '' the item's display name
sub Close( )
end interface
type Document
implements IFile, IItem '' one line, or one 'implements' line each
declare function FileName( ) as ustring implements IFile.Name
declare function Title( ) as ustring implements IItem.Name
declare sub Close( ) implements IFile.Close, IItem.Close '' one method, two members
as int32 opened = 1
private:
declare sub Flush( )
end type
function Document.FileName( ) as ustring
return "report.txt"
end function
function Document.Title( ) as ustring
return "Quarterly report"
end function
sub Document.Close( )
Flush( )
opened = 0
end sub
sub Document.Flush( )
print " (flushed)"
end sub
dim as Document doc
dim as IFile f = doc
dim as IItem it = doc
print f.Name( ); " / "; it.Name( )
it.Close( )
print "open:"; doc.openedreport.txt / Quarterly report
(flushed)
open: 0
The signatures must match exactly: the parameters, their passing modes, the result type and const-ness (TKO1001 names what differs). A plain type maps its members with ordinary methods; a class may map one to a virtual or abstract method, and a derived class's override is then what calls through the interface reach.
Every member must be provided (TKO1000 names each one missing). A member the base class already provides counts, and mapping it again in the derived class is refused (TKO1011): override the base's method instead.
- The clause belongs on the declaration inside the type, not on the out-of-line body (TKO0438).
- A
staticmethod cannot implement a member (TKO1014). - When the method and the interface member give a parameter different default values, a call through the interface uses the interface's, and the compiler warns (TKO1018).
- A generic class maps members of instances that use its own parameters:
implements IComparer( of T ).Compare.
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