基本型
- Integers - 32-bit signed integers
- Floats - 32-bit floating point numbers
- Long – 64-bit signed integers
- Double – 64-bit floating point numbers
- Booleans - true and false
- Strings - strings of characters
- Objects – Instantiated objects (defined with the class keyword)
- Arrays - Allocated with the syntax new [X] where ‘X’ is an expression computing the size of the array
- Dictionaries - Associative arrays, allocated with the syntax {}
変数の宣言
varで宣言し、全ての値はオブジェクトとなる。
var n = null; // Null reference
var x = 5; // 32-bit signed integers
var y = 6.0; // 32-bit floating point
var l = 5l; // 64-bit signed integers
var d = 4.0d; // 64-bit floating point
var bool = true; // Boolean (true or false)
var arr = new [20 + 30]; // Array of size 50
var str = "Hello"; // String
var dict = { x=>y }; // Dictionary: key is 5, value is 6.0
var z = arr[2] + x; // Null pointer waiting to happen
配列
宣言する用法は2つ
サイズを宣言
var array = new [size];
初期化をする
var array = [1, 2, 3, 4, 5];
多次元配列も可能
var array = [ [1,2], [3,4] ];
但し、空の2次元配列を直接宣言する方法がない。
// Shout out to all the Java programmers in the house
var array = new [first_dimension_size];
// Initialize the sub-arrays
for( var i = 0; i < first_dimension_size; i += 1 ) {
array[i] = new [second_dimension_size];
}
辞書配列(連想配列)
var dict = { "a" => 1, "b" => 2 }; // Creates a dictionary
System.println( dict["a"] ); // Prints "1"
System.println( dict["b"] ); // Prints "2"
System.println( dict["c"] ); // Prints "null"
To initialize an empty dictionary, use the following syntax:
var x = {}; // Empty dictionary
By default, objects hash on their reference value. Classes should override the hashCode() method in Toybox.Lang.Object to change the hashing function for their type:
class Person
{
// Return a number as the hash code. Remember that the hash code must be
// the same for two objects that are equal.
// @return Hash code value
function hashCode() {
// Using the unique person id for the hash code
return mPersonId;
}
}
Dictionaries automatically resize and rehash as the contents grow or shrink. This makes them extremely flexible, but it comes at a cost. Insertion and removal of the contents can cause performance problems if there is accidental or excessive resizing and rehashing. Also, because hash tables require extra space for allocation, they are not as space-efficient as either objects or arrays.
シンボル(Symbols)
シンボル(Symbols)は軽量な定数識別子である。
Monkey Cではコンパイラが新たなシンボルを見つけたら、シンボルに固有の値を割り当てる。
これによりシンボルを、const or enumを宣言すること無く、キーや定数として使うことができる。
var a = :symbol_1;
var b = :symbol_1;
var c = :symbol_2;
Sys.println( a == b ); // Prints true
Sys.println( a == c ); // Prints false
enumを宣言せずにキーを作成したい時に便利
var person = { :firstName=>"Bob", :lastName=>"Jones" };
定数
Constants are named, immutable values declared with the const keyword. These are useful for storing unchanging values that may be used repeatedly throughout code. Constants must be declared at the module or class level; they cannot be declared within a function.
constで宣言する
Constants support the same types as listed for variables.
const PI = 3.14;
const EAT_BANANAS = true;
const BANANA_YELLOW = "#FFE135";
列挙型(Enumerations, enum)
列挙型は自動で1ずつ増加する値がマッピングされる。
Unless explicity set (see the second example), each proceeding symbol is automatically assigned the value of its predecessor plus one, starting with 0.
下記の例のように最初のシンボル(Monday)には0,次のシンボル(Tuesday)は1,となる。
These symbols can be used just like constant variables (which is essentially what they are). Enums must be declared at the module or class level; they cannot be declared within a function.
enum {
Monday, // Monday = 0
Tuesday, // Tuesday = 1
Wednesday // Wednesday = 2
// ...and so on
}
enum {
x = 1337, // x = 1337
y, // y = 1338
z, // z = 1339
a = 0, // a = 0
b, // b = 1
c // c = 2
}
Note that assigning anything other than an integer will cause an error.
Calling Methods and Functions
To call a method within your own class or module, simply use the function call syntax:
function foo( a ) {
//Assume foo does something really impressive
}
function bar() {
foo( "hello" );
}
If calling on an instance of an object, precede the call with the object and a ‘.’.
最終更新:2016年03月11日 19:12