Thursday, June 13, 2024
HomeGolangIs it attainable to create a perform that solely receives structs with...

Is it attainable to create a perform that solely receives structs with a particular sort of fields? – Getting Assist

I’ve this perform:

 func Motion(dataStruct any) {

I would really like dataStruct to have solely fields of this group of varieties [bool, string, int]

Meaning this struct is legitimate:

sort CustomCase struct {
	Title string
	Cellphone int
	Alive bool
	Native bool

However this one isn’t as a result of Names is a slice of strings:

sort CustomCase struct {
	Names []string
	Cellphone int
	Alive bool
	Native bool

At the moment I’ve it solved with mirror and checking every discipline, but when what I ask is feasible then it might be nice as a result of if I code mistaken I’d have an error at compile time and never at run time how I’m presently working.

In the event you write/generate a complete bunch of boilerplate for structs with each variety of fields you want, with each struct discipline identify you want, you are able to do it:

bundle major

sort scalar interface  int 

sort struct1[T0 scalar] interface {
	~struct{ a T0 } // word that discipline needs to be named "a."
	// I can not get it to work with out specifying the identify.

sort struct2[T0, T1 scalar] interface {
	~struct {
		a T0 // discipline have to be named "a"
		b T1 // have to be named "b"

// you need to outline a brand new struct3, struct4, and so forth.
// for each struct with each variety of fields and each discipline identify

sort structs[T0, T1 scalar] interface  struct2[T0, T1]
	// you need to add a brand new Tn and structn[...] for each struct

func motion[T structs[T0, T1], T0, T1 scalar](v T) {}

sort OK0 struct{ a int }
sort OK1 struct {
	a int
	b string

func major() {
	motion[OK0, int, int](OK0{})
	motion[OK1, int, string](OK1{})

however this appears fully impractical.

I’d as a substitute advocate utilizing the go/* bundle(s) to scan via your supply code and determine any structs that don’t meet that standards.

This isn’t the answer I’m searching for as a result of the names are dynamic.

I figured. Like I stated, It’s impractical. In that case, you may write your individual “linter” to verify for and detect this earlier than you compile, however the Go compiler itself can’t verify it.

Have you ever tried sort assertion? Since you have got the bottom struct hardcode (I suppose), you may attempt to make a kind assertion in Motion and return if the struct isn’t the one you need.

No, as a result of a struct can repeat a number of instances the identical sort of discipline, it may possibly have 3 bools and be legitimate, a kind assertion have to be precise and can’t be dynamized as already indicated.

Oh, sorry, I lastly received it. That the structs are even have dynamically altering fields. Didn’t give it some thought at first

No, Go isn’t like, say, Javascript. A struct sort has a set of fields mounted at compile time.

I do know. That was a remark concerning the dialogue

I feel you would wish to make use of reflection to examine the fields contained in the handed construction…



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments