my2iu / Jinq

LINQ-style queries for Java 8
Other
658 stars 71 forks source link

Anonymous class as a custom tuple #102

Open artelk opened 2 years ago

artelk commented 2 years ago

It would be great to have this feature

List<Account> bobsAccounts = customerStream
   .where( c -> c.getFirstName().equals("Bob") )
   .join( c -> new Object() { Customer customer = c; Account account = c.getAccount(); } )
   .select( t -> t.account )
   .toList();

Type inference with var is only allowed for local variables (they could allow that for fields in anonymous classes which are fully local but they didn't...) so you will need to explicitly specify the field types. But it still could be useful making the code more readable than with getOne()/getTwo(). And it would allow you to avoid creating separate custom tuple classes.

my2iu commented 2 years ago

Jinq is normal Java code. Java does not support duck typing or other features needed to get your suggested syntax to work, so Jinq cannot support such functionality.

artelk commented 2 years ago
import java.util.function.*;

public class MyClass {
    public static void main(String args[]) {
        var a = new A<Integer>();
        var b = a.map(x -> new Object() { int v = x; })
                 .map(o -> new Object() { String s = Integer.toString(o.v); });
    }
}

class A<T> {
    public <R> A<R> map(Function<T, R> f) { return new A<R>(); }
}

This is compilable with Java 10+

UPD: And without var it works with Java 8:

A<Integer> a = new A<Integer>();
A<String> b = a.map(x -> new Object() { int v = x; })
               .map(o -> new Object() { String s = Integer.toString(o.v); })
               .map(o -> o.s);
my2iu commented 2 years ago

Okay, that’s good to know. I’ll keep it in mind as a possible future feature. It would require writing a bunch of new analysis code for classes to see if they satisfy the usage patterns that you’ve shown. I’ve been thinking about how the framework could be modified to handle stuff like that before. Unfortunately, to be honest, I haven’t had the cycles available to add major features to Jinq in several years, so I probably won’t be able to get to it.

artelk commented 2 years ago

Jinq has a potential to be the best (the only really good) Java ORM. Why it's not super-popular? 😄

my2iu commented 2 years ago

Writing database queries in Java/C#/etc just generally stopped being popular. Microsoft doesn’t even push LINQ that much even more. Using LINQ or Jinq skillfully requires programmers to understand SQL anyway, so it’s probably easier to just work with a SQL-like language entirely instead of forcing programmers to learn an entirely different query language.

Also, the technology behind Jinq is obscure, so I’m basically the only person who can code it up right now. The big software vendors don’t have the expertise to add Jinq-like functionality to their own ORMs, so they have no reason to promote such technology. Without strong demand, there’s no reason for the big software vendors to train up someone with the required compiler/VM/database/symbolic-execution expertise. Right now, the big software vendors are more interested in pushing NoSQL and AI and big data and stuff.

zdu-strong commented 10 months ago

@my2iu I like jinq very much, it's amazing, Thank you and all the developers.

For mysql and h2, it works very well. Recently, I am trying to adapt spanner, and it is going very well so far.