package main

import (
	"fmt"

	"golang.org/x/exp/constraints"
)

// section: function-vars
// Max returns the maximum of two values
func Max[T constraints.Ordered](a, b T) T {
	if a > b {
		return a
	}
	return b
}

func main() {
	// When assigning a generic function to a variable,
	// we need explicit type instantiation

	// This would NOT compile:
	// var maxFunc = Max  // compiler error: cannot infer types

	// This works - explicit instantiation:
	var maxInt func(int, int) int = Max[int]
	var maxString func(string, string) string = Max[string]

	// Now we can use the function variables
	fmt.Printf("Max integers: %d\n", maxInt(5, 8))
	fmt.Printf("Max strings: %s\n", maxString("apple", "banana"))

	// We can also use shorter syntax:
	maxFloat := Max[float64]
	fmt.Printf("Max floats: %.2f\n", maxFloat(3.14, 2.71))
}

// section: function-vars
