Wiki source for KeyPgTypeAlias


Show raw source

{{fbdoc item="title" value="Type (Alias)"}}----
Declares an alternative name for a type

{{fbdoc item="syntax"}}##
**Type** //typename// [[KeyPgAs|as]] //symbol//
##
{{fbdoc item="param"}}
##//typename//##
new alternative name.
##//symbol//##
symbol or data type declaration to associate with ##//typename//##.

{{fbdoc item="desc"}}
##//symbol//## may refer to any declared data type including a built-in data type, ##[[KeyPgSub|Sub]]## or ##[[KeyPgFunction|Function]]## pointer, ##[[KeyPgType|Type]]## declaration, ##[[KeyPgUnion|Union]]## declaration, or ##[[KeyPgEnum|Enum]]## declaration.

A type alias can be used to allow forward declarations of parameters in procedure declarations, but only used with pointers (whatever their passing mode), or otherwise with parameters (excluding arrays) but passed or returned only by reference. Generally the bodies of such procedures need to be implemented further in the code once the actual types are well defined.
A type alias can also be used to allow forward declarations of data fields in [[CatPgUserDefTypes|User Defined Types]], but only used with pointers.

A type alias must be used to allow declaration of pointer to a function pointer, otherwise ptr applies on return type and not on function.

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/udt/type-fwd.bas"}}%%(freebasic)
TYPE ParentFwd as Parent
TYPE Child
Name as zstring * 32
ParentRef AS ParentFwd ptr
''...
END TYPE

TYPE Parent
Name as zstring * 32
ChildList(0 to 9) AS Child
''...
END TYPE

DIM p AS Parent
p.Name = "Foo"
with p.ChildList(0)
.Name = "Jr."
.ParentRef = @p
'' ...
end with

with p.ChildList(0)
print .Name; " is child of "; .parentRef->Name
end with
%%
{{fbdoc item="filename" value="examples/manual/udt/type-fctptr.bas"}}%%(freebasic)
Function triple (ByVal i As Integer) As Integer
Return 3 * i
End Function

Type As Function (ByVal As Integer) As Integer function_alias

'Dim As Function (Byval As Integer) As Integer f ''this syntax works but is less readable than the next code line
Dim As function_alias f
f = @triple
Print f(123)

'Dim As Function (Byval As Integer) As Integer Ptr pf ''this syntax does not work because Ptr applies on Integer and not on function
Dim As function_alias Ptr pf ''this syntax works
pf = @f
Print (*pf)(123) ''the dereferenced pointer to procedure pointer must be enclosed in parentheses
%%
{{fbdoc item="diff"}}
- New to ""FreeBASIC""

{{fbdoc item="see"}}
- ##[[KeyPgType|Type...End Type]]##
- ##[[KeyPgTypeTemp|Type (Temporary)]]##

{{fbdoc item="back" value="CatPgUserDefTypes|User Defined Types"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode