21 lines
353 B
Go
21 lines
353 B
Go
|
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)
|
||
|
}
|
||
|
}
|