AddOverload ensures that the new overload does not collide with an existing overload signature; however, if the function signatures are identical, the implementation may be rewritten as its difficult to compare functions by object identity.
(overload *OverloadDecl)
| 257 | // however, if the function signatures are identical, the implementation may be rewritten as its |
| 258 | // difficult to compare functions by object identity. |
| 259 | func (f *FunctionDecl) AddOverload(overload *OverloadDecl) error { |
| 260 | if f == nil { |
| 261 | return fmt.Errorf("nil function cannot add overload: %s", overload.ID()) |
| 262 | } |
| 263 | if overload == nil { |
| 264 | return fmt.Errorf("cannot add nil overload to funciton: %s", f.Name()) |
| 265 | } |
| 266 | for oID, o := range f.overloads { |
| 267 | if oID != overload.ID() && o.SignatureOverlaps(overload) { |
| 268 | return fmt.Errorf("overload signature collision in function %s: %s collides with %s", f.Name(), oID, overload.ID()) |
| 269 | } |
| 270 | if oID == overload.ID() { |
| 271 | if o.SignatureEquals(overload) && o.IsNonStrict() == overload.IsNonStrict() { |
| 272 | // Allow redefinition of an overload implementation so long as the signatures match. |
| 273 | if overload.HasBinding() { |
| 274 | f.overloads[oID] = overload |
| 275 | } |
| 276 | // Allow redefinition of the doc string. |
| 277 | if len(overload.doc) != 0 && o.doc != overload.doc { |
| 278 | o.doc = overload.doc |
| 279 | } |
| 280 | return nil |
| 281 | } |
| 282 | return fmt.Errorf("overload redefinition in function. %s: %s has multiple definitions", f.Name(), oID) |
| 283 | } |
| 284 | if overload.HasLateBinding() != o.HasLateBinding() { |
| 285 | return fmt.Errorf("overload with late binding cannot be added to function %s: cannot mix late and non-late bindings", f.Name()) |
| 286 | } |
| 287 | } |
| 288 | f.overloadOrdinals = append(f.overloadOrdinals, overload.ID()) |
| 289 | f.overloads[overload.ID()] = overload |
| 290 | return nil |
| 291 | } |
| 292 | |
| 293 | // OverloadDecls returns the overload declarations in the order in which they were declared. |
| 294 | func (f *FunctionDecl) OverloadDecls() []*OverloadDecl { |