• [^] # Re: Compatibilité à partir du Jdk 7+

    Posté par (site web personnel) . En réponse au journal Sortie de Groovy 4.0.0. Évalué à 2.

    Mea maxima culpa, cette fonctionnalité mérite que l'on s'y attarde, j'avais peu de temps, aussi mettre du JSON dans des exemples de code pour illustrer un langage me révulse.

    Voici un autre exemple avec des objets classiques :

    enum Location {
     US('USA'), EU('Europe')
     Location(String label) {
     this.label = label
     }
     final String label
    }
    final class Machine {
     Location loc
     String name
     List<String> profile
    }
    def m1 = new Machine(loc: Location.EU, name: 'Tartanpion', profile: ['web', 'bi'])
    def m2 = new Machine(loc: Location.US, name: 'Escalope', profile: ['web', 'backend'])
    def m3 = new Machine(loc: Location.EU, name: 'Jambon', profile: ['bi'])
    def m4 = new Machine(loc: Location.US, name: 'Tartuffe', profile: ['web'])
    def m5 = new Machine(loc: Location.EU, name: 'TarteAuPoire', profile: ['web'])
    def machines = [m1, m2, m3, m4, m5]
    def l1 = GQ {
     from p in machines
     where p.name.startsWith('Tar')
     select p.name
    }.toList()
    println l1
    def l2 = GQ {
     from p in machines
     where p.loc == Location.EU
     select p.name, p.loc.label
    }.toList()
    println l2

    Ce qui donne :

    [Tartanpion, Tartuffe, TarteAuPoire]
    [[Tartanpion, Europe], [Jambon, Europe], [TarteAuPoire, Europe]]

    C'est puissant..