Sunday, May 19, 2024
HomeGolangNo elegant solution to make a struct with blended component? - Getting...

No elegant solution to make a struct with blended component? – Getting Assist


Pascal has a solution to make struct with variant sort with union.
In C++ ,you are able to do this with struct/ object with a typical ancestor.
However to this point, i didn’t discover a resolution with out unsafe pointer and casting. Did a miss a trick ?

Are you able to present an instance what downside you need to resolve?
Relying on the instance you may use Interfaces, embedding or pointer-casting.

I’m making an attempt to jot down an interpreter. I have to do linked listing with completely different sort of component like string | quantity | expression listing. It’s my train after I be taught a brand new language.

I’m utilizing these applications as a reference: https://github.com/pliba/kamin

to this point I’ve discovered this: https://www.jerf.org/iri/submit/2917/

However it’s not very compact compared to the C model.

If all parts in a single listing have the identical sort one can use generics like this: listing[string] if they’ve doubtlessly differing kinds in the identical listing, one may simply use the interface sort “any”.
Interface sorts retailer the information inline whether it is sufficiently small, or a pointer if the information is larger.
You need to use a union like knowledge sort for very particular use circumstances with pointer casting, however I might solely suggest it if all of the safer/simpler choices don’t fulfill your necessities below sensible circumstances.

I knew this resolution. However I hoped to discover a higher resolution. Sooner or later, language maintainers are going to have to contemplate the answer of C, or object-oriented programming, or a much less inflexible interface mannequin with return values. They’ve been debating on the topic since at the very least 2017. It’s heartbreaking.

It seems like I’m going to must preserve exploring programming languages.

Thanks anyway.

Go has made its’ title on being opinionated and barely completely different than different languages. If it doesn’t work to your use case, there are plenty of different actually nice choices on the market. However they the rigidity of the language/tooling is one thing most individuals see as a characteristic. This can be a tad like complaining about Rust since you don’t just like the borrow checker, when the borrow checker is among the promoting factors of Rust.

That is among the strengths and weaknesses of Go. Practically every thing in language/compiler design is a collection of tradeoffs. The Go group strikes slowly in an try and make the precise tradeoffs and never break their promise of compatibility.

Try zig for those who haven’t already!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments