ucl/repl/typeprinter.go

21 lines
353 B
Go
Raw Permalink Normal View History

2024-12-11 11:30:14 +00:00
package repl
import (
"fmt"
"io"
"reflect"
)
func AddTypePrinter[T any](r *REPL, p func(w io.Writer, t T, concise bool) error) {
var t T
tt := reflect.TypeOf(t)
r.typePrinters[tt] = func(w io.Writer, v any, concise bool) error {
vt, ok := v.(T)
if !ok {
return fmt.Errorf("cannot convert %v to T", v)
}
return p(w, vt, concise)
}
}