Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@yegappan
Copy link
Member

@yegappan yegappan commented May 14, 2025

A generic function definition:

def Foo<A, B>(arg1: A, arg2: B): B
     var x: A = arg1
     return arg2
enddef

A generic function invocation:

Foo<string, number>('abc', 10)

The following are supported:

  1. Generic def method.
  2. Generic funcref
  3. Generic object/class method

The following are not yet supported:

  1. generic lambda
  2. generic class

@zzzyxwvut
Copy link
Contributor

Another huge feature in the works, thank you.

Can there be more than 26 capital letters to draw a type
variable name from? Maybe some trailing digits can be
optionally allowed after the first and only letter. It may
aid grouping multiple identifiers for a common type, e.g.:

(example_a.vim)
vim9script

def Add<T1, T2, T3>(x: T1, y: T2): T3
    return <T3>(x + y)
enddef

echo Add<number, number, number>(1, 2)
echo Add<float, float, float>(1.0, 2.0)

If there are plans to gradually introduce support for
generic classes, methods, and lambda expressions, it will
soon become more feasible to use up all 26 letters, e.g.:

(example_b.vim)
vim9script

class M
endclass

class N
endclass

class O
endclass

class P
endclass

class Q
endclass

export class Framework<A, B, C, D, E>
    def _new(F: func, G: func)
        throw 'TODO'
    enddef

    static def Init<I, J, K, L, R>(F: func, G: func, H: func): Framework<I, J, K, L, R>
        return Framework._new<I, J, K, L, R>(
            F((t: <T>, u: <U>, v: <V>, w: <W>) => H(t, u, v, w)),
            G((s: <S>, x: <X>, y: <Y>, z: <Z>) => H(s, x, y, z)))
    enddef
endclass

Can these issues be looked into?

(issues_c.vim)
vim9script

interface I
    def string(): string
endinterface

class A
endclass

class B extends A implements I
    def string(): string
        return "B"
    enddef
endclass




# FIXME: This declaration is ambiguous (in the presence of class A)  
# and should be rejected because the type variable A cannot be used  
# in the function body unless it shadows the identifier of class A.   
# In other words, another type variable name should be chosen (see   
# below) or the function should lose its generic variable and become  
# a non-generic function (see further below).
def GenericString1<A>(o: A): string
    return o.string()
enddef

defcompile GenericString1




def GenericString2<T>(o: T): string
    return o.string()
enddef

echo GenericString2<B>(B.new())

# OK...
echo GenericString2<I>(B.new())
# FIXME: E1325?  The method dispatch works for "I" (above) and doesn't  
# work for "A".  Either reject both calls since subtype instances "B"  
# are passed or correct dispatching for "A".
echo GenericString2<A>(B.new())




def NonGenericString(o: any): string
    return string(o)
enddef

# FIXME: No parsing error?
echo NonGenericString<number>(null)

@vimpostor
Copy link
Contributor

Foo<string, number>('abc', 10)

I wonder if it would be feasible to implement type inference (maybe later, not necessarily now):

Foo('abc', 10)
" or maybe
Foo<>('abc', 10)

@yegappan yegappan force-pushed the generics branch 5 times, most recently from ed77edd to fd5fb23 Compare May 20, 2025 14:30
@ScriptScorpion
Copy link

is it just new structure of function in vim9script?

@ScriptScorpion
Copy link

WOW no errors

@yegappan
Copy link
Member Author

is it just new structure of function in vim9script?

A generic function allows you to operate on different types of data without losing type checks and
reduces code duplication. For a general description of this in different programming languages,
you can refer to the following pages:

https://en.wikipedia.org/wiki/Generic_function
https://www.typescriptlang.org/docs/handbook/2/generics.html
https://docs.oracle.com/javase/tutorial/java/generics/types.html
https://learn.microsoft.com/en-us/cpp/extensions/generics-cpp-component-extensions?view=msvc-170
https://dart.dev/language/generics
https://go.dev/doc/tutorial/generics

