Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anonymous class as a custom tuple #102

Open
artelk opened this issue May 23, 2022 · 6 comments
Open

Anonymous class as a custom tuple #102

artelk opened this issue May 23, 2022 · 6 comments

Comments

@artelk
Copy link

artelk commented May 23, 2022

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
Copy link
Owner

my2iu commented May 24, 2022

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.

@my2iu my2iu closed this as completed May 24, 2022
@artelk
Copy link
Author

artelk commented May 24, 2022

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
Copy link
Owner

my2iu commented May 24, 2022

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.

@my2iu my2iu reopened this May 24, 2022
@artelk
Copy link
Author

artelk commented Jun 7, 2022

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

@my2iu
Copy link
Owner

my2iu commented Jun 7, 2022

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
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants