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

How to work with objects? #20

Open
satouriko opened this issue Dec 17, 2019 · 11 comments
Open

How to work with objects? #20

satouriko opened this issue Dec 17, 2019 · 11 comments
Labels
enhancement New feature or request

Comments

@satouriko
Copy link

satouriko commented Dec 17, 2019

古有一道法、以一物照一物、謂之曰「射」。 凡變數以「射」為原型者、謂之曰「對象」。 今有程序員者一人、遍讀此作而不知「對象」應作何書、方前來求問。

@LingDong-
Copy link
Member

Hi there,

Good question! Objects currently share the same interface as Arrays (), because in JavaScript Arrays are also objects. As for compiling to Python, I implemented a custom container class in Python to simulate the same behavior.

吾有一列。名之曰「物」。昔之「物」之「「名」」者。今「「甲」」是矣。
var obj = {}; obj.name="a";

I know! the above is not intuitive and somewhat confusing. I'm considering about separating Objects from Arrays and give them a good name like .

Another good question is whether or not to bring more sophisticated OOP into wenyan, such as classes, inheritance, constructors, etc. I'll need to think more about that.

Thanks!

@satouriko
Copy link
Author

One more question, how do you identify an object from an array? AFAIC, 吾有一列。名之曰「物」。昔之「物」之「「名」」者。今「「甲」」是矣。 should make var obj = []; obj.name = 'a'. Will objects created by this semantics share the prototype Array?

@LingDong-
Copy link
Member

That's correct, you cannot tell Array from objects in my current implementation. But as I mentioned I'm considering adding real objects to the language, keep tuned :)

@yyx990803
Copy link

yyx990803 commented Dec 17, 2019

Suggestion for object syntax:

吾有一物。夫其物也。
  有一數。曰三。名之曰「甲」。
  有一言。曰「「噫吁戲」」。名之曰「乙」。
  有一爻。曰陰。名之曰「丙」。
名之曰「对象」。書之。
夫「对象」之「甲」。書之。

Compiles to:

var DUI4XIANG4 = {
  JIA3: 3,
  YI3: "噫吁戲",
  BING3: false
};
console.log(DUI4XIANG4);
console.log(DUI4XIANG4.JIA3);

@LingDong-
Copy link
Member

@yyx990803 Nice! I like it very much

@LingDong- LingDong- added the enhancement New feature or request label Dec 17, 2019
@satouriko
Copy link
Author

I think we may need a delete syntax like

昔「对象」之「甲」者。今不復存矣。

compiles to

delete DUI4XIANG4.JIA3;

BTW, will we consider add a null (or even undefined) and a typeof operator?

@LingDong-
Copy link
Member

LingDong- commented Dec 17, 2019

@rikakomoe These suggestions are all great, thanks. Will incorporate.

Also, adding link here to a related proposal about supporting classes, as a reference:
https://github.com/LingDong-/wenyan-lang/issues/31

@LingDong-
Copy link
Member

Hi everyone!

Object literals are here!
See commits e229e48 and 58bee3c.

Inspired by @yyx990803 and @rikakomoe 's suggestions, the basic syntax is like this:

吾有一物。名之曰「精衛」。其物如是。
	物之「「名」」者。言曰「「女娃」」。
	物之「「足數」」者。數曰二。
	物之「「喙數」」者。數曰一。
是謂「精衛」之物也。

Compiles to

var 精衛={};精衛={"名":"女娃","足數":2,"喙數":1,};

Classes are not added yet, but now you can simulate them with "factory" style:

吾有一術。名之曰「造物之術」。欲行是術。必先得一言。曰「甲」。乃行是術曰。
	吾有一物。名之曰「乙」。其物如是。
		物之「「丙」」者。言曰「甲」。
		物之「「丁」」者。數曰四。
	是謂「乙」之物也。
	乃得「乙」。
是謂「造物之術」之物也。

施「造物之術」於「「某甲」」。名之曰「戊」。
昔之「戊」之「「丁」」者。今五是矣。
施「JSON.stringify」於「戊」。書之。

... and a bit of JSON.stringify hack to display the result.

The delete syntax:

昔之「戊」之「「丁」」者。今不復存矣。
施「JSON.stringify」於「戊」。書之。

A new example containing all the above is added to ./examples/obj.wy. You can try it out on the online IDE too.

There might be some minor bugs with the implementation as it was freshly made, Please let me know if you find any bugs!

Regarding the design, I'm also open to changes if you can convince me ;)

Thank all of you again for the proposal. Let me know what you think. If this one goes down well my next step is to implement import statements. Stay tuned!

@yyx990803
Copy link

施「JSON.stringify」於「戊」。 突然有穿越的感觉,哈哈哈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants
@yyx990803 @LingDong- @satouriko and others