cast icon indicating copy to clipboard operation
cast copied to clipboard

doesn't handle named types

Open josharian opened this issue 3 years ago • 1 comments

I'd expect this test to pass:

func TestToFloat64NamedType(t *testing.T) {
	type X float64
	f := ToFloat64(X(2))
	if f != 2.0 {
		t.Fatalf("want %#v, got %#v", 2.0, f)
	}
}

The fix is pretty easy. Near the beginning of ToFloat64E, just after the call to indirect, add something like:

	v := reflect.ValueOf(i)
	if v.CanFloat() {
		return v.Float(), nil
	}

I'm filing an issue rather than sending a PR because the proper fix applies this to all conversions, not just floats, and that's a pretty substantive change, so I wanted to discuss first.

josharian avatar Dec 22 '22 18:12 josharian

Is anyone paying attention to this? I feel like it's a pretty common scenario, and there are also many named types in practical use that cannot be converted at the moment.

Tiny-Box avatar Aug 30 '23 13:08 Tiny-Box