Hello Ian. Here's a complete example constructed from the snippets:
------------------------- package main type Fun func() type Alpha struct { } func (a Alpha) funA() { } type Beta struct { } func (b Beta) funB() { } var a1,a2,a3,a4,a5 Alpha var b1,b2,b3,b4,b5 Beta var f Fun func main() { f = a1.funA f = b1.funB // Challenge: construct an expression that evaluates to true only if f is assigned to `funA` of any instance of `Alpha` } ------------------------- https://play.golang.org/p/HoGGR0NeLE2 Thanks! Kind regards, Howard On Tue, 11 Jun 2019 at 01:10, Ian Lance Taylor <i...@golang.org> wrote: > > On Sun, Jun 9, 2019 at 10:17 PM Houzuo Guo <guohou...@gmail.com> wrote: > > > > Thanks very much Ian - indeed I should have proof read the message. With > > the last paragraph, I wished to ask for a way to tell a non-nil ` var f > > Fun` is assigned to `funA` of any instance of structure Alpha, or `funB` of > > any instance of structure Beta - without performing equality comparison > > between the instances' method and the `f` variable. > > > > Take an example - suppose there are: > > - var a1,a2,a3,a4,a5 Alpha > > - var b1,b2,b3,b4,b5 Beta > > - var f Fun > > > > And that `f` is assigned to any of: > > - a1.funA, a2.funA ... > > - b1.funB, b2.funB ... > > > > Without performing equality check (f == a1.funA || f == a2.funA || f == > > a3.funA ...), construct a test condition that evaluates to true only if `f` > > is assigned to `funA` of any instance of `Alpha`. > > But in your code funA and funB are methods. You can't assign f to > a1.funA. You can't change a1.funA at all. > > Can you show a small runnable example of what you are asking about? > > Ian > > > > > > On Monday, 10 June 2019 07:46:58 UTC+3, Ian Lance Taylor wrote: > >> > >> On Sun, Jun 9, 2019 at 9:48 AM Houzuo Guo <guoh...@gmail.com> wrote: > >> > > >> > Given a function type: type Fun func() > >> > > >> > And two structure methods that satisfy the function type: > >> > > >> > type Alpha struct { > >> > } > >> > func (a Alpha) funA() { > >> > } > >> > > >> > type Beta struct { > >> > } > >> > func (b Beta) funB() { > >> > } > >> > > >> > At runtime, is there a way to tell whether a non-nil `var f Fun` is > >> > assigned to an arbitrary instance of structure Alpha, or Beta? Can > >> > reflection help in this case? > >> > >> I'm sorry, I don't understand your question. funA and funB are > >> methods. You can't assign to a method. And the structs have no > >> fields, so you can't assign to that either. So I'm not sure what you > >> mean. > >> > >> That said, I suspect the answer to your real question is "no." > >> > >> Ian > > > > -- > > 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/f7a6221a-aada-4f3a-ab12-34d26f550e87%40googlegroups.com. > > For more options, visit https://groups.google.com/d/optout. -- 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/CAHSE2WfAHLhipJ4Oaf-j%2BT2NdLBKUwpZq-vFhU_tsf8fkf2gtw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.