FAQ
Trying to construct a type with an embedded field where the type also
implements an interface:


type X struct {
     *sync.Mutex
     Value int
     // other named fields
}

type Y interface {
     sync.Locker
     // other method signatures properly implemented by X
}

/* Compile-time implementation prover */
var _ Y = X{}


The sync.Locker requires a pointer receiver - as other interfaces do. This
can be satisfied by X embedding the sync.Mutex as a pointer field. The
question is then initialization.

This works:

var _ Y = X{ new(sync.Mutex), 0}

but becomes clumsy as the number and complexity of fields in X increases
(not to mention error prone as the order and type of fields in X are
edited).

Naming *sync.Mutex in X does not work, as then Y is not implemented.

How then to initialize X or otherwise allow a direct embedding of
sync.Mutex in X while implementing Y?

Thanks...

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 19 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 27, '15 at 10:48p
activeAug 29, '15 at 1:56a
posts19
users7
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase