yaegi icon indicating copy to clipboard operation
yaegi copied to clipboard

support range aliasT !

Open tttoad opened this issue 3 years ago • 0 comments

Proposal

State in advance that my English is poor The following are all from Google Translate

Support code like this: opentracing-go/propagation.go (about line 158)

type HTTPHeadersCarrier http.Header

// Set conforms to the TextMapWriter interface.
func (c HTTPHeadersCarrier) Set(key, val string) {
	h := http.Header(c)
	h.Set(key, val)
}

// ForeachKey conforms to the TextMapReader interface.
func (c HTTPHeadersCarrier) ForeachKey(handler func(key, val string) error) error {
	for k, vals := range c {
		for _, v := range vals {
			if err := handler(k, v); err != nil {
				return err
			}
		}
	}
	return nil
}

Background

opentracing has such code

Workarounds

interp/cfg.go change to: (about line 144)

	typ := o.typ
	for flag := true; flag; {
		flag = false
		switch typ.cat {
		case valueT:
			ttyp := typ.rtype
			switch ttyp.Kind() {
			case reflect.Map:
				n.anc.gen = rangeMap
				ityp := &itype{cat: valueT, rtype: reflect.TypeOf((*reflect.MapIter)(nil))}
				sc.add(ityp)
				ktyp = &itype{cat: valueT, rtype: ttyp.Key()}
				vtyp = &itype{cat: valueT, rtype: ttyp.Elem()}
			case reflect.String:
				sc.add(sc.getType("int")) // Add a dummy type to store array shallow copy for range
				ktyp = sc.getType("int")
				vtyp = sc.getType("rune")
			case reflect.Array, reflect.Slice:
				sc.add(sc.getType("int")) // Add a dummy type to store array shallow copy for range
				ktyp = sc.getType("int")
				vtyp = &itype{cat: valueT, rtype: ttyp.Elem()}
			}
		case mapT:
			n.anc.gen = rangeMap
			ityp := &itype{cat: valueT, rtype: reflect.TypeOf((*reflect.MapIter)(nil))}
			sc.add(ityp)
			ktyp = typ.key
			vtyp = typ.val

		case ptrT:
			ktyp = sc.getType("int")
			vtyp = typ.val
			if vtyp.cat == valueT {
				vtyp = &itype{cat: valueT, rtype: vtyp.rtype.Elem()}
			} else {
				vtyp = vtyp.val
			}

		case stringT:
			sc.add(sc.getType("int")) // Add a dummy type to store array shallow copy for range
			ktyp = sc.getType("int")
			vtyp = sc.getType("rune")
		case arrayT, sliceT, variadicT:
			sc.add(sc.getType("int")) // Add a dummy type to store array shallow copy for range
			ktyp = sc.getType("int")
			vtyp = typ.val
		case aliasT:
			typ = typ.val
			flag = true
		}
	}

tttoad avatar May 18 '21 08:05 tttoad