2008年12月12日金曜日

JavaFX Expressions 文法

Block Expressions 文法
  1. var nums = [5739];  
  2. var total = {  
  3.      var sum = 0;  
  4.      for (a in nums) { sum += a };  
  5.      sum;  
  6. }  
  7. println("Total is {total}.");  // Total is 24  

The if Expression
  1. def age = 8;  
  2. var ticketPrice;0  
  3.   
  4. if (age < 5 ) {  
  5.      ticketPrice = 0;  
  6. else if (age < 12 or age > 65) {  
  7.      ticketPrice = 5;  
  8. else {  
  9.      ticketPrice = 10;  
  10. }  
  11. println("Age: {age} Ticket Price: {ticketPrice} dollars.");  

次のように1行で書くことも可能
  1. ticketPrice = if (age < 50 else if (age > 5 and age < 125 else 10;  

Range Expressions
  1. var num = [0..5];  
  2. var nums = [1..10 step 2];  // [ 1, 3, 5, 7, 9 ]  
  3. var nums = [10..1 step -1];  
  4.  // [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]  

The for Expression
  1. var days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];  
  2.   
  3. for (day in days) {  
  4.      println(day);  
  5. }  
  6.   
  7. // Resulting sequence squares the values from the original sequence.  
  8. var squares = for (i in [1..10]) i*i;   
  9.   
  10. // Resulting sequence is ["MON", "TUE", "WED", and so on...]  
  11. var capitalDays = for (day in days) day.toUpperCase();   

The while Expression
  1. var count = 0;  
  2. while (count < 10) {  
  3.     println("count == {count}");  
  4.     count++;  
  5. }   

The break and continue Expressions
  1. for (i in [0..10]) {  
  2.      if (i > 5) {  
  3.           break;  
  4.      }  
  5.   
  6.      if (i mod 2 == 0) {  
  7.           continue;  
  8.      }  
  9.   
  10.      println(i);  
  11. }  

The throw, try, catch and finally Expressions
  1. try {  
  2.      foo();  
  3. catch (e: Exception) {  
  4.      println("{e.getMessage()} (but we caught it)");  
  5. finally {  
  6.      println("We are now in the finally expression...");  
  7. }  
  8.   
  9. function foo() {  
  10.      var somethingWeird = false;  
  11.   
  12.      if(somethingWeird){  
  13.           throw new Exception("Something weird just happened!");  
  14.      } else {  
  15.           println("We made it through the function.");  
  16.      }  
  17. }  

0 件のコメント:

コメントを投稿