Operators: Reference
editOperators: Reference
editMethod Call
editUse the method call operator '()'
to call a member method on a
reference type value. Implicit
boxing/unboxing is evaluated as necessary per argument
during the method call. When a method call is made on a target def
type value,
the parameters and return type value are considered to also be of the def
type
and are evaluated at run-time.
An overloaded method is one that shares the same name with two or more methods. A method is overloaded based on arity where the same name is re-used for multiple methods as long as the number of parameters differs.
Errors
-
If the reference type value is
null
. - If the member method name doesnβt exist for a given reference type value.
- If the number of arguments passed in is different from the number of specified parameters.
- If the arguments cannot be implicitly cast or implicitly boxed/unboxed to the correct type values for the parameters.
Grammar
method_call: '.' ID arguments; arguments: '(' (expression (',' expression)*)? ')';
Examples
-
Method calls on different reference types.
Map m = new HashMap(); m.put(1, 2); int z = m.get(1); def d = new ArrayList(); d.add(1); int i = Integer.parseInt(d.get(0).toString());
declare
Map m
; allocateHashMap
instance βHashMap reference
; storeHashMap reference
tom
load from
m
βMap reference
; implicit castint 1
todef
βdef
; implicit castint 2
todef
βdef
; callput
onMap reference
with arguments (int 1
,int 2
)declare
int z
; load fromm
βMap reference
; callget
onMap reference
with arguments (int 1
) βdef
; implicit castdef
toint 2
βint 2
; storeint 2
toz
declare
def d
; allocateArrayList
instance βArrayList reference
; implicit castArrayList
todef
βdef
; storedef
tod
load from
d
βdef
; implicit castdef
toArrayList reference
βArrayList reference
calladd
onArrayList reference
with arguments (int 1
);declare
int i
; load fromd
βdef
; implicit castdef
toArrayList reference
βArrayList reference
callget
onArrayList reference
with arguments (int 1
) βdef
; implicit castdef
toInteger 1 reference
βInteger 1 reference
; calltoString
onInteger 1 reference
βString '1'
; callparseInt
onInteger
with arguments (String '1'
) βint 1
; storeint 1
ini
;
Field Access
editUse the field access operator '.'
to store a value to or load a value from a
reference type member field.
Errors
-
If the reference type value is
null
. - If the member field name doesnβt exist for a given reference type value.
Grammar
field_access: '.' ID;
Examples
The examples use the following reference type definition:
name: Example non-static member fields: * int x * def y * List z
-
Field access with the
Example
type.Example example = new Example(); example.x = 1; example.y = example.x; example.z = new ArrayList(); example.z.add(1); example.x = example.z.get(0);
declare
Example example
; allocateExample
instance βExample reference
; storeExample reference
toexample
load from
example
βExample reference
; storeint 1
tox
ofExample reference
load from
example
βExample reference @0
; load fromexample
βExample reference @1
; load fromx
ofExample reference @1
βint 1
; implicit castint 1
todef
βdef
; storedef
toy
ofExample reference @0
; (noteExample reference @0
andExample reference @1
are the same)load from
example
βExample reference
; allocateArrayList
instance βArrayList reference
; implicit castArrayList reference
toList reference
βList reference
; storeList reference
toz
ofExample reference
load from
example
βExample reference
; load fromz
ofExample reference
βList reference
; calladd
onList reference
with arguments (int 1
)load from
example
βExample reference @0
; load fromexample
βExample reference @1
; load fromz
ofExample reference @1
βList reference
; callget
onList reference
with arguments (int 0
) βint 1
; storeint 1
inx
ofList reference @0
; (noteExample reference @0
andExample reference @1
are the same)
Null Safe
editUse the null safe operator '?.'
instead of the method call operator or field
access operator to ensure a reference type value is non-null
before
a method call or field access. A null
value will be returned if the reference
type value is null
, otherwise the method call or field access is evaluated.
Errors
- If the method call return type value or the field access type value is not a reference type value and is not implicitly castable to a reference type value.
Grammar
null_safe: null_safe_method_call | null_safe_field_access ; null_safe_method_call: '?.' ID arguments; arguments: '(' (expression (',' expression)*)? ')'; null_safe_field_access: '?.' ID;
Examples
The examples use the following reference type definition:
name: Example non-static member methods: * List factory() non-static member fields: * List x
-
Null safe without a
null
value. -
Null safe with a
null
value;
List Initialization
editUse the list initialization operator '[]'
to allocate an List
type instance
to the heap with a set of pre-defined values. Each value used to initialize the
List
type instance is cast to a def
type value upon insertion into the
List
type instance using the add
method. The order of the specified values
is maintained.
Grammar
list_initialization: '[' expression (',' expression)* ']' | '[' ']';
Examples
-
List initialization of an empty
List
type value. -
List initialization with static values.
declare
List list
; allocateArrayList
instance βArrayList reference
; calladd
onArrayList reference
with arguments(int 1
); calladd
onArrayList reference
with arguments(int 2
); calladd
onArrayList reference
with arguments(int 3
); implicit castArrayList reference
toList reference
βList reference
; storeList reference
tolist
-
List initialization with non-static values.
declare
int i
; storeint 1
toi
declare
long l
; storelong 2
tol
declare
float f
; storefloat 3.0
tof
declare
double d
; storedouble 4.0
tod
declare
String s
; storeString "5"
tos
declare
List list
; allocateArrayList
instance βArrayList reference
; load fromi
βint 1
; calladd
onArrayList reference
with arguments(int 1
); load froml
βlong 2
; calladd
onArrayList reference
with arguments(long 2
); load fromf
βfloat 3.0
; load fromd
βdouble 4.0
; promotefloat 3.0
anddouble 4.0
: resultdouble
; implicit castfloat 3.0
todouble 3.0
βdouble 3.0
; multiplydouble 3.0
anddouble 4.0
βdouble 12.0
; calladd
onArrayList reference
with arguments(double 12.0
); load froms
βString "5"
; calladd
onArrayList reference
with arguments(String "5"
); implicit castArrayList reference
toList reference
βList reference
; storeList reference
tolist
List Access
editUse the list access operator '[]'
as a shortcut for a set
method call or
get
method call made on a List
type value.
Errors
-
If a value other than a
List
type value is accessed. -
If a non-integer type value is used as an index for a
set
method call orget
method call.
Grammar
list_access: '[' expression ']'
Examples
-
List access with the
List
type.List list = new ArrayList(); list.add(1); list.add(2); list.add(3); list[0] = 2; list[1] = 5; int x = list[0] + list[1]; int y = 1; int z = list[y];
declare
List list
; allocateArrayList
instance βArrayList reference
; implicit castArrayList reference
toList reference
βList reference
; storeList reference
tolist
load from
list
βList reference
; calladd
onList reference
with arguments(int 1
)load from
list
βList reference
; calladd
onList reference
with arguments(int 2
)load from
list
βList reference
; calladd
onList reference
with arguments(int 3
)load from
list
βList reference
; callset
onList reference
with arguments(int 0
,int 2
)load from
list
βList reference
; callset
onList reference
with arguments(int 1
,int 5
)declare
int x
; load fromlist
βList reference
; callget
onList reference
with arguments(int 0
) βdef
; implicit castdef
toint 2
βint 2
; load fromlist
βList reference
; callget
onList reference
with arguments(int 1
) βdef
; implicit castdef
toint 5
βint 5
; addint 2
andint 5
βint 7
; storeint 7
tox
declare
int y
; storeint 1
inty
declare
int z
; load fromlist
βList reference
; load fromy
βint 1
; callget
onList reference
with arguments(int 1
) βdef
; implicit castdef
toint 5
βint 5
; storeint 5
toz
-
List access with the
def
type.def d = new ArrayList(); d.add(1); d.add(2); d.add(3); d[0] = 2; d[1] = 5; def x = d[0] + d[1]; def y = 1; def z = d[y];
declare
List d
; allocateArrayList
instance βArrayList reference
; implicit castArrayList reference
todef
βdef
; storedef
tod
load from
d
βdef
; implicit castdef
toArrayList reference
βArrayList reference
; calladd
onArrayList reference
with arguments(int 1
)load from
d
βdef
; implicit castdef
toArrayList reference
βArrayList reference
; calladd
onArrayList reference
with arguments(int 2
)load from
d
βdef
; implicit castdef
toArrayList reference
βArrayList reference
; calladd
onArrayList reference
with arguments(int 3
)load from
d
βdef
; implicit castdef
toArrayList reference
βArrayList reference
; callset
onArrayList reference
with arguments(int 0
,int 2
)load from
d
βdef
; implicit castdef
toArrayList reference
βArrayList reference
; callset
onArrayList reference
with arguments(int 1
,int 5
)declare
def x
; load fromd
βdef
; implicit castdef
toArrayList reference
βArrayList reference
; callget
onArrayList reference
with arguments(int 0
) βdef
; implicit castdef
toint 2
βint 2
; load fromd
βdef
; implicit castdef
toArrayList reference
βArrayList reference
; callget
onArrayList reference
with arguments(int 1
) βdef
; implicit castdef
toint 2
βint 2
; addint 2
andint 5
βint 7
; storeint 7
tox
declare
int y
; storeint 1
inty
declare
int z
; load fromd
βArrayList reference
; load fromy
βdef
; implicit castdef
toint 1
βint 1
; callget
onArrayList reference
with arguments(int 1
) βdef
; storedef
toz
Map Initialization
editUse the map initialization operator '[:]'
to allocate a Map
type instance to
the heap with a set of pre-defined values. Each pair of values used to
initialize the Map
type instance are cast to def
type values upon insertion
into the Map
type instance using the put
method.
Grammar
map_initialization: '[' key_pair (',' key_pair)* ']' | '[' ':' ']'; key_pair: expression ':' expression
Examples
-
Map initialization of an empty
Map
type value. -
Map initialization with static values.
declare
Map map
; allocateHashMap
instance βHashMap reference
; callput
onHashMap reference
with arguments(int 1
,int 2
); callput
onHashMap reference
with arguments(int 3
,int 4
); callput
onHashMap reference
with arguments(int 5
,int 6
); implicit castHashMap reference
toMap reference
βMap reference
; storeMap reference
tomap
-
Map initialization with non-static values.
byte b = 0; int i = 1; long l = 2L; float f = 3.0F; double d = 4.0; String s = "5"; Map map = [b:i, l:f*d, d:s];
declare
byte b
; storebyte 0
tob
declare
int i
; storeint 1
toi
declare
long l
; storelong 2
tol
declare
float f
; storefloat 3.0
tof
declare
double d
; storedouble 4.0
tod
declare
String s
; storeString "5"
tos
declare
Map map
; allocateHashMap
instance βHashMap reference
; load fromb
βbyte 0
; load fromi
βint 1
; callput
onHashMap reference
with arguments(byte 0
,int 1
); load froml
βlong 2
; load fromf
βfloat 3.0
; load fromd
βdouble 4.0
; promotefloat 3.0
anddouble 4.0
: resultdouble
; implicit castfloat 3.0
todouble 3.0
βdouble 3.0
; multiplydouble 3.0
anddouble 4.0
βdouble 12.0
; callput
onHashMap reference
with arguments(long 2
,double 12.0
); load fromd
βdouble 4.0
; load froms
βString "5"
; callput
onHashMap reference
with arguments(double 4.0
,String "5"
); implicit castHashMap reference
toMap reference
βMap reference
; storeMap reference
tomap
Map Access
editUse the map access operator '[]'
as a shortcut for a put
method call or
get
method call made on a Map
type value.
Errors
-
If a value other than a
Map
type value is accessed.
Grammar
map_access: '[' expression ']'
Examples
-
Map access with the
Map
type.Map map = new HashMap(); map['value2'] = 2; map['value5'] = 5; int x = map['value2'] + map['value5']; String y = 'value5'; int z = x[z];
declare
Map map
; allocateHashMap
instance βHashMap reference
; implicit castHashMap reference
toMap reference
βMap reference
; storeMap reference
tomap
load from
map
βMap reference
; callput
onMap reference
with arguments(String 'value2'
,int 2
)load from
map
βMap reference
; callput
onMap reference
with arguments(String 'value5'
,int 5
)declare
int x
; load frommap
βMap reference
; callget
onMap reference
with arguments(String 'value2'
) βdef
; implicit castdef
toint 2
βint 2
; load frommap
βMap reference
; callget
onMap reference
with arguments(String 'value5'
) βdef
; implicit castdef
toint 5
βint 5
; addint 2
andint 5
βint 7
; storeint 7
tox
declare
String y
; storeString 'value5'
toy
declare
int z
; load frommap
βMap reference
; load fromy
βString 'value5'
; callget
onMap reference
with arguments(String 'value5'
) βdef
; implicit castdef
toint 5
βint 5
; storeint 5
toz
-
Map access with the
def
type.def d = new HashMap(); d['value2'] = 2; d['value5'] = 5; int x = d['value2'] + d['value5']; String y = 'value5'; def z = d[y];
declare
def d
; allocateHashMap
instance βHashMap reference
; implicit castHashMap reference
todef
βdef
; storedef
tod
load from
d
βdef
; implicit castdef
toHashMap reference
βHashMap reference
; callput
onHashMap reference
with arguments(String 'value2'
,int 2
)load from
d
βdef
; implicit castdef
toHashMap reference
βHashMap reference
; callput
onHashMap reference
with arguments(String 'value5'
,int 5
)declare
int x
; load fromd
βdef
; implicit castdef
toHashMap reference
βHashMap reference
; callget
onHashMap reference
with arguments(String 'value2'
) βdef
; implicit castdef
toint 2
βint 2
; load fromd
βdef
; callget
onHashMap reference
with arguments(String 'value5'
) βdef
; implicit castdef
toint 5
βint 5
; addint 2
andint 5
βint 7
; storeint 7
tox
declare
String y
; storeString 'value5'
toy
declare
def z
; load fromd
βdef
; load fromy
βString 'value5'
; callget
onHashMap reference
with arguments(String 'value5'
) βdef
; storedef
toz
New Instance
editUse the new instance operator 'new ()'
to allocate a
reference type instance to the heap and call a specified
constructor. Implicit boxing/unboxing is evaluated as
necessary per argument during the constructor call.
An overloaded constructor is one that shares the same name with two or more constructors. A constructor is overloaded based on arity where the same reference type name is re-used for multiple constructors as long as the number of parameters differs.
Errors
- If the reference type name doesnβt exist for instance allocation.
- If the number of arguments passed in is different from the number of specified parameters.
- If the arguments cannot be implicitly cast or implicitly boxed/unboxed to the correct type values for the parameters.
Grammar
new_instance: 'new' TYPE '(' (expression (',' expression)*)? ')';
Examples
- Allocation of new instances with different types.
declare |
|
declare |
|
declare |
String Concatenation
editUse the string concatenation operator '+'
to concatenate two values together
where at least one of the values is a String
type.
Grammar
concatenate: expression '+' expression;
Examples
-
String concatenation with different primitive types.
declare
String x
; storeString "con"
tox
;declare
String y
; load fromx
βString "con"
; concatString "con"
andString "cat"
βString "concat"
; storeString "concat"
toy
declare
String z
; addint 4
andint 5
βint 9
; concatint 9
andString "9concat"
; storeString "9concat"
toz
; (note the addition is done prior to the concatenation due to precedence and associativity of the specific operations) -
String concatenation with the
def
type.
Elvis
editAn elvis consists of two expressions. The first expression is evaluated
with to check for a null
value. If the first expression evaluates to
null
then the second expression is evaluated and its value used. If the first
expression evaluates to non-null
then the resultant value of the first
expression is used. Use the elvis operator '?:'
as a shortcut for the
conditional operator.
Errors
-
If the first expression or second expression cannot produce a
null
value.
Grammar
elvis: expression '?:' expression;
Examples
-
Elvis with different reference types.
declare
List x
; allocateArrayList
instance βArrayList reference
; implicit castArrayList reference
toList reference
βList reference
; storeList reference
tox
;declare
List y
; loadx
βList reference
;List reference
equalsnull
βfalse
; evaluate 1st expression:List reference
βList reference
; storeList reference
toy
store
null
toy
;declare
List z
; loady
βList reference
;List reference
equalsnull
βtrue
; evaluate 2nd expression: allocateArrayList
instance βArrayList reference
; implicit castArrayList reference
toList reference
βList reference
; storeList reference
toz
;