[vim/vim] vim9class can't use this keyword in lambda function (Issue #12336)

14 views
Skip to first unread message

Shinya Ohyanagi

unread,
May 3, 2023, 8:43:35 AM5/3/23
to vim/vim, Subscribed

Steps to reproduce

vim9script

class Counter
  this.counter = 10
  def new()
  enddef

  def Up()
    timer_start(1000, (_time) => {
      echomsg this.counter
      this.counter = this.counter + 1
    }, { repeat: -1 })
  enddef
endclass

const counter = Counter.new()
counter.Up()
  1. open Vim and :so %
  2. Error shows
Error detected while compiling /tmp/a.vim[17]..function Up[1]..<lambda>68:
line    2:
E1034: Cannot use reserved name this

If I remove this.counter = this.counter + 1 line, no error raised.

Expected behaviour

It would be great if this keyword can use in lambda function like JavaScript's arrow function this.

Notes:

I know I can use like followings

vim9script

class Counter
  this.counter = 10
  def new()
  enddef

  def Up()
    const self = this
    timer_start(1000, (_time) => {
      echomsg self.counter
      self.counter = self.counter + 1
    }, { repeat: -1 })
  enddef
endclass

const counter = Counter.new()
counter.Up()

Version of Vim

Vim9.1474

Environment

OS: macOS
Terminal: Terminal.app
value of $TERM: xterm-256color
shell: zsh

Logs and stack traces

No response


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/12336@github.com>

Rodgers Nyakundi

unread,
May 5, 2023, 1:33:38 PM5/5/23
to vim/vim, Subscribed

Vimscript "this" is a reserved keyword referring to the current object same to "self" in other programming language. to fix it rename "this" to something else eg self or obj


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/12336/1536568730@github.com>

Bram Moolenaar

unread,
May 13, 2023, 10:38:10 AM5/13/23
to vim/vim, Subscribed

Looks like a duplicate of #12076. If you agree you can close this issue.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/12336/1546666962@github.com>

Bram Moolenaar

unread,
May 19, 2023, 2:02:00 PM5/19/23
to vim/vim, Subscribed

Closed #12336 as completed via 2bd6a09.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/12336/issue_event/9291621058@github.com>

Reply all
Reply to author
Forward
0 new messages