Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9290439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:25:13+00:00 2026-06-18T20:25:13+00:00

Given an interface and two (or more) implementations I struggle to easily switch the

  • 0

Given an interface and two (or more) implementations I struggle to easily switch the implementation when extending the functionality.

For example assume that there is interface INumber which supports Inc and String and two implementations NumberInt32 and NumberInt64 with their obvious implementation. Assume that I want to implement an EvenCounter on top of INumber. The EvenCounter only has a IncTwice and shall call Inc twice. I struggle to get the types right without using an extra struct surrounding the INumber in EvenCounter.

type INumber interface {
    Inc() 
    String() string
}

type NumberInt32 struct {
    number int32
}

func NewNumberInt32() INumber {
    ret := new(NumberInt32)
    ret.number = 0
    return ret
}

func (this *NumberInt32) Inc() { 
    this.number += 1
}

func (this *NumberInt32) String() string {
    return fmt.Sprintf("%d", this.number)
}

// type NumberInt64.... // obvious

Here is where I struggle

type EvenCounter1 INumber // nope, additional methods not possible 
type EvenCounter2 NumberInt32 // nope
func (this *EvenCounter2) IncTwice() {  
for i:=0; i < 2; i+=1 {
    // this.Inc() // Inc not found
    // INumber(*this).Inc() // cannot convert       
    // in, ok := *this.(INumber) // cannot convert
    // v, ok := this.(INumber) // cannot convert
    // a concrete conversion a) does not work and b) won't help
    // here it should be generic
    // v, ok := this.(NumberInt32) 
    // How do I call Inc here on this?
    }
}

Just embedding in a struct works…

type EvenCounter3 struct {
    n INumber
}

func (this *EvenCounter3) IncTwice() {
    n := this.n // that is a step I want to avoid
    n.Inc() // using this.n.Inc() twice makes it slower
    n.Inc()
}

func (this *EvenCounter3) String() string {
    return this.n.String()
}

I could live with the need to implement delegation manually for each method, but
obviously I would want to rely on INumber and not the specific implementation (that
would mean changing lots of places to try another implementation, However, I would want to avoid the extra indirection and (most likely?) extra space. Is there a
way to avoid the struct and directly say EvenCounter is a (specific) INumber with additional methods?

BTW the real example is a set of integers and a map of integers to integers with millions of instances all intertwined (and no, just map[int]bool won’t suffice – too slow, bitset is interesting depening on use case, etc.) and testing different implementations of the set and map easily by changing 2-3 lines in the code (ideally just the type and maybe
the generic creation of instances resp. making copies)

Any help appreciated and I hope this hasn’t been asked yet…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:25:15+00:00Added an answer on June 18, 2026 at 8:25 pm

    Your variant using embedding does not actually embed. Embedded fields are anonymous and Go then delegates automatically.

    This simplifies your example to:

    type EvenCounter3 struct {
        INumber
    }
    
    func (this *EvenCounter3) IncTwice() {
        this.Inc() // using this.n.Inc() twice makes it slower
        this.Inc()
    }
    

    Note that String() is automatically delegated (“promoted” in Go speak).

    As to calling Inc() twice making it slower, well, that’s a limitation of using interfaces. The point of an interface is to not expose the implementation, so you cannot access its internal number variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two interface references obtained from different sources. Is there a programmatic way to
Given: public interface IBatchProcess { void Run(); } and multiple implementation of: public class
Given: I have an interface. I have only class that implements that interface. Question:
I am fairly new to StructureMap, but my understanding is that there are two
Suppose we have the two classes given below: public class baseclass implements interface {}
We have two different implementations of the same interface. Think of it as a
Given two interfaces: interface I1 { int Foo(); } interface I2 { void Foo();
Given an interface or interfaces, what is the best way to generate an class
Given a base class with the following interface: public class Base { public virtual
Given the following classes and interfaces class A{ @NotNull(groups=Section1.class) private String myString } interface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.