Added custom printers

This commit is contained in:
Leon Mika 2024-12-11 22:30:14 +11:00
parent 62724e3f37
commit 7a92eeddac
7 changed files with 287 additions and 63 deletions

20
repl/typeprinter.go Normal file
View file

@ -0,0 +1,20 @@
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)
}
}