[vim/vim] bug with instanceof / interface (Discussion #19128)

10 views
Skip to first unread message

nda-cunh

unread,
Jan 7, 2026, 9:07:12 PM (2 days ago) Jan 7
to vim/vim, Subscribed

Hi, i have this error with vim9.1
Error detected while compiling function SupraTreeBuffer.OnRemove:
line 4:
E1012: Type mismatch; expected object but got object

interface Deletable
	def SetDeleted()
endinterface

# .....
class FileNode extends Node implements Deletable
# .....

def OnRemove()
    const lnum = line('.')
    var node = this.table_actions[lnum - 1]
    if node->instanceof(Deletable)
	    var deletable: Deletable = <any>node
	    deletable.SetDeleted()
    endif
    this.RefreshKeepPos()
enddef

And i can't use node directly :/ How can i make this ? Thank !


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

Yegappan Lakshmanan

unread,
Jan 8, 2026, 1:50:47 AM (yesterday) Jan 8
to vim/vim, Subscribed

Can you provide a complete sample script/code to reproduce 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/repo-discussions/19128/comments/15441354@github.com>

nda-cunh

unread,
Jan 8, 2026, 2:04:18 AM (yesterday) Jan 8
to vim/vim, Subscribed

vim9script

def g:CompileMe()
	var t: Test = Test.new()
	t.OnRemove()
enddef

interface Deletable
	def SetDeleted()
endinterface

class Node
endclass

class FileNode extends Node implements Deletable
	def SetDeleted()
		echom 'FileNode marked as deleted.'
	enddef
endclass


class Test
	var node: Node
	def Print()
		this.node = FileNode.new()
	enddef

	def OnRemove()
		var node = this.node
		if node->instanceof(Deletable)
			var deletable: Deletable = node
			deletable.SetDeleted()
		endif
	enddef
endclass


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/19128/comments/15441443@github.com>

nda-cunh

unread,
Jan 8, 2026, 5:01:59 AM (yesterday) Jan 8
to vim/vim, Subscribed

Tell me there's a way to cast and retrieve the class, because I based my entire code structure on it 😭


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/19128/comments/15443147@github.com>

Yegappan Lakshmanan

unread,
Jan 8, 2026, 11:32:29 AM (yesterday) Jan 8
to vim/vim, Subscribed

The variable "node" is an instance of the class "Node" which doesn't implement the interface "Deletable". You are trying to assign "node" to the variable "deletable" which is an instance of the interface "Deletable". You can cast "node" to "FileNode" in the assignment.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/19128/comments/15446844@github.com>

nda-cunh

unread,
Jan 8, 2026, 3:34:35 PM (yesterday) Jan 8
to vim/vim, Subscribed

In my actual code, I have Node classes that are Delicious and others that are not.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/19128/comments/15448679@github.com>

nda-cunh

unread,
Jan 8, 2026, 3:40:31 PM (yesterday) Jan 8
to vim/vim, Subscribed

node->instanceof(Deletable) tel me node is Délectable, it need return the cast or we need another keyword like "as" or "is" for that ex:

like vala:

if node is Deletable
// here node is cast to Deletable
endif

or like in cpp
with dynamic cast dynamic_cast
it return the Deletable instance or null if not found

Otherwise, I don't really see the point of interfaces since the code cannot be modular.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/19128/comments/15448739@github.com>

Aliaksei Budavei

unread,
Jan 8, 2026, 6:09:29 PM (23 hours ago) Jan 8
to vim/vim, Subscribed

You need to downcast this.node to a Deletable type:

E.g.:

var deletable: Deletable = <FileNode>node
# OR:
var deletable: Deletable = <Deletable>(<FileNode>node)

See :help type-casting.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/19128/comments/15449605@github.com>

nda-cunh

unread,
Jan 8, 2026, 6:42:48 PM (23 hours ago) Jan 8
to vim/vim, Subscribed

sorry but it not work we can't cast it:


E1012: Type mismatch; expected object but got object


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/19128/comments/15449757@github.com>

Aliaksei Budavei

unread,
Jan 8, 2026, 6:52:38 PM (23 hours ago) Jan 8
to vim/vim, Subscribed

Patch your example as follows:

--- orig.vim
+++ patch.vim
@@ -21,16 +21,17 @@ endclass
 
 class Test
 	var node: Node
-	def Print()
+	def new()
 		this.node = FileNode.new()
 	enddef
 
 	def OnRemove()
 		var node = this.node
 		if node->instanceof(Deletable)
-			var deletable: Deletable = node
+			var deletable: Deletable = <FileNode>node
 			deletable.SetDeleted()
 		endif
 	enddef
 endclass
 
+g:CompileMe()

and give it a try. If it still fails, post your :version.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/19128/comments/15449793@github.com>

nda-cunh

unread,
Jan 8, 2026, 7:09:46 PM (22 hours ago) Jan 8
to vim/vim, Subscribed

with your patch i have this error too:
line 3:


E1012: Type mismatch; expected object but got object

my version is """
vim --version
VIM - Vi IMproved 9.1
"""


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/19128/comments/15449861@github.com>

Aliaksei Budavei

unread,
Jan 8, 2026, 7:36:17 PM (22 hours ago) Jan 8
to vim/vim, Subscribed

Look for a more recent version of Vim. The patched example
works at least with v9.1.1012.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/19128/comments/15449977@github.com>

nda-cunh

unread,
Jan 8, 2026, 7:49:50 PM (22 hours ago) Jan 8
to vim/vim, Subscribed

The cast works with the latest version of Vim. Too bad I'm stuck on an older version. However, I still think Vim should do the cast itself after an if instanceof, and that a keyword is better than a function like instanceof(). :/


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/19128/comments/15450041@github.com>

Reply all
Reply to author
Forward
0 new messages