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

The JS bot.Room.find() API does not fit for Java #4

Closed
diaozxin007 opened this issue Mar 19, 2020 · 10 comments
Closed

The JS bot.Room.find() API does not fit for Java #4

diaozxin007 opened this issue Mar 19, 2020 · 10 comments

Comments

@diaozxin007
Copy link
Collaborator

In java,
A static field or method should be directly referred to by its class name instead of its corresponding object name.

so

 Room room = bot.Room.find("xxxx");

is not suitable for java api

so I suggest use like

Room room = bot.findRoom("xxx");

Room room = bot.createRoom();

the same as contact

Contact contact = bot.findContact("xxx");

Contact contact = bot.createContact();
@huan
Copy link
Member

huan commented Mar 19, 2020

The bot.Room, bot.Contact, bot.Message design comes from wechaty/wechaty#518

We have a blog to record this process and some memorable information at https://blog.chatie.io/blessed-twins-bot/

Please read it first, and I'll go to dive into some Java design patterns first, then come back to you later and continue discussing under this issue.

@diaozxin007
Copy link
Collaborator Author

In java the code like

bot.Room.find()

cannot pass the code check tools just like:
find-bug
albaba p3c

so I think this is not a graceful api for java。

@huan
Copy link
Member

huan commented Mar 20, 2020

If not bot.Room.find(), how about using bot.Room().find()?

@diaozxin007
Copy link
Collaborator Author

LGTM

@huan
Copy link
Member

huan commented Mar 20, 2020

However, I still feel it will be more natural if we can use bot.Room.find().

What find-bug and p3c would say when they are linting the bot.Room.find() code?

If I have a Java class like the following presudo code:

class Bot {
  Room Room;
  public Bot() {
    this.Room = Room;
  }
}

bot = new Bot()
bot.Room.xxx()

What problem do they have in the Java language?

@diaozxin007
Copy link
Collaborator Author

diaozxin007 commented Mar 20, 2020

that code confuse Class and Object.
You user

Room Room;

first Room is Class and second one is an Object.

so bot.Room is object. not a class.

this is different

Room.find(xxx)

which one is a Class.

so that is impossible for java

@huan
Copy link
Member

huan commented Mar 20, 2020

Yes, I understand.

A class in Java might not be able to be referenced in a variable like the Python/JavaScript/ObjC, I guess there's no such concept like "MetaClass" in Java?

What if we use Kotlin: does Kotlin support that? I guess it can because it can be fully integrated with JavaScript?

@diaozxin007
Copy link
Collaborator Author

Our target is a Java sdk, we just use kotiln to develop a java sdk.

@huan
Copy link
Member

huan commented Mar 20, 2020

You are right. The end-user will use it in the Java language environment.

So I will not persist the bot.Room.xxx() design anymore.

The bot.Room().xxx() is good.

@huan huan changed the title The js wechaty api Not fit for Java api The JS bot.Room.find() API does not fit for Java Mar 20, 2020
@renjiayue
Copy link

我觉得此处的ROOM更应该是ROOMUtil

@huan huan transferred this issue from wechaty/java-wechaty-bak May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants