Top Banner
A language made for Java developers. @quicy Introduction
35

Introduction Xtend

May 27, 2015

Download

Technology

Hideki Kishida

Indroduction Xtend,
Extending Java,
Statically typed, Closure,
Extension method
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction Xtend

A language made for Java developers.

@quicy

Introduction

Page 2: Introduction Xtend

● Eclipseからリリースされている

● 新しいJVM言語

● 少ない学習コストで

● 静的型で安全に

● Javaをより簡潔に

● Javaをより強力に

Xtendとは

Page 3: Introduction Xtend

● Javaプロジェクトで一緒に使う

● Xtendクラス → Javaクラスに変換

● interface, enum, annotationなどは

全部Javaのまま

Xtendとは

Page 4: Introduction Xtend

Xtend == Java + α != 新言語

Javaのエクステンション

Xtendとは

Page 5: Introduction Xtend

class HelloXtend {def static void main(String[] args) {

println('Hello Xtend')}

}

Hello Xtend

Page 6: Introduction Xtend

import java.util.Listimport static extension java.util.Collections.*class HelloClosure {

def static void main(String[] _) {println(positiveOnly(newArrayList(1, 5, 2, -10, 4)))

}def static positiveOnly(List<Integer> values) {

values.filter [it > 0]}

}

Hello Closure

Page 7: Introduction Xtend

リアルタイムなJavaソース生成

Page 8: Introduction Xtend

● 静的型

● 高い表現力

● Javaの型システムをそのまま使える

● IDEによる賢い補完

Xtendのメリット

Page 9: Introduction Xtend

● 型推論

● プロパティアクセス

● 演算子オーバロード

● 全てが「式」

Javaをより簡潔に

Page 10: Introduction Xtend

● クロージャ

● 拡張メソッド

● 強力なswitch式

● マルチプル・ディスパッチ

● テンプレート式

● Xtend全体がJavaのシュガー

Javaをより強力に

Page 11: Introduction Xtend

● Scala使える状況ならScala使おう!

Scalaでいいんじゃん?

Page 12: Introduction Xtend

● Javaソースが必要なとき

(契約、GWT、etc...)

● Javaプログラマが

少ない学習コストで

今日からすぐに

開発/保守を効率化するとき

Xtend/いつ/なぜ?

Page 13: Introduction Xtend

● Eclipse + Xtend (☆Marketplace)

● 1. Javaプロジェクトを普通に作成

● 2. Xtend Classを作る

● 3. Quick Fixでライブラリを導入

● 4. あとはXtendクラスを書くだけ

● 5. 自動的にJavaソースに変換される

※ Mavenでもビルドできます

Xtendの開発環境

Page 14: Introduction Xtend

val List<String> names = getTheListOfNames() val names = getTheListOfNames()

※ 変数の型は省略できる

型推論~変数

Page 15: Introduction Xtend

for (String name : getTheListOfNames()) for (name : getTheListOfNames())

※ 変数の型は省略できる

型推論~forループ

Page 16: Introduction Xtend

def List<String> getTheListOfNames() { newArrayList("Tomte","Pippi","Carlson")}

def getTheListOfNames() { newArrayList("Tomte","Pippi","Carlson")}

※ リターン型は省略できる

型推論~リターン型

Page 17: Introduction Xtend

getTheListOfNames().map(String name | "Mr. "+name) getTheListOfNames().map(name | "Mr. "+name)

※ パラメータ型は省略できる

型推論~クロージャ・パラメータ

Page 18: Introduction Xtend

person.getName()person.name person.setName("Foo")person.name = "Foo" obj.compute()obj.compute

※ プロパティをより自然に扱える

プロパティ・アクセス

Page 19: Introduction Xtend

val apples = newArrayList(new Apple())val oranges = newArrayList(new Orange())val fruits = apples + oranges

※ 演算子の意味を多重定義できる

演算子オーバロード

Page 20: Introduction Xtend

