HerculesWS / Hercules

Hercules is a collaborative software development project revolving around the creation of a robust massively multiplayer online role playing game (MMORPG) server package. Written in C, the program is very versatile and provides NPCs, warps and modifications. The project is jointly managed by a group of volunteers located around the world as well as a tremendous community providing QA and support. Hercules is a continuation of the original Athena project.
http://herc.ws
GNU General Public License v3.0
900 stars 758 forks source link

[DIS] array syntactic sugar #1667

Open Helianthella opened 7 years ago

Helianthella commented 7 years ago

I wanna hear your thoughts before I start implementing some script shorthands so we can all agree on the syntax first.


array shorthand methods





math shorthand methods

old proposal (click here) ### array shorthand methods * `.@var = 1, 2, 3, 4, 5;` * shorthand for `setarray .@var[0], 1, 2, 3, 4, 5;` * `.@var = 1...5;` * shorthand for `setarray .@var[0], 1, 2, 3, 4, 5;` * `.@var = ...#var2;` * passes every element of `#var2` as an argument to the array shorthand method * shorthand for `copyarray(.@var[0], #var[0], getarraysize(#var[0]));` * `some_buildin(...array);` * passes every element of `array` as arguments to `some_buildin` ### destructuring * `.@a, .@b, .@c = .@d, .@e, .@f;` * shorthand for `.@a = .@d; .@b = .@e; .@c = .@f;` * `.@a, .@b, .@c = ....@d;` * shorthand for `.@a = .@d[0]; .@b = .@d[1]; .@c = .@d[2];` ### math shorthand methods * `5 ** 2` * shorthand for `pow(5, 2)`
Murilo-BiO commented 7 years ago

Something that would be great is multidimensional arrays. Is it possible to make with the current script engine?

Ridley8819 commented 7 years ago

@Murilo-BiO


eventInfo = [
    "reward": [
        "id": Apple,
        "amount": 20
    ]
];
Murilo-BiO commented 7 years ago

@Ridley8819 sent an example that I made in a conversation about changes in script engine, I suggested the multidimensional arrays with more readable keys like in PHP arrays

sagunkho commented 7 years ago

I'm going to have to disagree on most due to lack of flexibility or unnecessary processing for simple function calls. I strongly believe that code should be easily understandable. Some of the syntaxes such as ... imply "magic" happening. There is no magic in code (so to speak).

Might I suggest considering the following - .@var = [1, 2, 3, 4, 5]; for setarray .@var[0], 1, 2, 3, 4, 5;. and so if ever implementing multi-dimensional arrays, we could have the following variant(s) - .@var = [ [1, 2], [3, 4], [5, 0]]; and so on.

as for the math shorthand method, the python syntax is ok.

4144 commented 7 years ago

If add brackets, better add it like in C. this mean

.@var = {1, 2, 3, 4, 5};
.@var = {{1, 2}, {3, 4}}
Helianthella commented 7 years ago

@Smokexyz

Some of the syntaxes such as ... imply "magic" happening. There is no magic in code (so to speak).

and yet many languages use this:

(and so on)

sagunkho commented 7 years ago

@mekolat by magic I meant in your example you use 3 dots for assigning to a variable - .@var = 1...5; and also passing each index by value to another .@var = ...#var2;

Helianthella commented 7 years ago

well we could drop the range one (1...5) so that it's only ever used for arrays

sagunkho commented 7 years ago

@mekolat Sounds good to me 👍

MishimaHaruna commented 7 years ago

I really like these ideas, especially the array and math shorthands. Destructuring seems quite dangerous, especially given the current state of things with the script engine, so I'd refrain for now (at least until parentheses are mandatory in function calls, and perhaps variables need to be declared).

I give my approval for:

I'm not sure about the copyarray shorthand right now. We could consider delaying it to after we remove the use of all those weird .@var, ##var, $@var sigils (so maybe at that point we can even use @array to refer to the whole array).

Note that the parser isn't very friendly, and likely contains bugs, especially around parenthesized expressions and the if() conditional, so I recommend caution.

Helianthella commented 7 years ago

We could consider delaying it to after we remove the use of all those weird .@var, ##var, $@var sigils (so maybe at that point we can even use @array to refer to the whole array).

Instead of pretty much rewriting the script engine why not switch to AngelScript or Lua altogether?

Murilo-BiO commented 7 years ago

If I were to pick a replacement language, I'd have gone for a sane, widely used language, such as ruby (1.8%), perl (2.2%), python (4.5%), not some exotic language with a very small market share and little support and resources (lua 0.6%; squirrel not in the top 100, angelscript not in the top 100). (note: figures from the TIOBE index, based on the most popular search engines)

@mekolat -> @MishimaHaruna Haru said this on #845

Helianthella commented 7 years ago

if you want something simple then yeah python makes sense, or even javascript (if we add nodejs bindings)