You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/wiki/annotations.mdx
+14-8Lines changed: 14 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -646,7 +646,7 @@ local Numbers = {}
646
646
647
647
### @generic
648
648
649
-
Generics allow code to be reused and serve as a sort of "placeholder" for a type. Surrounding the generic in backticks (<code>`</code>) will capture the value and use it for the type. [Generics are still WIP](https://github.com/LuaLS/lua-language-server/issues/1861).
649
+
Generics allow code to be reused and serve as a sort of "placeholder" for a type. Surrounding the generic in backticks (<code>`</code>) will capture the string value from the argument and infer the named class/type as the generic type. [Generics are still WIP](https://github.com/LuaLS/lua-language-server/issues/1861).
650
650
651
651
**Syntax**
652
652
@@ -680,16 +680,22 @@ local v3, v4 = Generic(10)
680
680
681
681
```Lua
682
682
---@classVehicle
683
-
localVehicle= {}
684
-
functionVehicle:drive() end
685
683
686
684
---@genericT
687
-
---@paramclass`T` # the type is captured using `T`
688
-
---@returnT # generic type is returned
689
-
localfunctionnew(class) end
685
+
---@paramclass`T` # the type is captured
686
+
---@returnT # The return type will be the class/type inferred from the string value of the `class` argument
687
+
localfunctionnewWithCapture(class) end
688
+
689
+
-- obj1 has type `Vehicle` here, because "Vehicle" is captured from the provided argument
690
+
localobj1=newWithCapture("Vehicle")
691
+
692
+
---@genericT
693
+
---@paramclassT # the type is NOT captured
694
+
---@returnT # The return type will be the same as the type of the `class` argument
695
+
localfunctionnewWithoutCapture(class) end
690
696
691
-
--obj: Vehicle
692
-
localobj=new("Vehicle")
697
+
--obj2 has type `string` here, because the provided argument is a string
0 commit comments