12. コード紹介
Java Xtend
public class JavaMain {
public static void main(String... args) {
System.out.println("Hello, World");
}
}
class XtendMain {
def static void main(String... args) {
println("Hello, World")
}
}
そんな変わらない!
使う必要あんの?
13. コード紹介
Java Xtend
final Order order = new Order();
order.setMinShippingTime(new Date());
order.setTotalAmount(BigDecimal.ZERO);
val order = new Order => [
minShippingTime = new Date
totalAmount = BigDecimal::ZERO
]
ちょっと読みやすい?!
20. Classes and Members
• Imports
• “^” でエスケープ
• static import は “*” でしか出来ない
import hoge.^val.AAA
import static java.util.Collections.*
21. Classes and Members
• Class Declaration
• アクセス修飾子は既定で public
• constructors, fields, methods, annotations を書ける
• inner class は NG
• class に指定出来る修飾子は public, abstract, final, static
• protected とか無理。static も今のところ意味なし
package pronama.osaka
import java.io.Serializable
class MyClass extends Number implements Serializable {
…
}
22. Class and Members
• Constructors
• Java と違って new で宣言
• 既定で public
• public, protected, package, private が指定可能
class MyClass extends AnotherClass {
new(String s, int n) { super(s, n) }
new(String s) { this(s, 0) }
new() { this("default") }
}
23. Java と Xtend のアクセス修飾子比較
Java Xtend
public public
protected protected
private private
package private package
24. Classes and Members
• Fields
• 既定 で private
• public, protected, package, private が指定可能
• val/var で型推論。(val は final, var は not final)
class MyClass {
int count = 1
static boolean debug = false
var name = 'foo' // 型推論 String 型
val UNIVERSAL_ANSWER = 42 // 型推論 int 型
}
30. Classes and Members
• Methods
• Dispatch Method
• ディスパッチの説明はここがいいよ
• [雑記] 多重ディスパッチ (C# によるプログラミング入門)
• http://ufcpp.net/study/csharp/sp4_multipledispatch.html
def dispatch String add(int a, String b) { … }
def dispatch String add(int a, int b) { … }
def dispatch String add(String a, int b) { … }
def dispatch String add(String a, String b) { … }
31. Classes and Members
protected String _add(final int a, final String b) { … }
protected String _add(final int a, final int b) { … }
protected String _add(final String a, final int b) { … }
protected String _add(final String a, final String b) { … }
public String add(final Object a, final Object b) {
if (a instanceof Integer && b instanceof Integer) {
return _add((Integer)a, (Integer)b);
} else if (a instanceof Integer && b instanceof String) {
return _add((Integer)a, (String)b);
} else if (a instanceof String && b instanceof Integer) {
return _add((String)a, (Integer)b);
} else if (a instanceof String && b instanceof String) {
return _add((String)a, (String)b);
} else {
throw new IllegalArgumentException("…");
}
}
32. Classes and Members
• Annotations
• Java と同じ
@TypeAnnotation("hoge")
class MyClass2 {
@FieldAnnotation String myField
@MethodAnnotation(children = #[ @MyAnno(true), @MyAnno ])
def void meMethod(@ParameterAnnotation String param) {}
}
44. Expressions
• Literals
• Boolean, Null, Type
• Type は Java と違う。 typeof(クラス名)
• Java の場合は、 Class.class
val t = true
val f = false
val String s = null
val clazz = typeof(java.lang.String)
// java.lang.String.class
val clazz2 = typeof(int[]) // int[].class
48. Expressions
• Operator overloading
• 演算子をメソッドに変換して実現
val x = 2.71BD
val y = 3.14BD
val sum = x + y // BigDecimalExtension.operator_plus(x,y)
def Date operator_plus(Date a, int day) {
val c = Calendar::instance
c.time = a
c.add(Calendar::DATE, day)
c.time
}
val nextDay = new Date + 1
55. Expressions
• Operator overloading
• With Operator
• => doubleArrow
val item = new Pronama => [
place = "大阪"
number = 23
speakers = #[
new Speaker => [ name = "@murajun1978" ],
new Speaker => [ name = "@shinsukeoda" ],
new Speaker => [ name = "@uniunix" ],
new Speaker => [ name = "@chuki" ]
]
]
56. Expressions
• Operator overloading
• Range Operators
• .. , ..< , ..>
• Pair Operator
• a -> b で Pair<K, V> に
for (i : 0 ..< list.size) { }
for (i : list.size >.. 0) { }
(10..25).contains(17) // true
val b = 1 -> "a" // Pair<Integer, String>
61. Expressions
• Lambda Expressions
final JTextField textField = new JTextField();
textField.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
textField.setText("Something happend!");
}
});
val textField = new JTextField
textField.addActionListener([ ActionEvent e |
textField.text = "Something happend!"
])
62. Expressions
• Lambda Expressions
• パラメータの型は推論出来る
• パラメータが1個の場合は省略可(パラメータ名は“it”)
val textField = new JTextField
textField.addActionListener([
textField.text = "Something happend!"
])
63. Expressions
• Lambda Expressions
• ラムダの中で this は外側のクラスのインスタンスを指す
• 自身のインスタンスを指す場合は、”self” を使う
abstract class Hoge {
protected def end() { }
def void execute()
}
val Hoge hoge = [ |
self.end // self 付けないと、外側のクラスの end メソッドを使う。
// 定義されていない場合は、コンパイルエラー
]
64. Expressions
• Lambda Expressions
• Typing
• ラムダは Function/Procedure に変換される
val toUpperCaseFunction =
[ String s | s.toUpperCase ] // (String) => String
val (String) => String stringToStringFunction = [ toUpperCase ]
val Function1<? super String, ? extends String> same = [ toUpperCase ]
val stringToStringFunction2 = [ String s | s.toUpperCase ]
65. Expressions
• If
• 戻り値は、if, else の戻り値の共通の型になる
• Java の3項演算子(predicate ? then : else)は無い
val num = if (true) BigDecimal::ZERO else 5l
// num is Number
val obj = if (false) "abc" else 19
// obj is Comparable<? & Serializable>
String name = firstName != null ?
firstName + " " + lastName : lastName;
var name =
if (firstName != null) firstName + ' ' + lastName
else lastName
66. Expressions
• Switch
• Java と結構違う!
• フォールスルー無し (break 要らず)
• 何でも使える (Java だと int/Integer/String/Enum のみ)
• case で指定した値/式 が boolean の場合はその結果を使う
• boolean では無い場合は、Object.equals を使う
val result = switch myString {
case myString.length > 5 : "長い文字列"
case "何か" : "何か 文字列"
default : "他の文字列"
}
67. Expressions
• Switch
• Type guards
• case に型を指定することも出来る。指定した型しか case の条件
を判定しない
def isNullOrEmpty(Object target) {
switch target {
CharSequence case target.length === 0: true
Collection<?> case target.empty: true
case target === null: true
default: false
}
}
68. Expressions
• Loop
全部 戻り値無し(void)
• For
• for (T1 value : arrayOrIterable) expression
• 変数の型(T1) は推論可能
• While Loop
• while (predicate) expression
• Do-While Loop
• do expression while (predicate)