Skip to content

exported generic functions with generic type constraint interface = nil type #1700

@rcoreilly

Description

@rcoreilly

The following program sample.go triggers an unexpected result

type DataTypes interface {
	string | bool | float32 | float64 | int | int32 | uint32 | byte
}

func MyFun[T DataTypes]() T { //yaegi::add
    v T
    return v
}

import above..

Expected result

Not crashing

Got

crashing

Yaegi Version

#1647 generics import

Additional Notes

This is specific to #1647 -- reporting here assuming that might eventually be merged?

In the above example Go code, it doesn't seem like there is a way to represent the type constraint interface DataTypes as a reflect object, and the current yaegi type system ends up returning a nil type in types.go:nodeType2 for it, with a nil error, which ends up crashing the funcType case in nodeType2, when it is processing the type parameters for MyFun per above.

I can add to the #1647 PR to return an error for a nil type, so this doesn't crash, and gives a hint to the user.

So, this is just documentation of this issue, and checking to see if anyone has any ideas about how to get around this limitation on using type constraint interfaces?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions