Feather2 / documentation / Terminology.md
Terminology
Array
An ordered collection of data, which can be accessed by this index
.
Arrays have a fixed size. They can neither grow, nor shrink.
val myArray = arrayOf<String>( "Hello", "World" )
There are special arrays which hold primitive types.
These are not the same :
val myArray = arrayOf<int>( 2, 3, 5 )
val myArray = intArrayOf( 2, 3, 5 )
The latter is preferred, as it is faster, and more memory efficient.
See also List
, which can grow and shrink in size.
Boxing
A primitive
type is converted to its boxed
(object) alternative.
These conversions are done automatically.
Boxing is required, because generic
types do not support primitive types.
For example :
// List is a generic type, and therefore cannot store primitive values.
// So myList is a List of Integer (not a List of int).
val myList = listOf<int>()
myList.add( 1 )
The last line automatically converts the 1
to a boxed Integer
,
before add
is called.
Unboxing
is the opposite process. i.e. converting a boxed type to a
primitive type. This is also automatic.
Function
A piece of code with several inputs and a single output.
The output is often called its return value
.
Some functions have no return value, in which case the return value
is void
.
When defining a function, its inputs are called parameters
.
When calling a function the values passed to it are called arguments
.
All of it inputs are treated equally (unlike a method
, whose first input is special).
Functions have a single implementation (there is no polymorphism
)
Functions are not object-oriented. They exist inside a class
,
but this acts merely as a namespace. i.e. Two functions can have the same name,
and arguments, and can be uniquely identified by their class.
Unlike methods
, a function has no receiver
.
The override
keyword is not applicable to a function.
Declared like this :
class MyClass {
fun myFunction( param1 : Type1, parameter2 : Type2 ) : ReturnType {
// body of the function
}
}
Note that functions are contained within a class
, despite not being
object-oriented.
When calling a function from the same class that it is declared :
myFunction( arg1, arg2 )
When calling from elsewhere :
MyClass.myFunction( arg1, arg2 )
List
Lists are similar to arrays
, but lists can grow and shrink as required.
Lists are part Java's java.util
package.
To create a list :
val myList = listOf<String>( "Hello", "World" )
Lists are mutable, i.e. you can add/remove/change elements in the list.
This is in contrast to Kotlin, which has List
(which isn't mutable) and
MutableList
(which obviously is mutable!).
Lists (as with all generic
types, cannot hold primitive values.
Therefore, if you use them to store int
s, double
s etc.,
then the values will be boxed
and unboxed
automatically as required.
FYI, listOf
method creates a Java ArrayList
.
Method
A piece of code, similar to a function
, with one major difference.
The first input (called the receiver
) is special.
The receiver must never be null
, as it determines which implementation
is called (polymorphism
).
Declared like this :
class MyClass {
meth myMethod( param1 : Type1, param2 : Type2 ) : ReturnType {
// body of the method
}
}
To call a method, you must first create an instance
of its class :
val myInstance = MyClass()
myInstance.myMethod( arg1, arg2 )
Note that this is wrong :
MyClass.myMethod( arg1, arg2 )
Object
Object
the the base class for all other classes.
Kotlin uses the term Any
instead.
Unlike Kotlin, there is no object
keyword
(which defines a class
with a single instance
).
Package
Primitive Types
Primitive types are not instances of a class. Feather has the same primitive types as Java. The keywords for these types are :
void
, bool
, byte
, short
, int
, long
, float
, double
.
Note that java uses the keyword boolean
, rather than bool
.
As with Java, there are boxed
versions of the primitive types.
Void
, Boolean
, Byte
, Short
, Integer
, Long
, Float
, Double
.
Primitive types are fast, and therefore preferable to the boxed
alternative.
Converting between the boxed
and unboxed
types is automatic.
Unlike Java, primitive type keywords can also be used for generic type arguments.
For example :
val myList = listOf<int>( 1, 2, 8 )
The implementation is the same as Java (i.e. the list is a list of Integer
, not int
).
Only the syntax differs.
The reason we do this, is so that Double
, Integer
etc is never needed
within a Feather script.
By default, Double
would not de recognised as a valid type.
This prevents accidental use of Double
instead of double
etc.
You can import the boxed types if you want :
import java.lang.Double
Unboxing
The opposite process to Boxing
.
For example :
val myList = listOf<int>( 2, 3, 5 )
val first : int = myList[0]
The list contains (boxed) Integers
, but the value is converted to
a primitive int
automatically.
Note, this can also be written as :
val first = myList[0]
However, first
would be a boxed Integer (not an int
).