# Generics - is there an equivalent to Rust "Self" type

**URL:** <https://forum.golangbridge.org/t/generics-is-there-an-equivalent-to-rust-self-type/27808>\
**Category:** Getting Help\
**Created:** [June 20, 2022, 5:26am UTC](https://forum.golangbridge.org/t/generics-is-there-an-equivalent-to-rust-self-type/27808 "2022-06-20T05:26:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![globalflea](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/globalflea/32/11485_2.png) [@globalflea](https://forum.golangbridge.org/u/globalflea)\
**Post date:** [June 20, 2022, 5:26am UTC](https://forum.golangbridge.org/t/generics-is-there-an-equivalent-to-rust-self-type/27808/1 "2022-06-20T05:26:25Z")

</div>

Rust generic has a “Self” which is the Concrete type of the instantiated Generic type. Is there an equivalent in Go?

Specifically, so i can get Clone() to return a Self. Then i can have generic struct which will implement the Clone interface.

Something like,

```go
type Cloner[T any] interface {
    Clone() T
}

type Parent[T any] struct {
    parentAttr T
    Cloner[Self] 
}

func (p Parent[T]) Clone() Parent[T] { return a clone of the parent obj }

type Child[T any] struct {
   Parent[T]
   childAttr string
}

func (c Child[T]) Clone() Child[T] { return a clone of the child obj }

```

---

<div class="post-metadata">

**Author:** ![christophberger](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/christophberger/32/11391_2.png) [@christophberger](https://forum.golangbridge.org/u/christophberger)\
**Post date:** [June 20, 2022, 6:01am UTC](https://forum.golangbridge.org/t/generics-is-there-an-equivalent-to-rust-self-type/27808/2 "2022-06-20T06:01:07Z")

</div>

Hi @globalflea,

It is indeed possible that an interface method’s argument refers to the interface type - see this article for details:

[How to use generics for creating self-referring interfaces](https://appliedgo.com/blog/generic-interface-functions)

TL;DR:

Step 1. The Cloner interface stays unchanged.

```go
type Cloner[C any] interface {
	Clone() C
}

```

C is not restricted to anything yet, but step 2 takes care of this.

Step 2. A function that clones any type uses `Cloner[T]` as type restriction:

```go
func CloneAny[T Cloner[T]](c T) T {
	return c.Clone()
}

```

Then, for any variable `s` whose type implements `Cloner`, you can simply call

```go
CloneAny(s)

```

to clone s.

[Playground link](https://go.dev/play/p/F--f9Nfzvkw)

The blog article explains this solution in more detail.

---

<div class="post-metadata">

**Author:** ![globalflea](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/globalflea/32/11485_2.png) [@globalflea](https://forum.golangbridge.org/u/globalflea)\
**Post date:** [June 20, 2022, 6:10am UTC](https://forum.golangbridge.org/t/generics-is-there-an-equivalent-to-rust-self-type/27808/3 "2022-06-20T06:10:18Z")

</div>

Thanks.

---

<div class="post-metadata">

**Author:** ![guettli](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/guettli/32/10978_2.png) [@guettli](https://forum.golangbridge.org/u/guettli)\
**Post date:** [June 22, 2022, 12:06pm UTC](https://forum.golangbridge.org/t/generics-is-there-an-equivalent-to-rust-self-type/27808/4 "2022-06-22T12:06:30Z")

</div>

Hi Christoph,

thank you for your blog post.

You use this snippet in the post:

```auto
cloned := CloneAny(s)
if cs, ok := cloned.(CloneableSlice); ok {
    fmt.Println(cs[3])
}

```

Why not use `cloned := s.Clone()` and make every Clone-method return the particular type?

Then you don’t need a type assertion.

---

<div class="post-metadata">

**Author:** ![christophberger](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/christophberger/32/11391_2.png) [@christophberger](https://forum.golangbridge.org/u/christophberger)\
**Post date:** [June 22, 2022, 9:23pm UTC](https://forum.golangbridge.org/t/generics-is-there-an-equivalent-to-rust-self-type/27808/5 "2022-06-22T21:23:51Z")

</div>

Hi @guettli,

> [@guettli](#):
>
> Why not use `cloned := s.Clone()` and make every Clone-method return the particular type?

Because then the Clone methods would not implement the Cloner interface.

Your solution is of course perfectly fine if a Cloner interface is not required. Always use the simplest solution possible!

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [September 20, 2022, 9:24pm UTC](https://forum.golangbridge.org/t/generics-is-there-an-equivalent-to-rust-self-type/27808/6 "2022-09-20T21:24:04Z")

</div>

Closed on September 20, 2022, 9:24pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