e1 += e2 e1.operator_add(e2)e1 || e2 e1.operator_or(e2)e1 && e2 e1.operator_and(e2)e1 == e2 e1.operator_equals(e2)e1 != e2 e1.operator_notEquals(e2)e1 < e2 e1.operator_lessThan(e2)e1 > e2 e1.operator_greaterThan(e2)e1 <= e2 e1.operator_lessEqualsThan(e2)e1 >= e2 e1.operator_greaterEqualsThan(e2)e1 -> e2 e1.operator_mappedTo(e2)

演算子の種類(1)

Page 21: Introduction Xtend

e1 .. e2 e1.operator_upTo(e2)e1 + e2 e1.operator_plus(e2)e1 - e2 e1.operator_minus(e2)e1 * e2 e1.operator_multiply(e2)e1 / e2 e1.operator_divide(e2)e1 % e2 e1.operator_modulo(e2)e1 ** e2 e1.operator_power(e2)! e1 e1.operator_not()- e1 e1.operator_minus()

演算子の種類(2)

Page 22: Introduction Xtend

val data = if (file.exists) { fileContentsToString(file) } else { 'has no data' }

※ 文じゃないから評価結果が値になる

※ caseやtry~catchも式です

すべてが「式」

Page 23: Introduction Xtend

class Printer { def void print(Person person) { println(person.fullName) } def getFullName(Person p) { p.firstName + " " + p.lastName }}

クラスにメソッドを外部から追加する

イメージで呼び出せるようになる

拡張メソッド~ローカル拡張

Page 24: Introduction Xtend

class Printer { @Inject extension PersonExtension def print(Person person) { println( person.fullName ) }}

※ 外部定義された拡張をDIで適用する

拡張メソッド~Inject

Page 25: Introduction Xtend

import static extension java.util.Collections.*

静的拡張ライブラリを使える。

コレクションや文字列の拡張が便利。

高階関数+クロージャが超強力!

拡張メソッド~static import

Page 26: Introduction Xtend

val names = people.map [ p | p.name ]

※ コレクション処理がとても簡単に

クロージャ(1)~ブロック

Page 27: Introduction Xtend

val predicate =[Person person | "Hans" == person.name]

people.filter(predicate)

※ ラムダ式で生成した関数を

 高階関数に渡せる

クロージャ(2)~ラムダ式

Page 28: Introduction Xtend

people.filter[ Person p | "Hans" == p.name ]people.filter[ p | "Hans" == p.name ]people.filter[ "Hans" == it.name ]people.filter[ "Hans" == name ]

※ 型推論や暗黙itパラメータで簡略化

クロージャ(3)~簡略化

Page 29: Introduction Xtend

html [ head [ title [$("XML encoding with Xtend")] ] body [ h1 [$("XML encoding with Xtend")] p [$("this dsl can be used as alternative to XML")]

※ BuilderなどのDSLとしても有効

クロージャ(4)~Builder DSL

Page 30: Introduction Xtend

val fullName = '''Name: «p.firstName» «p.lastName»''' def getFullName(Person p) '''

Fist name: «p.firstName»Last name: «p.lastName»

'''

※ テンプレート文字列を簡単に作れる

※ IF/FORなどの制御構造も埋め込める

テンプレート式

Page 31: Introduction Xtend

val Shape shape = ...val desc = switch (shape) { Rectangle case shape.width == shape.height : "Square ("+shape.width+")" Rectangle : "Rectangle ("+shape.width+" x "+shape.height+")" Circle : "Circle ("+shape.diameter+")" default : "Don't know"}

※ 型ガードと条件検査を同時にできる

Switch式

Page 32: Introduction Xtend

def example() { val Shape s = new Rectangle() println(s.label)}def dispatch label(Shape s) { "some shape"}def dispatch label(Rectangle r) { ← "a rectangle"}

※ 実行時の型でメソッドが選択される

マルチプル・ディスパッチ

Page 33: Introduction Xtend

● 言語機能もIDEも、まだ少し力不足

● 開発が活発です

● ちょっと足りないなと思うものも、

次々に実装されていきます

どんどん進化中!

Page 34: Introduction Xtend

● 開発者がイケメン

● Sven Efftingeさん素敵です

+1

Page 35: Introduction Xtend

● 少ない学習コストで

● 静的型で安全に

● Javaをより簡潔に

● Javaをより強力に

Xtendのまとめ