@yegappan yegappan force-pushed the generics branch 14 times, most recently from c7cfaca to 75ea81b Compare May 28, 2025 04:33
@yegappan yegappan force-pushed the generics branch 3 times, most recently from 0ff2ce8 to dac21eb Compare May 31, 2025 05:57
@dkearns
Copy link
Contributor

dkearns commented Jul 17, 2025

vim9script
def Foo<T>(a: T)
  var b: T = a
enddef
def Foo

outputs

   def <SNR>65_Foo(a: any)
1    var b: T = a
   enddef

@errael
Copy link
Contributor

errael commented Jul 17, 2025

vim9script
def Foo<T>(a: T)
  var b: T = a
enddef
def Foo

Off topic, I didn't know, or had forgotten, that def Foo outputs annotated source of the function.

Doesn't seem to be documented.

@yegappan yegappan force-pushed the generics branch 4 times, most recently from 41250d4 to ec5541c Compare July 19, 2025 19:08
@yegappan
Copy link
Member Author

vim9script

def Bar<T>()
  echomsg "Bar"
enddef

var B = Bar<string>

disassemble B

outputs

Error detected while processing /tmp/gen.vim:
line   18:
E1061: Cannot find function B

Thanks for reporting this issue. This issue should be addressed in the latest updated PR.

@yegappan
Copy link
Member Author

Set and List operations can now be generified (see below)!

Supporting generic constructors for enums is hardly of any priority right now, but I hope, together with protected constructors for abstract classes and enums, this may be revisited at a later date.

Congratulations and thank you for developing this feature, @yegappan.

(list.vim)
(set.vim)

Thanks for creating these example scripts. This exposed some existing memory leaks which were tricky to fix.
All of these should be addressed now in the latest updated PR.

@yegappan
Copy link
Member Author

It is nice that constructors can be parameterised like other functions and methods:

(example_k.vim)
Creating enum values with a generic constructor then doesn't seem far-fetched (E1123):

This is supported now in the latest updated PR.

@zzzyxwvut
Copy link
Contributor

I'm glad that this is taken care of. Oops, I don't have any
broken code to share.

@yegappan
Copy link
Member Author

I'm glad that this is taken care of. Oops, I don't have any broken code to share.

Thanks for continuously testing and reporting the problems.

@chrisbra
Copy link
Member

Thanks, so I assume this is ready now?

@dkearns
Copy link
Contributor

dkearns commented Jul 20, 2025

@yegappan, I'm sorry about that merge commit, I haven't the faintest idea where that came from. You may want to force push again.

I have a 22 year old cat with hyperthyroidism and a love of keyboard dancing as my initial suspect.

@yegappan
Copy link
Member Author

@yegappan, I'm sorry about that merge commit, I haven't the faintest idea where that came from. You may want to force push again.

I have a 22 year old cat with hyperthyroidism and a love of keyboard dancing as my initial suspect.

No problem. I did a force push again.

@yegappan
Copy link
Member Author

Thanks, so I assume this is ready now?

@chrisbra Yes. This PR is ready.

@dkearns
Copy link
Contributor

dkearns commented Jul 21, 2025

@yegappan and @chrisbra, I don't really consider it a blocker but, just in case it's been overlooked, function listing still needs some work.

See: #17313 (comment)

@chrisbra
Copy link
Member

thanks all. Let me merge it and we can do further improvements with a followup PR for the function listing.

@chrisbra chrisbra closed this in 3416cee Jul 21, 2025
dkearns added a commit to dkearns/vim that referenced this pull request Jul 23, 2025
Match Vim9 generic functions, added in vim#17313.

Signed-off-by: Doug Kearns <[email protected]>
chrisbra pushed a commit that referenced this pull request Jul 23, 2025
Match Vim9 generic functions, added in #17313.

closes: #17722

Signed-off-by: Doug Kearns <[email protected]>
Signed-off-by: Christian Brabandt <[email protected]>
zeertzjq added a commit to zeertzjq/neovim that referenced this pull request Sep 29, 2025
…nctions

Match Vim9 generic functions, added in vim/vim#17313.

closes: vim/vim#17722

vim/vim@72473ce

Co-authored-by: Doug Kearns <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants