mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-04 10:57:50 +08:00
96752afa4d
Struct map is a mechanism to let Go code expose simple structs to Elvish code. The difference between struct maps and maps is convenience for Go code; they also have different performance characteristics, but since struct maps are always quite small, the difference is not meaningful for Elvish's use cases. As a result, there is no good reason that Elvish code needs to be aware of the difference between struct maps and normal maps. Making them indistinguishable to Elvish code simplifies the language. This commit does the following: - Change Equal, Hash, Kind and Repr to treat struct maps like maps. - Change Assoc and Dissoc to "promote" struct maps to maps. - Remove the custom Repr method of parse.Source. - Update documentation to reflect this change.
26 lines
663 B
Go
26 lines
663 B
Go
package vals
|
|
|
|
// Dissocer wraps the Dissoc method.
|
|
type Dissocer interface {
|
|
// Dissoc returns a slightly modified version of the receiver with key k
|
|
// dissociated with any value.
|
|
Dissoc(k any) any
|
|
}
|
|
|
|
// Dissoc takes a container and a key, and returns a modified version of the
|
|
// container, with the given key dissociated with any value. It is implemented
|
|
// for the Map type and types satisfying the Dissocer interface. For other
|
|
// types, it returns nil.
|
|
func Dissoc(a, k any) any {
|
|
switch a := a.(type) {
|
|
case Map:
|
|
return a.Dissoc(k)
|
|
case StructMap:
|
|
return promoteToMap(a).Dissoc(k)
|
|
case Dissocer:
|
|
return a.Dissoc(k)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|