This won't work for the general case, but since it appears you're working with your own type (at least in your example), and assuming you don't want to affect ToString
, you could do something like this:
type ITransformable =
abstract member BoxedValue : obj
type MyList<'T>(values: seq<'T>) =
interface ITransformable with
member x.BoxedValue = box values
fsi.AddPrintTransformer(fun (x:obj) ->
match x with
| :? ITransformable as t -> t.BoxedValue
| _ -> null)
Output:
> MyList([1;2;3])
val it : MyList<int> = [1; 2; 3]
For a third-party generic type you could use AddPrintTransformer
and reflection to get the value to be displayed. An interface is just easier if you have the source.