Search code examples
gogenericsdata-structurestype-conversion

How to test if a generic type can fit a more restrictive interface - golang


I am building an all-purpose data structure, that I intend to use in various contexts and with various bits of data.

I am currently attempting to make a matcher, that will look into my structure and return all nodes containing the data given. My problem being that, since I need my structure to be as generic as possible, I need my data to be of a generic type matching any, and this won't allow me to make equalities.

I have built a "descendant type" (there's probably a correct term, I'm self-taught on this) that has the more rigorous comparable constraint.

I want to see if I can convert from the more general one to the more specific one (even if I have to catch an error and return it to the user). I know that I don't specifically need to, but it makes the code understandable down the line if i do it like that.

Here's a bit of code to explain my question :

type DataStructureGeneralCase[T any] struct {
    /*
       my data structure, which is too long to make for a good example, so I'm using a slice instead
    */
    data []T
}

type DataStructureSpecific[T comparable] DataStructureGeneralCase[T]

// this works because any contains comparable

func (ds *DataStructureSpecific[T]) GetMatching(content T) int {
    /*The real function returns my custom Node type, but let's be brief*/
    for idx, item := range ds.data {
        if item == content {
            return idx
        }
    }
    return -1
}

func (dg *DataStructureGeneralCase[T]) TryMatching(content T) (int, error) {
    if ds, ok := (*dg).(DataStructureGeneral); ok {
        // Does not work because dg is not interface, which I understand
    } else {
        return -1, fmt.Errorf("Could not convert because of non-comparable content")
    }
}

My question can be summarized to "How can I do my conversion ?".


Solution

  • func CheckImplements[T any, I any]() bool {
        var o T
        _, ok := interface{}(&o).(I)
        return ok
    }
    

    So yeah, runtime only.