I see that your example works, but I can't see how to adapt it to my use 
case.

type Set[T comparable] map[T]struct{}

func New[T comparable](elements ...T) Set[T] {
    set := make(Set[T], len(elements))
    for _, element := range elements {
        set[element] = struct{}{}
    }
    return set
}

func (me Set[T]) String() string {
    ////// Want to sort by T < T //////////////////
    elements := make([]string, 0, len(me))
    for element := range me {
        elements = append(elements, fmt.Sprintf("%#v", element))
    }
    sort.Strings(elements)
    /////////////////////////////////////////////////////
    s := "{"
    sep := ""
    for _, element := range elements {
        s += fmt.Sprintf("%s%s", sep, element)
        sep = " "
    }
    return s + "}"
}

On Tuesday, November 8, 2022 at 9:19:48 AM UTC Jan Mercl wrote:

> On Tue, Nov 8, 2022 at 9:53 AM 'Mark' via golang-nuts
> <golan...@googlegroups.com> wrote:
>
> > Given a function:
> >
> > func F[T comparable](a T) {
> > }
> >
> > is it possible to check T's type inside F?
> >
> > My use case is that I have a function with signature G[T comparable](x 
> []T) and inside G I want to sort the elements in slice x where T could be 
> int or string.
>
> https://go.dev/play/p/zxQYVvOMX35 ?
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/577c056e-bf3f-4002-b431-f73aeda75a6dn%40googlegroups.com.

Reply via email to