Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/vimPublic

Add Vim9 generic function support#17313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
yegappan wants to merge1 commit intovim:masterfromyegappan:generics

Conversation

yegappan
Copy link
Member

@yegappanyegappan commentedMay 14, 2025
edited
Loading

A generic function definition:

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

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

girishji, habamax, briancarbone, and nda-cunh reacted with thumbs up emojidkearns, Konfekt, briancarbone, jmdevin, vimpostor, FernandoBasso, techntools, ddad431, Maverickx0, gmichokostas, and dezza reacted with hooray emojibriancarbone, FernandoBasso, lifepillar, lxhillwind, and nda-cunh reacted with heart emojibriancarbone, FernandoBasso, and bfredl reacted with eyes emoji
@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)
vim9scriptdef Add<T1, T2, T3>(x: T1,y: T2): T3return<T3>(x+y)enddefecho 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)
vim9scriptclass MendclassclassNendclassclass OendclassclassPendclassclass Qendclassexportclass Framework<A, B, C, D, E>def_new(F:func,G:func)throw'TODO'enddefstaticdef 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)))enddefendclass

Can these issues be looked into?

(issues_c.vim)
vim9scriptinterface I    def string(): stringendinterfaceclass Aendclassclass B extends A implements I    def string(): string        return "B"    enddefendclass# 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()enddefdefcompile GenericString1def GenericString2<T>(o: T): string    return o.string()enddefecho 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 maybeFoo<>('abc', 10)
habamax reacted with thumbs up emojijmdevin reacted with heart emoji

@yegappanyegappanforce-pushed thegenerics branch 5 times, most recently fromed77edd tofd5fb23CompareMay 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
MemberAuthor

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

ScriptScorpion reacted with thumbs up emoji

@yegappanyegappanforce-pushed thegenerics branch 14 times, most recently fromc7cfaca to75ea81bCompareMay 28, 2025 04:33
@yegappanyegappanforce-pushed thegenerics branch 3 times, most recently from0ff2ce8 todac21ebCompareMay 31, 2025 05:57
@dkearns
Copy link
Contributor

dkearns commentedJul 17, 2025
edited
Loading

vim9scriptdef Foo<T>(a: T)varb: T=aenddefdef Foo

outputs

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

@errael
Copy link
Contributor

vim9scriptdef Foo<T>(a: T)varb: T=aenddefdef Foo

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

Doesn't seem to be documented.

@yegappanyegappanforce-pushed thegenerics branch 4 times, most recently from41250d4 toec5541cCompareJuly 19, 2025 19:08
@yegappan
Copy link
MemberAuthor

vim9scriptdef Bar<T>()echomsg"Bar"enddefvar 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.

zzzyxwvut and dkearns reacted with thumbs up emoji

@yegappan
Copy link
MemberAuthor

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

Supporting generic constructors forenums is hardly of any priority right now, but I hope, together withprotected constructors forabstract classes andenums, 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.

zzzyxwvut reacted with thumbs up emoji

@yegappan
Copy link
MemberAuthor

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

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

This is supported now in the latest updated PR.

zzzyxwvut reacted with thumbs up emoji

@zzzyxwvut
Copy link
Contributor

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

lifepillar reacted with laugh emoji

@yegappan
Copy link
MemberAuthor

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

@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
MemberAuthor

@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
MemberAuthor

Thanks, so I assume this is ready now?

@chrisbra Yes. This PR is ready.

@dkearns
Copy link
Contributor

@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.

dkearns and guidom-pluribus-one reacted with hooray emoji

dkearns added a commit to dkearns/vim that referenced this pull requestJul 23, 2025
Match Vim9 generic functions, added invim#17313.Signed-off-by: Doug Kearns <dougkearns@gmail.com>
chrisbra pushed a commit that referenced this pull requestJul 23, 2025
Match Vim9 generic functions, added in#17313.closes:#17722Signed-off-by: Doug Kearns <dougkearns@gmail.com>Signed-off-by: Christian Brabandt <cb@256bit.org>
zeertzjq added a commit to zeertzjq/neovim that referenced this pull requestSep 29, 2025
…nctionsMatch Vim9 generic functions, added invim/vim#17313.closes:vim/vim#17722vim/vim@72473ceCo-authored-by: Doug Kearns <dougkearns@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@dkearnsdkearnsdkearns left review comments

@h-easth-easth-east left review comments

+1 more reviewer

@zzzyxwvutzzzyxwvutzzzyxwvut left review comments

Reviewers whose approvals may not affect merge requirements
Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

9 participants
@yegappan@zzzyxwvut@vimpostor@ScriptScorpion@dkearns@chrisbra@lifepillar@errael@h-east

[8]ページ先頭

©2009-2025 Movatter.jp