Revision history for KeyPgOpCast


Revision [22373]

Last edited on 2018-08-28 14:14:30 by fxm [Enum does not support member operators]
Additions:
- New to ""FreeBASIC"".
Deletions:
- New to ""FreeBASIC""


Revision [22371]

Edited on 2018-08-28 14:05:26 by fxm [Enum does not support operators]
Additions:
{ [[KeyPgType|Type]] | [[KeyPgClass|Class]] | [[KeyPgUnion|Union]] } //typename//
Deletions:
{ [[KeyPgType|Type]] | [[KeyPgClass|Class]] | [[KeyPgUnion|Union]] | [[KeyPgEnum|Enum]] } //typename//


Revision [21585]

Edited on 2016-09-04 01:49:51 by fxm [Added the implicit compiler call to the Cast() As String operator on the Print keyword]
Additions:
In addition to its explicit form usage ##Cast( [[DataType|datatype]], //expression// )##, the ##**Cast**## ##[[KeyPgOperator|operator]]## allows also implicit conversion into ##[[DataType|datatype]]## (see same example 1 below). For this not explicit usage, the compiler may decide which cast overload to call based on how the object is used (for example on the Print keyword, the compiler calls the ##Cast() As String## operator if defined).
Deletions:
In addition to its explicit form usage ##Cast( [[DataType|datatype]], //expression// )##, the ##**Cast**## ##[[KeyPgOperator|operator]]## allows also implicit conversion into ##[[DataType|datatype]]## (see same example 1 below). For this not explicit usage, the compiler may decide which cast overload to call based on how the object is used.


Revision [21422]

Edited on 2016-04-28 16:02:15 by fxm [Added last example]
Additions:
Such a ##**Cast**## operator code will induce an infinite loop when called, unless an implicit conversion from ##//typename//## to ##//datatype//## already exists through a matched constructor (or a let operator) for ##//datatype//##, so with a higher priority (see example 5 below).
Workarounds for the not working syntax: ##//UDT_instance// = Cast( //UDT//, //built_in_type_variable// )##:
Conversion from UDT1 into UDT2, by using in UDT1 a Cast operator exiting by ##//Return This//##, but without infinite loop thanks to a matched Let operator in UDT2:
{{fbdoc item="filename" value="examples/manual/casting/opcast5.bas"}}%%(freebasic)
Declare Operator Let (ByRef u As _UDT1)
Print "UDT1.Cast() As UDT2"
Return This '' implicit conversion by compiler using the defined "UDT2.Let(Byref As UDT1)" operator
Operator UDT2.Let (ByRef u As UDT1)
Print "UDT2.Let(Byref As UDT1)"
Print Cast(UDT2, u1).I2
Deletions:
Such a ##**Cast**## operator code will induce an infinite loop when called, unless an implicit conversion from ##//typename//## to ##//datatype//## already exists through a matched constructor (or a let operator) for ##//datatype//##, so with a higher priority.
Workarounds for the not working syntax: ##//UDT_instance// = Cast( //UDT//, //built_in_type_variable// )##


Revision [21416]

Edited on 2016-04-24 08:33:33 by fxm [Wording]
Additions:
Dim As Integer J = Cast(Integer, u) '' construction with explicit initialization using the defined "Cast() As Integer" operator
Dim As Integer K = u '' construction with implicit initialization by compiler using the defined "Cast() As Integer" operator
Dim As String S = Cast(String, u) '' construction with explicit initialization using the defined "Cast() As String" operator
Dim As String G = u '' construction with implicit initialization by compiler using the defined "Cast() As String" operator
Deletions:
Dim As Integer J = Cast(Integer, u) '' construction with explicit initializer using the defined "Cast() As Integer" operator
Dim As Integer K = u '' construction with implicit initializer by compiler using the defined "Cast() As Integer" operator
Dim As String S = Cast(String, u) '' construction with explicit initializer using the defined "Cast() As String" operator
Dim As String G = u '' construction with implicit initializer by compiler using the defined "Cast() As String" operator


Revision [21414]

Edited on 2016-04-24 08:23:18 by fxm [Wording]
Additions:
The ##**Cast**## ##[[KeyPgOperator|operator]]## allows conversions that can also be used on the right-hand side of expressions of construction (with initializer) and assignment (see example 1 below).
For implicit conversion from one UDT into another UDT, a ##**Cast**## ##[[KeyPgOperator|operator]]## in the first UDT can replace (in second priority) a constructor and a Let operator in the second UDT (see examples 3 and 4 below).
Such a ##**Cast**## operator code will induce an infinite loop when called, unless an implicit conversion from ##//typename//## to ##//datatype//## already exists through a matched constructor (or a let operator) for ##//datatype//##, so with a higher priority.
Dim As Integer J = Cast(Integer, u) '' construction with explicit initializer using the defined "Cast() As Integer" operator
Dim As Integer K = u '' construction with implicit initializer by compiler using the defined "Cast() As Integer" operator
Dim As String S = Cast(String, u) '' construction with explicit initializer using the defined "Cast() As String" operator
Dim As String G = u '' construction with implicit initializer by compiler using the defined "Cast() As String" operator
Conversion from UDT1 into UDT2, by using a constructor and a Let operator in UDT2:
Deletions:
The ##**Cast**## ##[[KeyPgOperator|operator]]## allows conversions that can also be used on the right-hand side of expressions of copy-construction and assignment (see example 1 below).
For implicit conversion from one UDT into another UDT, a ##**Cast**## ##[[KeyPgOperator|operator]]## in the first UDT can replace (in second priority) a copy-constructor and a Let operator in the second UDT (see examples 3 and 4 below).
Such a ##**Cast**## operator code will induce an infinite loop when called, unless an implicit conversion from ##//typename//## to ##//datatype//## already exists through a copy constructor (or a let operator), so with a higher priority.
Dim As Integer J = Cast(Integer, u) '' explicit copy-construction using the defined "Cast() As Integer" operator
Dim As Integer K = u '' implicit copy-construction by compiler using the defined "Cast() As Integer" operator
Dim As String S = Cast(String, u) '' explicit copy-construction using the defined "Cast() As String" operator
Dim As String G = u '' implicit copy-construction by compiler using the defined "Cast() As String" operator
Conversion from UDT1 into UDT2, by using a copy-constructor and a Let operator in UDT2:


Revision [21413]

Edited on 2016-04-24 06:10:29 by fxm [Added warning on risks of infinite loop]
Additions:
Such a ##**Cast**## operator code will induce an infinite loop when called, unless an implicit conversion from ##//typename//## to ##//datatype//## already exists through a copy constructor (or a let operator), so with a higher priority.
Deletions:
Such a ##**Cast**## operator code will induce an infinite loop when called, unless an implicit conversion from ##//typename//## to ##//datatype//## already exists by using a copy constructor (or a let operator), so with a higher priority.


Revision [21412]

Edited on 2016-04-24 04:29:50 by fxm [Added warning on risks of infinite loop]
Additions:
**Warning:** For a ##**Cast**## operator that returns by value (no byref), do not use generally an exit code like ##Return //expression//## (or ##Operator = //expression//##) if ##//expression//## is an instance of ##//typename//##.
Such a ##**Cast**## operator code will induce an infinite loop when called, unless an implicit conversion from ##//typename//## to ##//datatype//## already exists by using a copy constructor (or a let operator), so with a higher priority.
Deletions:
**Warning:** For a ##**Cast**## operator that returns by value (no byref), do not use generally an exit code like ##Return //expression//## or ##Operator = //expression//## if ##//expression//## is an instance of ##//typename//##.
Such a ##**Cast**## operator code will induce an infinite loop when called, unless an implicit conversion from ##//typename//## to ##//datatype//## already exists by using a copy constructor or a let operator (so with a higher priority).


Revision [21411]

Edited on 2016-04-24 04:24:05 by fxm [Added warning on risks of infinite loop]
Additions:
**Warning:** For a ##**Cast**## operator that returns by value (no byref), do not use generally an exit code like ##Return //expression//## or ##Operator = //expression//## if ##//expression//## is an instance of ##//typename//##.
Deletions:
**Warning:** For a ##**Cast**## operator that returns by value (no byref), do not use generally an exit code like ##Return //expression//## (or ##Operator = //expression//##) if ##//expression//## is an instance of ##//typename//##.


Revision [21410]

Edited on 2016-04-23 15:29:31 by fxm [Added warning on risks of infinite loop]
Additions:
Such a ##**Cast**## operator code will induce an infinite loop when called, unless an implicit conversion from ##//typename//## to ##//datatype//## already exists by using a copy constructor or a let operator (so with a higher priority).
Deletions:
Such a ##**Cast**## operator will induce an infinite loop when called, unless an implicit conversion from ##//typename//## to ##//datatype//## already exists thanks to a copy constructor or a let operator, so with a higher priority.


Revision [21409]

Edited on 2016-04-23 15:23:22 by fxm [Added warning on risks of infinite loop]
Additions:
**Warning:** For a ##**Cast**## operator that returns by value (no byref), do not use generally an exit code like ##Return //expression//## (or ##Operator = //expression//##) if ##//expression//## is an instance of ##//typename//##.
Such a ##**Cast**## operator will induce an infinite loop when called, unless an implicit conversion from ##//typename//## to ##//datatype//## already exists thanks to a copy constructor or a let operator, so with a higher priority.
Deletions:
**Warning:** For a ##**Cast**## operator that returns by value (no byref), does not use an exit code like ##Return //expression//## (or ##Operator = //expression//##) if ##//expression//## is an instance of ##//typename//##.
This assumes that there is already an implicit conversion from ##//typename//## to ##//datatype//##, and therefore overloading with an explicit conversion operator is generally useless.
Besides, there is a high risk that overloading the implicit conversion with an explicit conversion operator induces an infinite loop when called (except for case of implicit conversion through copy constructor or let operator).


Revision [21408]

Edited on 2016-04-23 10:07:13 by fxm [Added warning on risks of infinite loop.]
Additions:
This assumes that there is already an implicit conversion from ##//typename//## to ##//datatype//##, and therefore overloading with an explicit conversion operator is generally useless.
Besides, there is a high risk that overloading the implicit conversion with an explicit conversion operator induces an infinite loop when called (except for case of implicit conversion through copy constructor or let operator).
Deletions:
This assumes that there is already an implicit conversion from ##//typename//## to ##//datatype//##, and therefore overloading with an explicit conversion operator is useless.
Besides, there is a high risk that overloading with an explicit conversion operator induces an infinite loop when called (except for case of implicit conversion through copy constructor or let operator).


Revision [21407]

Edited on 2016-04-23 09:52:01 by fxm [Added warning on risks of infinite loop]
Additions:
**Warning:** For a ##**Cast**## operator that returns by value (no byref), does not use an exit code like ##Return //expression//## (or ##Operator = //expression//##) if ##//expression//## is an instance of ##//typename//##.
This assumes that there is already an implicit conversion from ##//typename//## to ##//datatype//##, and therefore overloading with an explicit conversion operator is useless.
Besides, there is a high risk that overloading with an explicit conversion operator induces an infinite loop when called (except for case of implicit conversion through copy constructor or let operator).


Revision [21402]

Edited on 2016-04-18 09:31:43 by fxm [Wording]
Additions:
For implicit conversion from one UDT into another UDT, a ##**Cast**## ##[[KeyPgOperator|operator]]## in the first UDT can replace (in second priority) a copy-constructor and a Let operator in the second UDT (see examples 3 and 4 below).
Deletions:
For conversion from one UDT into another UDT, a ##**Cast**## ##[[KeyPgOperator|operator]]## in the first UDT can replace (in second priority) a copy-constructor and a Let operator in the second UDT (see examples 3 and 4 below).


Revision [21401]

Edited on 2016-04-17 13:26:55 by fxm [Typo]
Additions:
**Note:** The overloaded ##**Cast**## ##[[KeyPgOperator|operator]]## can convert only a UDT instance (not a built-in type variable) because it can be overloaded only as a member ##[[KeyPgOperator|operator]]## (not as a global operator).
Deletions:
**Note:** The overloaded ##**Cast**## ##[[KeyPgOperator|operator]]## can convert only a UDT instance (a not a built-in type variable) because it can be overloaded only as a member ##[[KeyPgOperator|operator]]## (not as a global operator).


Revision [21400]

Edited on 2016-04-17 07:54:43 by fxm [Wording]
Additions:
The name of the type for conversion into it, a built-in data type (standard data type) or a UDT different from ##//typename//##
(using constructor, Let operator, and a Cast operator which returns by reference)
Conversion from UDT1 into UDT2, by using a copy-constructor and a Let operator in UDT2:
Conversion from UDT1 into UDT2, by using a Cast operator in UDT1:
Deletions:
The name of the type for conversion into it (a built-in data type or a UDT different from ##//typename//##)
(using constructor, Let operator, Cast operator with return by reference)
Conversion from UDT1 into UDT2, by using a copy-constructor and a Let operator in the UDT2:
Conversion from UDT1 into UDT2, by using a Cast operator in the UDT1:


Revision [21394]

Edited on 2016-04-16 16:04:09 by fxm [Added two other examples]
Additions:
The ##**Cast**## ##[[KeyPgOperator|operator]]## allows conversions that can also be used on the right-hand side of expressions of copy-construction and assignment (see example 1 below).
In addition to its explicit form usage ##Cast( [[DataType|datatype]], //expression// )##, the ##**Cast**## ##[[KeyPgOperator|operator]]## allows also implicit conversion into ##[[DataType|datatype]]## (see same example 1 below). For this not explicit usage, the compiler may decide which cast overload to call based on how the object is used.
(see example 2 below)
For conversion from one UDT into another UDT, a ##**Cast**## ##[[KeyPgOperator|operator]]## in the first UDT can replace (in second priority) a copy-constructor and a Let operator in the second UDT (see examples 3 and 4 below).
Workarounds for the not working syntax: ##//UDT_instance// = Cast( //UDT//, //built_in_type_variable// )##
Conversion from UDT1 into UDT2, by using a copy-constructor and a Let operator in the UDT2:
{{fbdoc item="filename" value="examples/manual/casting/opcast3.bas"}}%%(freebasic)
Type _UDT1 As UDT1
Type UDT2
Dim As Integer I2
Declare Constructor (Byref u As _UDT1)
Declare Operator Let (Byref u As _UDT1)
Constructor UDT2 ()
Type UDT1
Dim As Integer I1
Constructor UDT2 (Byref u As UDT1)
Print "UDT2.Constructor(Byref As UDT1)",
This.I2 = u.I1
Operator UDT2.Let (Byref u As UDT1)
Print "UDT2.Let(Byref As UDT1)",,
This.I2 = u.I1
Dim As UDT1 u1
u1.I1 = 123
Dim As UDT2 u2 = u1 '' implicit conversion by compiler using the defined "UDT2.Constructor(Byref As UDT1)"
Print u2.I2
u1.I1 = 456
u2 = u1 '' implicit conversion by compiler using the defined "UDT2.Let(Byref As UDT1)" operator
Print u2.I2
Conversion from UDT1 into UDT2, by using a Cast operator in the UDT1:
{{fbdoc item="filename" value="examples/manual/casting/opcast4.bas"}}%%(freebasic)
Type UDT2
Dim As Integer I2
Type UDT1
Dim As Integer I1
Declare Operator Cast () As UDT2
Operator UDT1.Cast () As UDT2
Print "UDT1.Cast() As UDT2",,
Dim As UDT2 u
u.I2 = This.I1
Return u
Dim As UDT1 u1
u1.I1 = 123
Dim As UDT2 u2 = u1 '' implicit conversion by compiler using the defined "UDT1.Cast() As UDT2" operator
Print u2.I2
u1.I1 = 456
u2 = u1 '' implicit conversion by compiler using the defined "UDT1.Cast() As UDT2" operator
Print u2.I2
Deletions:
The ##**Cast**## ##[[KeyPgOperator|operator]]## allows conversions that can also be used on the right-hand side of expressions of copy-construction and assignment (see example below).
In addition to its explicit form usage ##Cast( [[DataType|datatype]], //expression// )##, the ##**Cast**## ##[[KeyPgOperator|operator]]## allows also implicit conversion into ##[[DataType|datatype]]## (see same example below). For this not explicit usage, the compiler may decide which cast overload to call based on how the object is used.
For conversion from one UDT into another UDT, a ##**Cast**## ##[[KeyPgOperator|operator]]## in the first UDT can replace (in second priority) a copy-constructor and a Let operator in the second UDT.
(see other example below)

Workarounds for the not working: syntax ##//UDT_instance// = Cast( //UDT//, //built_in_type_variable// )##


Revision [21393]

Edited on 2016-04-16 15:17:18 by fxm [Rewording]
Additions:
For conversion from one UDT into another UDT, a ##**Cast**## ##[[KeyPgOperator|operator]]## in the first UDT can replace (in second priority) a copy-constructor and a Let operator in the second UDT.
Deletions:
For conversion from one UDT into another UDT, a ##**Cast**## ##[[KeyPgOperator|operator]]## of the first UDT can replace (in second priority) a copy-constructor and a Let operator of the second UDT.


Revision [21392]

Edited on 2016-04-16 15:11:00 by fxm [Rewording]
Additions:
Operator to convert a UDT (User Defined Type) variable into a specified data type
The name of the type for conversion into it (a built-in data type or a UDT different from ##//typename//##)
The ##**Cast**## ##[[KeyPgOperator|operator]]## allows conversions that can also be used on the right-hand side of expressions of copy-construction and assignment (see example below).
In addition to its explicit form usage ##Cast( [[DataType|datatype]], //expression// )##, the ##**Cast**## ##[[KeyPgOperator|operator]]## allows also implicit conversion into ##[[DataType|datatype]]## (see same example below). For this not explicit usage, the compiler may decide which cast overload to call based on how the object is used.
For conversion from one UDT into another UDT, a ##**Cast**## ##[[KeyPgOperator|operator]]## of the first UDT can replace (in second priority) a copy-constructor and a Let operator of the second UDT.
**Note:** The overloaded ##**Cast**## ##[[KeyPgOperator|operator]]## can convert only a UDT instance (a not a built-in type variable) because it can be overloaded only as a member ##[[KeyPgOperator|operator]]## (not as a global operator).
To convert a built-in type variable into a UDT (not working syntax: ##Cast( //UDT//, //built_in_type_variable// )##), the best way into the UDT is to define a constructor or/and a Let operator, otherwise to use a ##**Cast**## ##[[KeyPgOperator|operator]]## but with return by reference (by an assignment: ##Cast( //built_in_datatype//, //UDT_instance// ) = //built_in_type_variable//##).
Deletions:
Operator to convert a User Defined Type variable to a specified data type
The name of the type for conversion into it (a built-in data type or a user defined type different from ##//typename//##)
The ##**Cast**## ##[[KeyPgOperator|operator]]## allows conversions that can also be used on the right-hand side of expressions of copy-construction and assignment (see example below), and can so replace (in second priority) a copy-constructor and a Let operator.
In addition to its explicit form usage ##Cast( [[DataType|datatype]], //expression// )##, the ##**Cast**## ##[[KeyPgOperator|operator]]## allows also implicit conversion to ##[[DataType|datatype]]## (see same example below). For this not explicit usage, the compiler may decide which cast overload to call based on how the object is used.
**Note:** The overloaded ##**Cast**## ##[[KeyPgOperator|operator]]## can convert only a UDT instance (not a built-in type variable) because it can be overloaded only as a member ##[[KeyPgOperator|operator]]## (not as a global operator). To convert a built-in type variable into a UDT (not working syntax: ##Cast( //UDT//, //built_in_type_variable// )##), the better way is to define in UDT a constructor or/and a Let operator, otherwise to use a Cast operator but with return by reference (assignment: ##Cast( //built_in_datatype//, //UDT_instance// ) = //built_in_type_variable//##).


Revision [21391]

Edited on 2016-04-16 14:11:45 by fxm [Updated examples]
Additions:
Print "UDT.Cast() As Integer",
Print "UDT.Cast() As String",
Declare Constructor ()
Declare Constructor (ByVal I0 As Integer)
Declare Operator Let (ByVal I0 As Integer)
Declare Operator Cast () ByRef As Integer
Constructor UDT ()
Constructor UDT (ByVal I0 As Integer)
Print "UDT.Constructor(Byval As Integer)",
Operator UDT.Let (ByVal I0 As Integer)
Print "UDT.Let(Byval As Integer)",,
Operator UDT.Cast () ByRef As Integer
Print "UDT.Cast() Byref As Integer",,
u = UDT(34) '' explicit conversion using the defined "Constructor(Byval As Integer)"
u = 56 '' implicit conversion by compiler using the defined "Let(Byval As Integer)" operator
Cast(Integer, u) = 78 '' explicit conversion using the defined "Cast() Byref As Integer" operator with byref return
Deletions:
Declare Constructor (Byval I0 As Integer = 0)
Declare Operator Let (Byval I0 As Integer)
Declare Operator Cast () Byref As Integer
Constructor UDT (Byval I0 As Integer = 0)
Operator UDT.Let (Byval I0 As Integer)
Operator UDT.Cast () Byref As Integer
u = UDT(34) '' conversion using the defined constructor
u = 56 '' conversion using the defined Let operator
Cast(Integer, u) = 78 '' conversion using the defined Cast operator with byref return


Revision [21390]

Edited on 2016-04-16 11:11:46 by fxm [Added second example]
Additions:
In addition to its explicit form usage ##Cast( [[DataType|datatype]], //expression// )##, the ##**Cast**## ##[[KeyPgOperator|operator]]## allows also implicit conversion to ##[[DataType|datatype]]## (see same example below). For this not explicit usage, the compiler may decide which cast overload to call based on how the object is used.
(see other example below)
Deletions:
In addition to its explicit form usage ##Cast( [[DataType|datatype]], //expression// )##, the ##**Cast**## ##[[KeyPgOperator|operator]]## allows also implicit conversion to ##[[DataType|datatype]]## (see also example below). For this not explicit usage, the compiler may decide which cast overload to call based on how the object is used.


Revision [21389]

Edited on 2016-04-16 11:05:57 by fxm [Added second example]
Additions:
{{fbdoc item="filename" value="examples/manual/casting/opcast1.bas"}}%%(freebasic)
Workarounds for the not working: syntax ##//UDT_instance// = Cast( //UDT//, //built_in_type_variable// )##
(using constructor, Let operator, Cast operator with return by reference)
{{fbdoc item="filename" value="examples/manual/casting/opcast2.bas"}}%%(freebasic)
As Integer I
Declare Constructor (Byval I0 As Integer = 0)
Declare Operator Let (Byval I0 As Integer)
Declare Operator Cast () Byref As Integer
Constructor UDT (Byval I0 As Integer = 0)
This.I = I0
End Constructor
Operator UDT.Let (Byval I0 As Integer)
This.I = I0
Operator UDT.Cast () Byref As Integer
'u = Cast(UDT, 12) '' unsupported - error 20: Type mismatch
u = UDT(34) '' conversion using the defined constructor
Print u.I
u = 56 '' conversion using the defined Let operator
Print u.I
Cast(Integer, u) = 78 '' conversion using the defined Cast operator with byref return
Print u.I
Deletions:
{{fbdoc item="filename" value="examples/manual/casting/opcast.bas"}}%%(freebasic)


Revision [21388]

Edited on 2016-04-16 10:28:26 by fxm [Complemented note]
Additions:
**Note:** The overloaded ##**Cast**## ##[[KeyPgOperator|operator]]## can convert only a UDT instance (not a built-in type variable) because it can be overloaded only as a member ##[[KeyPgOperator|operator]]## (not as a global operator). To convert a built-in type variable into a UDT (not working syntax: ##Cast( //UDT//, //built_in_type_variable// )##), the better way is to define in UDT a constructor or/and a Let operator, otherwise to use a Cast operator but with return by reference (assignment: ##Cast( //built_in_datatype//, //UDT_instance// ) = //built_in_type_variable//##).
Deletions:
**Note:** The overloaded ##**Cast**## ##[[KeyPgOperator|operator]]## can convert only a UDT instance (not a built-in variable) because it can be overloaded only as a member ##[[KeyPgOperator|operator]]## (not as a global operator).


Revision [21387]

Edited on 2016-04-16 09:17:56 by fxm [Rewording]
Additions:
The ##**Cast**## ##[[KeyPgOperator|operator]]## allows conversions that can also be used on the right-hand side of expressions of copy-construction and assignment (see example below), and can so replace (in second priority) a copy-constructor and a Let operator.
In addition to its explicit form usage ##Cast( [[DataType|datatype]], //expression// )##, the ##**Cast**## ##[[KeyPgOperator|operator]]## allows also implicit conversion to ##[[DataType|datatype]]## (see also example below). For this not explicit usage, the compiler may decide which cast overload to call based on how the object is used.
Deletions:
In addition to its explicit form usage ##Cast( [[DataType|datatype]], //expression// )##, the ##**Cast**## ##[[KeyPgOperator|operator]]## allows also implicit conversion to ##[[DataType|datatype]]## (see example below). For this not explicit usage, the compiler may decide which cast overload to call based on how the object is used.


Revision [21385]

Edited on 2016-04-16 06:44:39 by fxm [Refined definition of parameters]
Additions:
The name of the type for conversion into it (a built-in data type or a user defined type different from ##//typename//##)
The expression to convert, an instance of ##//typename//##
**Note:** The overloaded ##**Cast**## ##[[KeyPgOperator|operator]]## can convert only a UDT instance (not a built-in variable) because it can be overloaded only as a member ##[[KeyPgOperator|operator]]## (not as a global operator).
Deletions:
The name of the type for conversion into it (different from ##//typename//##)
The expression to convert, an object of ##//typename//##
**Note:** The overloaded ##**Cast**## ##[[KeyPgOperator|operator]]## can convert only a UDT instance because it can be overloaded only as a member ##[[KeyPgOperator|operator]]##.


Revision [21379]

Edited on 2016-04-16 03:17:04 by fxm [Created new page "Operator CAST" for overloading CAST]
Additions:
{{fbdoc item="title" value="Operator CAST"}}----
Operator to convert a User Defined Type variable to a specified data type
{ [[KeyPgType|Type]] | [[KeyPgClass|Class]] | [[KeyPgUnion|Union]] | [[KeyPgEnum|Enum]] } //typename//
[[KeyPgDeclare|declare]] [[KeyPgOperator|operator]] **cast** () [ [[KeyPgByrefFunction|byref]] ] [[KeyPgAs|as]] [[DataType|datatype]]
...
End { [[KeyPgType|Type]] | [[KeyPgClass|Class]] | [[KeyPgUnion|Union]] }
[[KeyPgOperator|operator]] //typename//.**cast** () [ [[KeyPgByrefFunction|byref]] ] [[KeyPgAs|as]] [[DataType|datatype]] [ [[KeyPgExport|Export]] ]
...
{{fbdoc item="usage"}}##
{{fbdoc item="param"}}
##//typename//##
The name of the ##[[KeyPgType|Type]]##, ##[[KeyPgClass|Class]]##, or ##[[KeyPgUnion|Union]]##
##//datatype//##
The name of the type for conversion into it (different from ##//typename//##)
##//expression//##
The expression to convert, an object of ##//typename//##
Converts an ##//expression//## (a ##//typename//## variable) into a different ##[[DataType|datatype]]##.
##**Cast**## ##[[KeyPgOperator|operator]]## must be declared inside the ##[[KeyPgType|Type]]##, ##[[KeyPgClass|Class]]##, or ##[[KeyPgUnion|Union]]##.
As all non-static member procedures, it has passed a hidden ##[[KeyPgThis|this]]## parameter.
The ##**Cast**## ##[[KeyPgOperator|operator]]## is the only operator (or function) that can be declared multiple times when only the return type differs, but not the same as the ##[[KeyPgType|Type]]##, ##[[KeyPgClass|Class]]##, or ##[[KeyPgUnion|Union]]## they are declared in.
In addition to its explicit form usage ##Cast( [[DataType|datatype]], //expression// )##, the ##**Cast**## ##[[KeyPgOperator|operator]]## allows also implicit conversion to ##[[DataType|datatype]]## (see example below). For this not explicit usage, the compiler may decide which cast overload to call based on how the object is used.
**Note:** The overloaded ##**Cast**## ##[[KeyPgOperator|operator]]## can convert only a UDT instance because it can be overloaded only as a member ##[[KeyPgOperator|operator]]##.
Very simple syntaxic example highlighting the conversion capabilities (explicit and implicit) by using Cast operators:
{{fbdoc item="filename" value="examples/manual/casting/opcast.bas"}}%%(freebasic)
Type UDT
Dim As Integer I
Declare Operator Cast () As Integer
Declare Operator Cast () As String
End Type
Operator UDT.Cast () As Integer
Return This.I
End Operator
Operator UDT.Cast () As String
Return Str(This.I)
End Operator
Dim As UDT u
u.I = 12
Print Cast(Integer, u) '' explicit conversion using the defined "Cast() As Integer" operator
Print Cast(String, u) '' explicit conversion using the defined "Cast() As String" operator
Print u '' implicit conversion by compiler using the defined "Cast() As String" operator
Print
u.I = 34
Dim As Integer J = Cast(Integer, u) '' explicit copy-construction using the defined "Cast() As Integer" operator
Print J
Dim As Integer K = u '' implicit copy-construction by compiler using the defined "Cast() As Integer" operator
Print K
Print
u.I = 56
J = Cast(Integer, u) '' explicit assignment using the defined "Cast() As Integer" operator
Print J
K = u '' implicit assignment by compiler using the defined "Cast() As Integer" operator
Print K
Print
u.I = 78
Dim As String S = Cast(String, u) '' explicit copy-construction using the defined "Cast() As String" operator
Print S
Dim As String G = u '' implicit copy-construction by compiler using the defined "Cast() As String" operator
Print G
Print
u.I = 90
S = Cast(String, u) '' explicit assignment using the defined "Cast() As String" operator
Print S
G = u '' implicit assignment by compiler using the defined "Cast() As String" operator
Print G
Print
Sleep
- Only available in the //[[CompilerOptlang|-lang fb]]// dialect.
- ##[[KeyPgCast|Cast]]##
{{fbdoc item="back" value="CatPgOpIndex|Operator List"}}
Deletions:
{{fbdoc item="title" value="CAST"}}----
Converts an expression to a specified data type
Converts ##//expression//## into a different ##[[DataType|datatype]]##. Useful to be used in macros when ##[[DataType|datatype]]## is unknown and also when converting to [[KeyPgTypeAlias|Type Alias]].
Note: this is a general form of conversion operators such as ##[[KeyPgCint|Cint]]## or ##[[KeyPgCdbl|Cdbl]]##. They are more versatile because they can be used on types that have a ##[[KeyPgOperator|Cast operator]]##, but don't have a built-in keyword for it. e.g. ##**Cast**( //my_type//, //expr//)##. They are also suitable for use in cases where the type of a variable is not fixed in the code - for example, it might be ##[[KeyPgPpdefine|Define]]##d earlier, or may be the ##[[KeyPgTypeof|Type of]]## a different variable or expression.
Note: If you want to use an operator specifically for converting to different types of ##[[KeyPgPointer|Pointer]]##s, consider using ##[[KeyPgCptr|Cptr]]## instead.
{{fbdoc item="filename" value="examples/manual/casting/cast.bas"}}%%(freebasic)
'' will print -128 because the integer literal will be converted to a signed Byte
'' (this Casting operation is equivalent to using CByte)
print cast( byte, &h0080 )
'' will print 3 because the floating-point value will be converted to an Integer
'' (this Casting operator is equivalent to using CInt)
print cast( integer, 3.1 )
- Not available in the //[[CompilerOptlang|-lang qb]]// dialect unless referenced with the alias ##**""__Cast""**##.
{{fbdoc item="back" value="CatPgCasting|Converting Data Types"}}


Revision [21378]

The oldest known version of this page was created on 2016-04-16 03:07:48 by fxm [Created new page "Operator CAST" for overloading CAST]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode