#How to switch over type parameter

3 messages · Page 1 of 1 (latest)

remote rover
#
func (i *Interpreter[any]) VisitUnaryExpr(expr Unary[any]) any {
    right := i.evaluate(expr.Right)
    switch expr.Operator.Type {
    case MINUS:
        switch any(right).(type) {
        case int:
            return -right.(int)
        }
    }
}
cannot use type switch on type parameter value any(right) (value of type any/* type parameter */ constrained by any)compilerInvalidTypeSwitch
#

🤷‍♀️

remote rover
#
func identity[T any](t T) T {
    return t
}

func switchOver[T any](param T) {
    i := identity(param)
    switch i.(type) {
    case int:
        println("int")
    }
}