Home  >  Article  >  Backend Development  >  Can Generic Functions Modify Shared Fields in Unrelated Structs Across Packages?

Can Generic Functions Modify Shared Fields in Unrelated Structs Across Packages?

DDD
DDDOriginal
2024-10-27 10:20:03920browse

 Can Generic Functions Modify Shared Fields in Unrelated Structs Across Packages?

Can Generic Functions Work with Shared Members Across External Package Structs?

Problem:

The goal is to define a generic function that can modify specific fields across varying Firebase message structs. Despite having overlapping fields of similar types, the Message and MulticastMessage structs lack an explicit relationship. However, attempting to use an interface constraint to handle this raises an error.

Solution 1: Type Switch

If the number of types in the union is small, a type switch approach can work. This involves manually handling each type and calling the appropriate methods to set the desired field values.

func highPriority[T firebaseMessage](message T) T {
    switch m := any(message).(type) {
    case *messaging.Message:
        setConfig(m.Android)
    case *messaging.MulticastMessage:
        setConfig(m.Android)
    }
    return message
}

Solution 2: Wrapper with Method

An alternative solution involves wrapping the original structs and defining a common method in the wrapper that can be used to set the shared fields.

type wrappedMessage interface {
    *MessageWrapper | *MultiCastMessageWrapper
    SetConfig(c foo.Config)
}

type MessageWrapper struct {
    messaging.Message
}

func (w *MessageWrapper) SetConfig(cfg messaging.Android) {
    *w.Android = cfg
}

Solution 3: Reflection

For handling a larger number of structs, reflection can be employed to set the shared fields dynamically. However, note that this requires the structs and fields to be addressable.

func highPriority[T firebaseMessage](message T) T {
    cfg := &messaging.Android{} 
    reflect.ValueOf(message).Elem().FieldByName("Android").Set(reflect.ValueOf(cfg))
    return message
}

Notes:

  • Solution 2 provides the most flexibility and allows for type safety.
  • Solution 3 is a more generic approach but requires reflection and may incur performance overhead.

The above is the detailed content of Can Generic Functions Modify Shared Fields in Unrelated Structs Across Packages?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn