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.![]()
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.![]()
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.![]()
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.![]()
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.![]()
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.![]()
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.![]()
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.![]()
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.![]()
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.![]()
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.![]()
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.![]()
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.![]()