Open statementreply opened 4 years ago
@statementreply wow, deep questions.
Currently in wenyan-lang arrays are aliased when assigned to a new variable, like in JavaScript, python, etc., and current behavior seems to reflect that correctly. Immutable types are copied on the other hand.
Feel free to suggest another behavior, e.g. shallow copy, deep-copy, copy-assignment in C++, etc. if you think there are reasons it should be preferred.
One thing I'm thinking is maybe we can utilize two different syntax (which are currently equivalent) to distinguish aliasing and copying.
// Hypothetically:
夫「甲」。名之曰「乙」。// alias
吾有一列。曰「乙」。名之曰「甲」// copy
Also, since I think 曰「乙」名之曰「甲」
can be a little bit confusing to read, I'm thinking about adding 同
to the syntax like so:
吾有一列。同「乙」。名之曰「甲」
Again, discussions are very welcome :)
I think current behavior is expected & desirable. What do you find wrong?
Thanks for bringing this up and feel free to discuss what you think would be better!
I would personally prefer alias/reference as default behaviors for arrays and objects (as most of languages did). I think we can have some methods in stdlib for deep cloning
Closure capture... What do you find wrong?
Nothing wrong.
As we are already taking different paths from JavaScript (e.g. towards strong typing), I believe that eventually we'll need to decide whether wenyan should follow other JavaScript design choices.
Currently in wenyan-lang arrays are aliased
... another behavior, e.g. shallow copy, deep-copy, copy-assignment in C, etc.
I suggest that we do either of the following, but not a mixture of them (e.g. Java String), for each particular situation (數/爻/言/列/物/術 variables, function arguments, closure captures, etc.).
Value semantic
This is the current behavior of 數/爻.
Reference semantic
This is close to the current behavior of 列/物, except that their default values are new Array
and new Object
.
Alias semantic
This is the current behavior of closure captures.
Value/reference semantic
Should the code above print [] [1] or [1] [1]?
Should the code above print 1 [] 2 [3] or 1 [3] 2 [3] or 2 [3] 2 [3]?
Should the code above compile? If so, should it print 1 or -1?
Should the code above compile? If so, should it print [] or [1]?
Should the code above compile? If so, should it print [] or [1]?
Closure capture
Should the code above compile and print 2?
Should the code above compile and print 2?
Should the code above compile? If so, should it print 2 or 3?
Should the code above compile? If so, should it print 1 2 3 3 2 1 or 1 3 6 3 5 6 or 1 3 6 9 11 12?