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 6069169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:47:36+00:00 2026-05-23T09:47:36+00:00

If types T1 and T2 are based on type T , and type T

  • 0

If types T1 and T2 are based on type T, and type T only comes into existence from a NewT1() or NewT2(), is there any way a function func (*T) WhoAmI() can know whether it "really" is a T1 or T2?

package main

import "fmt"
import "reflect"

type T struct{ s string }

func (v *T) WhoAmI() string {

    // pull type name with reflect
    fmt.Println(reflect.TypeOf(v).Elem().Name()) // always prints "T"!

    // todo: if I am actually T1
    return "T1"
    // todo: else if I am actually T2
    return "T2"
}

type T1 T

func NewT1(s string) T1 { return T1{s} }

type T2 T

func NewT2(s string) T2 { return T2{s} }

func main() {
    var t1 = T1{"xyz"}
    var t2 = T2{"pdq"}
    s1 := ((*T)(&t1)).WhoAmI() // would like to return "T1"
    s2 := ((*T)(&t2)).WhoAmI() // would like to return "T2"
    fmt.Println(s1, s2)
}

to speak technically:

once t1 type T1 is coerced into type T so func (*T) WhoAmI() can be called, does t1 completely lose the fact that its type is really T1? if not, how do we reclaim the knowledge from the perspective of a method receiving type T?

to speak generally:

in other words, if one type is based on another, if a variable of the derived type is coerced into the base type to run a method, can that method learn the real type of the receiver who called it?

  • 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-05-23T09:47:36+00:00Added an answer on May 23, 2026 at 9:47 am

    No, it’s not possible. Creating a new type from an old one is not like creating a new class that inherits from a parent class in an class-based language. In your case T knows nothing about either T1 or T2 and if you’re calling the WhoAmI method you have a receiver of type T by definition.

    Your design might work better with an interface. Try something more like this:

    type T interface {
        WhoAmI() string
    }
    
    type T1 struct {
        s string
    }
    
    func (t *T1) WhoAmI() string { return "T1" }
    
    type T2 struct {
        s string
    }
    
    func (t *T2) WhoAmI() string { return "T2" }
    

    Try it on the Go playground

    T1 and T2 both implement the interface T, so they can be used as type T.

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

Sidebar

Related Questions

Is there any way that I can create a new delegate type based on
I have a Tabular type Form based upon a SELECT * FROM table type
I've created TFS project based on MSF for CMMI process template. For any type
On Windows, is there any way to programatically approve a USB device after insertion,
In Python, is there a better way to parameterise strings into regular expressions than
Background: Customer X has a CCK-based Content Type (vendorxxentry) and is using Views2 to
Suppose I want to create a set of observers based on type. That is
Is it possible for a generic interface's type to be based on a specific
C++ does not allow polymorphism for methods based on their return type. However, when
I want to align my member variables based on a class template type but

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.