| 1 | <h2>Comments</h2> |
| 2 | <pre><code>// Single line comment |
| 3 | /* Multi-line |
| 4 | comment */</code></pre> |
| 5 | |
| 6 | <h2>Strings</h2> |
| 7 | <pre><code>"foo 'bar' baz" |
| 8 | 'foo "bar" baz' |
| 9 | """Multi-line |
| 10 | string""" |
| 11 | '''Multi-line |
| 12 | string''' |
| 13 | "String /containing/ slashes" |
| 14 | </code></pre> |
| 15 | |
| 16 | <h2>Slashy strings (regex)</h2> |
| 17 | <pre><code>/.*foo.*/ |
| 18 | /regex"containing quotes"/ |
| 19 | $/.*"(.*)".*/(.*)/$</code></pre> |
| 20 | |
| 21 | <h2>Interpolation inside GStrings and regex</h2> |
| 22 | <pre><code>"The answer is ${21*2}" |
| 23 | "The $foxtype ${foxcolor.join()} fox" |
| 24 | /foo${21*2}baz/ |
| 25 | 'No interpolation here : ${21*2}'</code></pre> |
| 26 | |
| 27 | <h2>Full example</h2> |
| 28 | <pre><code>#!/usr/bin/env groovy |
| 29 | package model |
| 30 | |
| 31 | import groovy.transform.CompileStatic |
| 32 | import java.util.List as MyList |
| 33 | |
| 34 | trait Distributable { |
| 35 | void distribute(String version) {} |
| 36 | } |
| 37 | |
| 38 | @CompileStatic |
| 39 | class Distribution implements Distributable { |
| 40 | double number = 1234.234 / 567 |
| 41 | def otherNumber = 3 / 4 |
| 42 | boolean archivable = condition ?: true |
| 43 | def ternary = a ? b : c |
| 44 | String name = "Guillaume" |
| 45 | Closure description = null |
| 46 | List<DownloadPackage> packages = [] |
| 47 | String regex = ~/.*foo.*/ |
| 48 | String multi = ''' |
| 49 | multi line string |
| 50 | ''' + """ |
| 51 | now with double quotes and ${gstring} |
| 52 | """ + $/ |
| 53 | even with dollar slashy strings |
| 54 | /$ |
| 55 | |
| 56 | /** |
| 57 | * description method |
| 58 | * @param cl the closure |
| 59 | */ |
| 60 | void description(Closure cl) { this.description = cl } |
| 61 | |
| 62 | void version(String name, Closure versionSpec) { |
| 63 | def closure = { println "hi" } as Runnable |
| 64 | |
| 65 | MyList ml = [1, 2, [a: 1, b:2,c :3]] |
| 66 | for (ch in "name") {} |
| 67 | |
| 68 | // single line comment |
| 69 | DownloadPackage pkg = new DownloadPackage(version: name) |
| 70 | |
| 71 | check that: true |
| 72 | |
| 73 | label: |
| 74 | def clone = versionSpec.rehydrate(pkg, pkg, pkg) |
| 75 | /* |
| 76 | now clone() in a multiline comment |
| 77 | */ |
| 78 | clone() |
| 79 | packages.add(pkg) |
| 80 | |
| 81 | assert 4 / 2 == 2 |
| 82 | } |
| 83 | }</code></pre> |