Menu
CoddyTech

Generic Value Containers

Lesson 35 of 48 in Coddy's Swift for GDScript Developers course.

GDScript Variant fields can store values of different types without declaring a user-defined generic type parameter. Typed built-in arrays and dictionaries provide other constraints, but this custom Pair intentionally leaves its fields dynamic. Swift generics preserve a concrete element type per instantiation.

A Swift generic struct can return the same Element type it stores without erasing it. An unconstrained Element does not automatically support arithmetic or ordering; add protocol requirements only when needed. Copying the struct follows the semantics of its component values, including any reference sharing.

Both pairs exchange their left and right elements. The source class is a shared object, while the target is a value-type generic container whose typed behavior must work beyond the one String example.

Complete GDScript comparison program:

extends SceneTree
class Pair:
    var left
    var right
    func _init(a, b):
        left = a
        right = b
    func exchange() -> void:
        var old = left
        left = right
        right = old

func _init():
    var pair = Pair.new("east", "west")
    pair.exchange()
    print(pair.left, " ", pair.right)
    quit()

Complete Swift comparison program, compiled from these files:

Pair.swift

struct Pair<Element> {
    var left: Element
    var right: Element
    mutating func exchange() {
        let old = left
        left = right
        right = old
    }
}

main.swift

var pair = Pair(left: "east", right: "west")
pair.exchange()
print(pair.left, pair.right)
challenge icon

Challenge

Easy

Implement Stack<Element> as a value type with init(), mutating push(_:), mutating pop() -> Element?, and read-only count. Pop the newest remaining item, or nil if empty. It must work for Int and String and copies must be independent. Input has 0 through 50 integers -100 through 100; push all, then pop until empty, outputting popped integers separated by spaces.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Stack.swift, Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
var s = Stack<String>(); s.push("a"); s.push("b"); var c = s; guard c.pop() == "b" && c.count == 1 && s.count == 2 else { return false }; return s.pop() == "b" && s.pop() == "a" && s.pop() == nil
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Swift for GDScript Developers

Practice on your own: Swift playground