[vim/vim] "Dictionary required" when referencing class members returned from autoloaded functions inside functions (Issue #12198)

85 views
Skip to first unread message

mawkish

unread,
Mar 25, 2023, 12:55:31 PM3/25/23
to vim/vim, Subscribed

Steps to reproduce

  1. Put the following in ~/.vim/autoload/autoloaded_script.vim:
vim9script

export class My_class
	this.hello: string
endclass

export def Return_my_class(): My_class
	var r = My_class.new('From my_test_autoloaded_script')
	return r
enddef
  1. Source the following in Vim:
vim9script

import autoload 'autoloaded_script.vim'

def My_func()
	var whatever = autoloaded_script.Return_my_class()
	echo whatever.hello
enddef

My_func()

You'll see E715: Dictionary required. This doesn't happen if you move the code outside the function; it doesn't happen if you declare the class inside the script you're sourcing. It doesn't happen when you call the class's constructure inside your test file.

I'm guessing that this issue stems from the same space as that those mentioned in #11822 and #12024.

Expected behaviour

You'd expect it to print "From my_test_autoloaded_script".

Version of Vim

9.0.1427

Environment

Fedora 37
Kitty
xterm-kitty
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/12198@github.com>

Virginia Senioria

unread,
Mar 26, 2023, 1:47:48 AM3/26/23
to vim/vim, Subscribed

These issues, along with #12196, may originate from the same reason: Vim rely on the typing information it knows at the point when the code is compiled to generate proper instructions, and for member access with any/unknown type as its lhs, Vim would generate STRINGMEMBER instruction to index the member by its name; the problem is that the instruction only accept dictionaries as lhs, thus the error is raised.

So as a workaround now, give a more precise explicit typing can solve this: it forces Vim to generate proper instructions to access the correct member. This is an example:

a.vim:

vim9script

export class T
    this.n: string
    def F()
        echom "F:" this.n
    enddef
endclass
export def RT(): T
    return T.new("QWQ")
enddef

b.vim:

vim9script

import autoload "./a.vim"

def F()
    var v: a.T = a.RT()
    echo v.n
    v.F()
enddef

F()

a: a.T forces Vim to generate the correct instruction.

BTW, since the use of any is inevitable now, I think maybe the only solution is to extend STRINGMEMBER instruction, allowing it to access class members and functions.


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/12198/1484002337@github.com>

mawkish

unread,
Mar 28, 2023, 7:20:25 AM3/28/23
to vim/vim, Subscribed

Thank for this workaround; adding the typing hadn't occurred to me.


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/12198/1486675255@github.com>

errael

unread,
Apr 19, 2024, 11:19:49 AM4/19/24
to vim/vim, Subscribed

Just an FYI, this has nothing to do with autoload. Putting the two files in the same directory, with a primary file that looks like the following also fails.

vim9script

import './f2.vim'

def My_func()
	echo f2.Return_my_class().hello
enddef

My_func()


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/12198/2066794239@github.com>

Christian Brabandt

unread,
Nov 11, 2024, 2:07:04 PM11/11/24
to vim/vim, Subscribed

Closed #12198 as completed via 56d45f1.


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/12198/issue_event/15254416992@github.com>

Reply all
Reply to author
Forward
0 new messages