Yuescript icon indicating copy to clipboard operation
Yuescript copied to clipboard

Method invocation operator suggestion

Open nikneym opened this issue 4 years ago • 15 comments

It would be nice if there was an alternative or maybe complete new (though I'm sure that people have used to it) syntax for method calling. Backslash \ is both hard to type in some localized keyboards and looks unexpected. My suggestion is using : which is same one of Lua or something like C member calls ->.

-- instead of
Player\update dt

Player:update dt
Player->update dt

with Player
  :update dt
  ->update dt

Don't know if either of these syntaxes would break the compiler since I'm unaware of architecture of the Yue compiler but I've still wanted to suggest. Thanks!

nikneym avatar Apr 21 '21 19:04 nikneym

I've been thinking of two ways to solve this.

  1. Since Player:update dt and Player ->update dt are valid syntax for the moment, it won't be a good idea to simply override the syntax with different meanings. We can make use of some other combined symbols to make an alias for method calling. For example Player:>update dt.

  2. Or we can implement the infix macro operator feature to support custom operators. This feature could be like:

macro . = (a, b)-> "#{a}\#{b}"
Player$.update dt
-- or
macro : = (a, b)-> "#{a}\#{b}"
Instace$:getPlayer!$:update dt

Compiles to:

Player:update(dt)
(Instace:getPlayer()):update(dt)

pigpigyyy avatar Apr 22 '21 08:04 pigpigyyy

Hmm, I think first solution fit there better since second one requires macro operator in between. :> looks cool, maybe directly Player>update dt?

nikneym avatar Apr 22 '21 12:04 nikneym

Player>update dt is not available because > is greater than comparison operator here. No binary operator can be reused for method call including <=, >=, ~=, !=, ==, .., <<, >>, //, +, -, *, /, %, >, <, |, &, ~. So we have to choose a different operator.

pigpigyyy avatar Apr 22 '21 14:04 pigpigyyy

IMHO this "feature" make no sense.

CriztianiX avatar Apr 22 '21 15:04 CriztianiX

Well, I think this feature has its value for coding with some special keyboard layout. Googled it, it seems to be not easy for the Spanish keyboard layout to type backslash character.

pigpigyyy avatar Apr 22 '21 16:04 pigpigyyy

Well... I write some code with dialogue and this idle come to mind:

npc\talk "Hello" -- It look bad!
npc@talk "Hello" -- It look more better!

How about level up '@'. '@' already mean 'self' but in some case it not use well. EX:

me@talk "Hello!"
npc@talk "WTF?"
return

-->> Lua:

me(self:talk("Hello!")) -- 1
npc(self:talk("WTF?")) -- 2
return -- 3

Maybe it should better if:

me:talk("Hello!") -- 1
npc:talk("WTF?") -- 2
return -- 3

I thing use '@' is more readability than ''. Maybe we can keep '\', just add support '@' without break old script! It look like there are not any use for code like "func@prop" in old syntax anyway! Not sure but correct me if I wrong here~

Thanks and regards!

thesolitudeofnoblesoul avatar Apr 30 '21 09:04 thesolitudeofnoblesoul

I agree. \ is not convenient to type (qwerty keyboard user here). It's one of the things that turn me off into trying moonscript

flamendless avatar Apr 30 '21 09:04 flamendless

Using @ will cause ambiguous problem with the "self" indexing syntax under "with" statement.

with tb
  @item 123
  \item 123

is currently

local _with_0 = tb
self:item(123)
_with_0:item(123)

So we have to disable the use of "self" symbol to do method calling with @. And if we have to choose a single symbol to be the alternative for \, the only choice that is not causing problem could be backquote `. Is there any better idea for choosing symbols?

pigpigyyy avatar May 01 '21 02:05 pigpigyyy

I somewhat skeptical about use '`' for any syntax except quote just because it is valable. It really strange because it itself name backquote and natural expected mean to quote things.

Uhm... I look like we out of any single simple, then how about:

me.@talk "abc"

It looks more verbose and should not conflict with anything.

thesolitudeofnoblesoul avatar May 01 '21 03:05 thesolitudeofnoblesoul

What about ~~->~~ or :: ?

EDIT: i've found out that -> is already used for function definition, so it's just :: then

flamendless avatar May 01 '21 07:05 flamendless

IMO @ operator is really good replacement for self and I'm sure most of the projects use @ instead of self. So giving it a second meaning may confuse people and make the code hard to maintain. In a second thought, I guess -> would also make code hard to read since it's already used for function definition.

Creating a custom macro for method invocation would make users to have to remember custom-defined macros each project. So similar problems with -> or @ continues.

My final conclusion: I think it makes no sense to use single character for such case since neither . nor : are options and available characters like ^, | or backquote have the same issue with \ since most of these operators are rarely used in invocation at other languages, it makes code look alien. My final vote is on :: since it's not overlapping with any other operator. I know it's not excessively common for such thing, but at least it looks more understandable compared to \. Also some other languages use it for similar purposes (C#, C++).

And lastly, it's a spiritual continuation of :

nikneym avatar May 01 '21 13:05 nikneym

The .@ symbols seem reasonable for Yuescript but is not seen in other languages for similar purpose. So as a C++ programer, I feel more comfortable with the idea for :: too. But this will break a syntax for

_ = a::b

which was compiled to

local _ = {
  a = {
    b = b
  }
}

now compiles to

-- same as "_ = a\b"
local _
do
  local _base_0 = a
  local _fn_0 = _base_0.b
  _ = _fn_0 and function(...)
    return _fn_0(_base_0, ...)
  end
end

I just implemented this in a new commit and is going to add more tests for the new operator.

pigpigyyy avatar May 01 '21 16:05 pigpigyyy

To add type annotation feature for issue #15, the alternative method calling symbol was changed to .@ instead of ::. And :: is being used as a starting symbol of type annotation.

tb\func!
tb.@func!

s::str = "abc"
add = (a::num, b::num)->::num a + b

pigpigyyy avatar Sep 01 '21 02:09 pigpigyyy

Googled it, it seems to be not easy for the Spanish keyboard layout to type backslash character.

Then how do Spanish people use the Windows command prompt or Run dialog?

Sod-Almighty avatar Nov 16 '21 10:11 Sod-Almighty

https://www.answers.com/Q/Where_is_the_backslash_located_on_a_spanish_keyboard

pigpigyyy avatar Nov 17 '21 01:11 pigpigyyy

Method invocation operator is now using :: as an aliasing for \ due to the type annotation feature is abandoned.

pigpigyyy avatar Jan 13 '23 07:01 pigpigyyy