Показаны сообщения с ярлыком programming. Показать все сообщения
Показаны сообщения с ярлыком programming. Показать все сообщения

пятница, 20 сентября 2013 г.

groovy switch: nasty bug

I found nasty error in Groovy compiler. Consider the following code:

1
2
3
4
5
6
7
8
byte b = 1
switch(b) {
  case 0..9:
    println 'it is between 0 and 9'
  break
  default:
    println 'it is something else'
}

It executes 'default' part, not the part with 0..9, which is not what a programmer would typically expect.
The reason behind it should be related to type conversion between "byte" and "int" types. With the following workaround:

1
swtch((int)b)

the program executes "proper" case.

среда, 4 сентября 2013 г.

groovy: switch statement and closure comprehension – nice for DSL

It is rather easy to extend groovy switch statement with our own DSL:


The trick here is that single-argument versions of IsGreaterThan, IsLessThan return closures. Switch-statement “understands” closures: it passes it’s argument (x in our case) as a parameter to the closure and expects boolean result being returned from the closure.Same thing can be done via function currying, but it looks not so nice, as with function overload.

вторник, 6 августа 2013 г.

Partial interface implementation in groovy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
interface X {
  void a()
  void b()  
}

class XAdapter implements X {
  void a() { println 'default implementation of a' }
  void b() { println 'default implementation of b' }
}

def o = [ 
  a: { println 'overridden implementation of a' } 
] as XAdapter

o.a()
o.b()
will output:
overridden implementation of a
default implementation of b

среда, 5 июня 2013 г.

Programming for fun: script for batch installation of maven artifacts

I created gradle script that delivers batch installation of maven artifacts to local maven repository (source code here: https://github.com/akhikhl/contribs).

The script supports two tasks - installContribs and cleanContribs - but can be extended with additional tasks (for example, implementing deployment to corporate repo).

how installContribs task works:

1. you call it with the command-line:

gradle -b contribs.gradle 
(or you rename script to "build.gradle" and just put it somewhere in the multi-project tree, it will be called by gradle automatically).

2. installContribs task iterates the current folder (where the script resides) and all it's subfolders.

3. in each folder it iterates files with extension "pom".

4. for each found pom-file it looks for ".jar", "-sources.jar" and "-javadoc.jar" and install all found files (together with pom) to the local maven repository.

5. pom-file without jars will be installed as an artifact on it's own. Typical use-case: installation of parent-poms and aggregator-poms.

The script accurately calculates inputs/outputs. If all files were not changed since the last installation, it does not install anything and shows "UP-TO-DATE" in the console.

cleanContribs task makes script "forget" about the time of the last artifact installation. As the result, running script with installContribs task will install all artifacts anew.