Saturday, September 7, 2024
HomeGolangPolymorphic struct fields - Getting Assist

Polymorphic struct fields – Getting Assist


(new to Go)

I’d wish to create a struct subject, which might maintain pointers to 2 various kinds of structs. Instance:

sort Letter struct {
    ...
    Earlier *Character // that is invalid, I haven't got this kind, it is simply an instance
}

sort Punct struct {
    ...
    Earlier *Character // that is invalid, I haven't got this kind, it is simply an instance
}

l1 := Letter{}
l2 := Letter{ Earlier: &l1, }
p1 := Punct{ Earlier: &l2, }
p2 := Punct{ Earlier: &p1, }

The thought (simplified) is {that a} string consists of characters, which may both be letters or punctuation indicators (no I can’t use rune sort right here, as a result of every struct comprises another helpful info I have to affiliate with every character). Every character has a reference to the earlier character – thus the Earlier subject. As you’ll be able to see, in every struct Earlier needs to be assigned a pointer to both a struct of a special sort or a struct of the identical sort.

However I’m unsure the right way to go about polymorphism right here and which sort ought to the Earlier subject be declared as. Interfaces don’t appear to make sense right here, as a result of they cope with strategies, not subject sorts. How would you go about this?

sort Character sturct {
    Pre *Character
    Letter * Letter
    Punct * Punct
}
l1:=Character{Letter:&Letter{}}
l2:=Character{Pre:&l1,Letter:&Letter{}}
p1:=Character{Pre:&l2,Punct:&Punct{}}
p2:=Character{Pre:&p1,Punct:&Punct{}}

What must you concentrate on as a single aspect? As for whether or not this cell is a letter or an emblem, this can be a property of this unit.

Whereas it’s doable to realize the specified consequence by way of the interface, it could be essential to name the assertion sort.

Oh, thanks, this seems very apparent. Solely downside is, it doesn’t fairly scale and would require a further subject within the Character struct for every new struct I’d determine so as to add. Marvel if there are different methods to realize the identical purpose (I hoped possibly my understanding of interfaces is solely missing and there’s an answer involving interfaces).

Would you be capable to briefly define how would one do it with interfaces?

sort Letter struct {
    Earlier any
}

sort Punct struct {
    Earlier any
}

l1 := Letter{}
l2 := Letter{ Earlier: &l1, }
p1 := Punct{ Earlier: &l2, }
p2 := Punct{ Earlier: &p1, }
// Like this, however to name, it must be like this:
l,okay:=p1.Earlier.(*Letter)
if okay{
}

That is merely a easy use of any to symbolize the interface, and can truly add some particular strategies to keep away from different construction sort implementations and trigger code confusion.

Proper. I considered utilizing any, though that appeared slightly broad. I don’t must be significantly strict with my sorts, however, I suppose, it additionally wouldn’t harm if I might perceive the right way to obtain Go-style equal of polymorphism & inheritance in OOP, one thing like this (pseudo OOP-code):

class Character { ... }
class Letter extends Character {
    earlier Character 
}
class Punct extends Character {
    earlier Character
}
l1 = new Letter();
l2 = new Letter();
p1 = new Punct();
p2 = new Punct();
l2.earlier = l1; 
p1.earlier = l2;
p2.earlier = p1;

This code restricts the earlier subject to cases of courses, which might be descendants of Character.

sort Character interface {
    Pre()Character
    ...
}
sort Letter struct {
    Earlier Character
}
func(l *Letter)Pre()Character{
    returen l.Earlier
}

sort Punct struct {
    Earlier Character
}
func(p *Punct)Pre()Character{
    returen p.Earlier
}

I don’t assume you perceive what an interface is.
Any is only a easy interface sort, I’m simply utilizing any for example. Really, what you must do needs to be one thing like this.

You need to use generics to realize the identical factor with out any.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments