Repository navigation
Const Member
Home › User Defined Types › Const (Member)
Specifies that a member procedure is read only.
Type typename
Declare Const Sub|Function|Property|Operator ...
End Type
[Const] Sub|Function|... typename ...
...
End Sub|Function|...Specifies that a method does not change the object it is called on. The hidden This parameter will be considered read-only. The declaration can be read as 'invoking a const method promises not to change the object', and the compiler will error if the member procedure tries to change any of the data fields, or calls a non-const member procedure.
Read-only (Const) declarations are a measure of type safety that can be read as 'promises not to change.' The compiler uses the const declarations to check operations on variables and parameters and generate an error at compile time if their data could potentially change. There is no runtime overhead for using Const qualifiers since all of the checks are made at compile time.
Constructors and destructors cannot be Const (not useful).
Member procedures can not be both Const and Static since static member procedures do not have a hidden This parameter.
For methods with Const in their declaration, Const can also be specified on the corresponding method bodies, for improved code readability.
- New to FreeBASIC
'' Const Member Procedures
Type foo
x As Integer
c As Const Integer = 0
Declare Const Sub Inspect1()
Declare Const Sub Inspect2()
Declare Sub Mutate1()
Declare Sub Mutate2()
End Type
''
Sub foo.Mutate1()
'' we can change non-const data fields
x = 1
'' but we still can't change const data
'' fields, they are promised not to change
'' c = 1 '' Compile error
End Sub
''
Sub foo.Mutate2()
'' we can call const members
Inspect1()
'' and non-const members
Mutate1()
End Sub
''
Sub foo.Inspect1()
'' can use data members
Dim y As Integer
y = c + x
'' but not change them because Inspect1()
'' is const and promises not to change foo
'' x = 10 '' Compile error
End Sub
''
Sub foo.Inspect2()
'' we can call const members
Inspect1()
'' but not non-const members
'' Mutate1() '' Compile error
End SubCategory: User Defined Types
Kind: member
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
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