How to work with objects?
satouriko opened this issue · 9 comments
古有一道法、以一物照一物、謂之曰「射」。 凡變數以「射」為原型者、謂之曰「對象」。 今有程序員者一人、遍讀此作而不知「對象」應作何書、方前來求問。
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!
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
?
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 :)
Suggestion for object syntax:
吾有一物。夫其物也。
有一數。曰三。名之曰「甲」。
有一言。曰「「噫吁戲」」。名之曰「乙」。
有一爻。曰陰。名之曰「丙」。
名之曰「对象」。書之。
夫「对象」之「甲」。書之。
Compiles to:
var DUI4XIANG4 = {
JIA3: 3,
YI3: "噫吁戲",
BING3: false
};
console.log(DUI4XIANG4);
console.log(DUI4XIANG4.JIA3);
@yyx990803 Nice! I like it very much
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?
@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
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!
施「JSON.stringify」於「戊」。
突然有穿越的感觉,哈哈哈