Groovy single-page-documentation
Java 파생 스크립트 언어
설치(SDKMAN)
↓ shell
실행
↓ shell
컴파일
.class 파일을 생성한다
↓ shell
Language Specification
Java 스펙과 다른 것만 기술한다
Syntax
Identifiers
- Quoted identifiers
임의의 문자열 상수 표현 및 GString 표현을 '.' 이후의 식별자로 이용할 수 있다
↓ groovy
Strings
- Single-quoted string : 'text'
- Triple-single-quoted string : '''text'''
- 여러 줄에 걸친 텍스트 문자열 표현 가능
- 들여쓰기 공백도 포함되므로 유의 ─ String#stripIndent 등을 이용하여 지울 수 있다
- 줄 끝에 \를 쓰면 \n를 삽입하지 않는다
- groovy.lang.GString; String interpolation
- 보간 ${}식에는 임의 표현식 ─ 심지어 여러 문장도 가능하다. 반환 없는 문장에 대해서는 null이 채워지고, 여러 문장인 경우 마지막 문장의 평가값만 문자열 보간에 이용된다
- 모호하지 않은 경우, .표현식은 {}를 생략할 수 있다
- $ 자체를 표현하고 싶은 경우 \$로 이스케이프
- GString의 평가값이 String과 같아도 해시코드는 서로 다르다
- Triple-double-quoted string : """text"""
- Slashy string : /text/
↓ groovy
↓ groovy
Interpolation 가능
Interpolation 가능. \를 이스케이프하지 않는다. 대신 / 자체를 표현하려면 \/로 이스케이프. 여러 줄 문자열 가능
Characters
↓ groovy
Numbers
- BigInteger/BigDecimal literal : 1234g
- Double literal : 1234d
- Power operator : x**y
Lists
↓ groovy
Arrays
↓ groovy
Maps
↓ groovy
Operators
Relational operators
- == : equal
- === : identical Since 3.0.0
is 연산자와 동일
Conditional operators
- Elvis operator
↓ groovy
Object operators
- Safe navigation operator : ?.
- Direct field access operator : @
- Method pointer operator : .&
↓ groovy
↓ groovy
↓ groovy
메서드를 가리키는 groovy.lang.Closure 인스턴스를 반환한다
↓ groovy
Regex expression
↓ groovy
Other operators
- Spread(-dot) operator
- Spreading method arguments
- Spread list elements
- Spread map elements
- Range operator
- Subscript operator
- Spaceship operator
- Safe index operator : ?[] Since 3.0.0
- Membership operator : in
- List에 대하여 in == contains()
- 그 외엔 in == isCase()
- Coercion operator : as
컬렉션에 대한 프로젝션 반환. 컬렉션의 collect와 유사
↓ groovy
↓ groovy
↓ groovy
↓ groovy
next(), previous()가 정의된 임의 Comparable로 range를 구성할 수 있다
↓ groovy
getAt(int), putAt(int, value)를 정의하면 아래 연산이 가능
↓ groovy
<=> == compareTo
↓ groovy
↓ groovy
대상 타입이 소스와 동일하더라도 새 인스턴스를 생성함에 유의. asType() 메서드로 사용자정의 변환 가능
↓ groovy
Operator overloading
operator | method |
---|---|
+ | plus |
- | minus |
* | multiply |
/ | div |
% | mod |
** | power |
| | or |
& | and |
^ | xor |
as | asType |
obj() | call |
obj[x] | getAt |
obj[x] = y | putAt |
x in y | isCase |
<< | leftShift |
>> | rightShift |
>>> | rightShiftUnsigned |
++ | next |
-- | previous |
+x | positive |
-x | negative |
~x | betwiseNegate |
Program structure
Package names
↓ groovy
Imports
↓ groovy
Default imports
↓ groovy
Scripts versus classes
Groovy는 스크립트와 Java 클래스 작성 모두를 허용하며, 스크립트는 컴파일 시 클래스로 변환된다
- 스크립트의 함수 선언은 자동으로 컴파일된 클래스의 멤버가 된다
- 나머지 스크립트는 컴파일된 클래스의 run() 바디가 된다
Variables
↓ groovy
Object orientation
Types
- Constructors
- Annotation
- Meta-annotations
↓ groovy
↓ groovy
↓ groovy
Traits
Interface와 AbstractClass를 섞은 느낌
Closures
Syntax
↓ groovy
Delegation strategy
- this : 클로저를 갖는 클래스 인스턴스
- owner : 클로저를 갖는 클래스 또는 클로저 인스턴스
- delegate : 사용자가 임의로 설정할 공유값. 기본값은 owner
- Closure.resolveStrategy
- Closure.OWNER_FIRST : owner 먼저. owner에 없으면 delegate
- Closure.DELEGATE_FIRST : delegate 먼저. delegate에 없으면 owner
- Closure.OWNER_ONLY, DELEGATE_ONLY
- Closure.TO_SELF : Closure 서브클래스를 직접 작성하는 경우, Closure 인스턴스 자체
↓ groovy
delegate를 생략했을 때, 어느 것을 먼저 찾을 것인지
Closures in GStrings
↓ groovy
Functional programming
- Left currying : Closure.curry(...)
- Right currying : Closure.rcurry(...)
- Index based currying : Closure.ncurry(idx, value, ...)
- Memoization : Closure.memoize()
- Composition : beforeClosure >> afterClosure, afterClosure << beforeClosure
- Trampoline : 재귀 호출 쌓이는 스택을 회피하기 위해 클로저를 반복적 호출로 변환
memoizeAtMost, memoizeAtLeast, memoizeBetween
↓ groovy
Semantics
Statements
- Multiple assignment
- Object destructing with multiple assignment
- switch/case
- Looping structures
↓ groovy
getAt/putAt 메서드를 정의하면 객체에 대한 복수 할당이 가능하다
↓ groovy
↓ groovy
↓ groovy