You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A data file can contain structures in similar schemas in many places. Making their commonalities explicit may be useful as they enable writing functions that can work with many types at once.
Go provides a way to generalize multiple types based on their common methods via interfaces. This is not helpful in the context of data mapping. Although can be leveraged from to enable generalization that is based on common fields.
Example for generalization based on common fields from stdlib
An example usage can be seen in go/types package which defines accessors (getter/setter) on fields of a type struct object which is embedded by struct Func, struct Const, struct Var etc.
// provides generalizationtypeObjectinterface {
// ...setType(...)
setOrder(...)
setColor(...)
setParent(...)
sameId(...)
scopePos(...)
setScopePos(...)
}
// provides the common fields for many "derived" typetypeobjectstruct {
parent*Scopepos token.Pospkg*PackagenamestringtypTypeorder_uint32color_colorscopePos_ token.Pos
}
// accessors make every type embeds "object" to comply "Object" by their common fields rather than methodsfunc (obj*object) setParent(...) { ... }
func (obj*object) setType(...) { ... }
func (obj*object) setOrder(...) { ... }
func (obj*object) setColor(...) { ... }
func (obj*object) setScopePos(...) { ... }
// declares types derived from "object" conforms "Object"// each contain "object" as embedded type, in addition to custom fieldstype (
PkgNamestruct {
object// ...// ...
}
Conststruct {
object// ...
}
TypeNamestruct {
object
}
Varstruct {
object// ...// ...// ...// ...
}
Funcstruct {
object// ...// ...
}
Labelstruct {
object// ...
}
Builtinstruct {
object// ...
}
Nilstruct {
object
}
)
// usecases of generalization;// as in storing, accepting or returning generalized "Object" valuestypedeclInfostruct {
depsmap[Object]bool
}
func (check*Checker) objDecl(objObject, def*TypeName) {
...ifobj.color() ==white&&obj.Type() !=nil {
obj.setColor(black)
return
}
switchobj.color() {
casewhite:
assert(obj.Type() ==nil)
...casegrey:
...switchobj:=obj.(type) {
case*Const:
if!check.validCycle(obj) ||obj.typ==nil {
obj.typ=Typ[Invalid]
}
case*Var:
if!check.validCycle(obj) ||obj.typ==nil {
obj.typ=Typ[Invalid]
}
case*TypeName:
if!check.validCycle(obj) {
...obj.typ=Typ[Invalid]
}
case*Func:
if!check.validCycle(obj) {
...
}
default:
unreachable()
}
assert(obj.Type() !=nil)
return
}
d:=check.objMap[obj]
ifd==nil {
check.dump("%v: %s should have been declared", obj.Pos(), obj)
...
}
...
}
Purpose of issue
To open discussions about the use cases of a feature that is related to allowing generalization based on common fields.
Example usages
Iterating over similar items stored in a list
Passing items in similar schemas to functions that won't need to work on the excluded fields.
Candidate method to allow generalization
Allowing user to specify target dicts and keys, that will get accessors implemented on by Gonfique. So, users can define interface's to generalize types implementing same accessors, to enable themselves accepting those types at once for functions or storing them in a sequence.
Preface
A data file can contain structures in similar schemas in many places. Making their commonalities explicit may be useful as they enable writing functions that can work with many types at once.
Go provides a way to generalize multiple types based on their common methods via
interface
s. This is not helpful in the context of data mapping. Although can be leveraged from to enable generalization that is based on common fields.Example for generalization based on common fields from stdlib
An example usage can be seen in
go/types
package which defines accessors (getter/setter) on fields of a typestruct object
which is embedded bystruct Func
,struct Const
,struct Var
etc.Purpose of issue
To open discussions about the use cases of a feature that is related to allowing generalization based on common fields.
Example usages
Candidate method to allow generalization
Sources
The text was updated successfully, but these errors were encountered: