ZBasic Language Reference
82
ZBasic Microcontrollers
Example
Option Overload
' enable overloading
Sub foo(ByVal x as Integer)
Debug.Print "foo ByVal"
End Sub
Sub foo(ByRef x as Integer)
Debug.Print "foo ByRef"
End Sub
Sub foo(ByVal s as String)
Debug.Print "foo string"
End Sub
Dim i as Integer
Sub Main()
Call foo(5) ' this call is unambiguous
Call foo(i) ' this call is ambiguous
Call foo("abc") ' this call is unambiguous
End Sub
In the example above, for the first invocation of foo() in Main() the compiler determines that the first
overload of foo() having the ByVal parameter is the only one whose signature is compatible with the
invocation. This is not ambiguous because the constant value cannot be passed by reference so the
overload of foo() with the ByRef parameter cannot possibly be used.
In contrast, for the second invocation of foo(), the first two overloads are both compatible resulting in an
ambiguous reference.
3.20 Aliases
Occasionally, it is useful to be able to access a variable or parts of a variable as different types at different
times. Although this can be accomplished by using the System Library routines BlockMove() or
RamPeek()/RamPoke() it is simpler and more efficient to use the concept of an alias. Simply stated,
defining an alias tells the compiler to generate code to access a variable or part of a variable as if it were
a different type. To be clear, no new data space is allocated by defining an alias. It simply provides a
different way of accessing previously defined space.
The syntax for defining an alias is similar to that for defining a variable. For example, the syntax for
defining an alias at the module level is shown below.
{Public | Private | Dim} <name>[(<dim-list>)] As <type> Alias <var-ref>
As with normal variables, Dim has exactly the same effect as Private. Within a subroutine, a function
or any block structure, a local alias may be defined using the syntax shown below.
Dim <name>[(<dim-list>)] As <type> Alias <var-ref>
In both cases the <var-ref> element is the name of a RAM variable or the name of another alias
optionally including a parenthesized set of one or more constant index expressions. The parenthesized
index list is only allowed, of course, if the referent item is an array.
|