Skip to content

vim9: Make Autoload Functions Easier to Define and Call #9435

@rbong

Description

@rbong

Is your feature request about something that is currently impossible or hard to do? Please describe the problem.

As the language is being overhauled I believe we should make autoload functions nicer and easier to convert to other languages.

I've included some quotations from the vim 9 doc to support this idea.

we can also make Vim script easier to use. In other words: "less weird"

Compared with other languages, prefixing the file name on every function call is weird and awkward.
The only other option for public functions is the global namespace, which practically also requires some name prefix.

It should be possible to convert code from other languages to Vim script.

For a project with a public API, my options with vim9script function naming are g:SomeLongPrefix or some#long#prefix#.
Naming prefixes like this are not required in any other language.
This makes conversion more awkward than anything else with vim9script.

This becomes worse the more deeply nested autoload files are.
Using the global namespace is no better.

Describe the solution you'd like

Add a command to define the current autoload file and automatically prefix this name.
Allow "importing" functions directly from autoload files without pre-loading them.

Example:

In Vim9 script:

vim9script

# This prefixes function names with "my_plugin#nested#"
setautoload my_plugin#nested#

def foo()
  echo "Foo"
enddef

# To avoid name conflicts, you can also manually define function names.
def my_plugin#nested#bar()
  echo "Bar"
enddef

This is equivalent to the following in legacy vimscript:

def my_plugin#nested#foo()
  echo "Foo"
enddef

def my_plugin#nested#bar()
  echo "Bar"
enddef

You could also import this function:

vim9script

# Replaces all calls to "foo" with "my_plugin#nested#foo"
import { foo } from autoload "my_plugin#nested#"

# Replaces all calls to "nested_foo" with "my_plugin#nested#foo"
import { foo as nested_foo } from autoload "my_plugin#nested#"

Using this, functions still would not be loaded until they were actually called.

Describe alternatives you've considered

Autoloading could be automatic, or autoload names could automatically be prefixed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions