Generics in GDScript

# Starting with version 4.0, Godot has added support for static typing,
# which provides some of the benefits of generics.

# As an example of a function with type hints, `slices_index` takes
# an array of any type and an element of that type and returns the 
# index of the first occurrence of v in s, or -1 if not present.
func slices_index(s: Array, v) -> int:
    for i in range(s.size()):
        if v == s[i]:
            return i
    return -1

# As an example of a custom type, `List` is a
# singly-linked list with values of any type.
class List:
    var head: Element
    var tail: Element

    class Element:
        var next: Element
        var val

    # We can define methods on custom types just like we
    # do on regular types.
    func push(v):
        if tail == null:
            head = Element.new()
            head.val = v
            tail = head
        else:
            tail.next = Element.new()
            tail.next.val = v
            tail = tail.next

    # all_elements returns all the List elements as an array.
    func all_elements() -> Array:
        var elems = []
        var e = head
        while e != null:
            elems.append(e.val)
            e = e.next
        return elems

func _ready():
    var s = ["foo", "bar", "zoo"]

    # When calling functions with type hints, we don't have to
    # specify the types - the engine infers them automatically.
    print("index of zoo:", slices_index(s, "zoo"))

    var lst = List.new()
    lst.push(10)
    lst.push(13)
    lst.push(23)
    print("list:", lst.all_elements())

# Output:
# index of zoo: 2
# list: [10, 13, 23]

In this GDScript translation:

  1. We’ve used GDScript’s static typing features to mimic some aspects of Go’s generics.

  2. The SlicesIndex function has been translated to slices_index, using GDScript’s naming conventions.

  3. The List type has been implemented as a custom class. In GDScript, we don’t need to specify generic types, as it’s a dynamically typed language.

  4. The Push and AllElements methods have been translated to push and all_elements respectively, following GDScript naming conventions.

  5. The main function has been replaced with the _ready() function, which is automatically called when the script is loaded.

  6. We’ve used GDScript’s print function instead of fmt.Println.

  7. Array literals in GDScript use square brackets [] instead of curly braces {}.

Note that GDScript doesn’t have true generics like Go does. The static typing in GDScript provides some similar benefits, but it’s not as powerful or flexible as Go’s generics system. This translation demonstrates the closest equivalent functionality in GDScript.