6

I'm trying to implement some caching functions in Golang but I want them to be valid for both strings and other objects that implement the Stringer interface. I'm making an attempt of it using Golang generics and this is what I have so far:

import (
    "fmt"
)

type String interface {
    ~string | fmt.Stringer
}

However, this gives an error cannot use fmt.Stringer in union (fmt.Stringer contains methods). Is there a way to do this without relying on reflection or type boxing/unboxing?

0

2 Answers 2

6

The confusion might be warranted because the type parameters proposal suggests code like yours, however ended up as an implementation restriction in Go 1.18.

It is mentioned in the specs, and in Go 1.18 release notes. The specs are the normative reference:

Implementation restriction: A union (with more than one term) cannot contain the predeclared identifier comparable or interfaces that specify methods, or embed comparable or interfaces that specify methods.

There is also a somewhat extensive explanation of why this wasn't included in Go 1.18 release. The tl;dr is simplifying the computation of union type sets (although in Go 1.18 method sets of type parameters aren't computed implicitly either...).

Consider also that with or without this restriction you likely wouldn't gain anything useful, beside passing T to functions that use reflection. To call methods on ~string | fmt.Stringer you still have to type-assert or type-switch.

Note that if the purpose of such constraint is simply to print the string value, you can just use fmt.Sprint, which uses reflection.

For the broader case, type assertion or switch as in colm.anseo's answer works just fine when the argument can take exact types as string (without ~) and fmt.Stringer. For approximations like ~string you can't exhaustively handle all possible terms, because those type sets are virtually infinite. So you're back to reflection. A better implementation might be:

func StringLike(v any) string {
    // switch exact types first
    switch s := v.(type) {
    case fmt.Stringer:
        return s.String()

    case string:
        return s
    }

    // handle the remaining type set of ~string
    if r := reflect.ValueOf(v); r.Kind() == reflect.String {
        return r.String()
    }

    panic("invalid type")
}

Playground: https://go.dev/play/p/-wzo2KPKzWZ

2
  • This is an excellent explanation of the problem. From my perspective, the goal was somewhere halfway between your suggested fix and reflection. I wanted to generate a string from any type that implemented fmt.Stringer or string itself while maintaining strong compile-time type-checking but it seems like this isn't possible with go1.18. Either way, thank you for your detailed answer.
    – Woody1193
    May 17, 2022 at 8:31
  • 1
    @Woody1193 it's possible they lift this restriction in the future. The comment I linked from the Go issue tracker hints that the implementation of unions-with-methods is doable. Allowing tilde types in switches would also help, and this also might be allowed in the future. In Go 1.18 everything is conservative to avoid messing things up too much in case of critical design flaws. An alternative for type-safety could be declaring stringLike unexported and have exported accessors String[S ~string](v S) and Stringer(s fmt.Stringer) that call stringLike
    – blackgreen
    May 17, 2022 at 10:04
2

Generics - which allows in theory many types to be used - settles on a single concrete type at compilation time. Interfaces allow for multiple types at runtime. You are looking to combine both of these at once -unfortunately that is not possible.


The closest you can get without using reflection would be using a runtime type assertion:

func StringLike(v any) string {

    if s, ok := v.(string); ok {
        return s
    }

    if s, ok := v.(fmt.Stringer); ok {
        return s.String()
    }

    panic("non string invalid type")
}

https://go.dev/play/p/p4QHuT6R8yO

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.