Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range branch with label(break/continue/goto/return) #511

Closed
wants to merge 34 commits into from
Closed

Range branch with label(break/continue/goto/return) #511

wants to merge 34 commits into from

Conversation

JessonChan
Copy link
Member

L:
for k, v := range [1,2,3] {
	if k < 2 {
		continue L
	}
	if k > 2{
                println("range break")
		break L
	}
	println(k, v)
}

@codecov-commenter
Copy link

codecov-commenter commented Jul 31, 2020

Codecov Report

Merging #511 into master will increase coverage by 0.03%.
The diff coverage is 95.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #511      +/-   ##
==========================================
+ Coverage   89.90%   89.93%   +0.03%     
==========================================
  Files          40       41       +1     
  Lines       10210    10295      +85     
==========================================
+ Hits         9179     9259      +80     
- Misses        752      754       +2     
- Partials      279      282       +3     
Impacted Files Coverage Δ
cl/expr.go 86.51% <ø> (-0.07%) ⬇️
cl/ctx_check.go 76.40% <33.33%> (+0.09%) ⬆️
exec/golang/stmt.go 94.27% <53.33%> (-2.50%) ⬇️
cl/context.go 83.55% <100.00%> (+0.68%) ⬆️
cl/stmt.go 93.59% <100.00%> (-0.02%) ⬇️
exec/bytecode/code.go 100.00% <100.00%> (ø)
exec/bytecode/context.go 94.44% <100.00%> (-0.11%) ⬇️
exec/bytecode/flow.go 97.01% <100.00%> (+0.68%) ⬆️
exec/bytecode/func.go 93.65% <100.00%> (-0.08%) ⬇️
exec/bytecode/goinstr_lstcompr.go 94.22% <100.00%> (+0.81%) ⬆️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1b9df62...8bbc24f. Read the comment docs.

@JessonChan JessonChan changed the title Range branch with label Range branch with label(break/continue/goto/return) Aug 4, 2020
@xushiwei
Copy link
Member

It seems too complex to implement this feature.
Let's discuss how to improve it.

@JessonChan
Copy link
Member Author

Let's discuss how to improve it.

Glad to discuss this.

@xushiwei
Copy link
Member

xushiwei commented Aug 16, 2020

In cl package we can introduce ContainerTrait and container Iterator:

func TraitContainer(containerTy reflect.Type) ContainerTrait

type ContainerTrait struct {
    Key, Value reflect.Type // Key can be nil, and it means the Iterator only has Value(), not has Key()
    Iter interface{} // func(c interface{}) Iterator or func(c interface{}) ValueIterator
}

type Iterator interface {
     Next() bool
     Key() interface{}
     Value() interface{}
}

type ValueIterator interface {
     Next() bool
     Key() reflect.Value
     Value() reflect.Value
}

And then

for k, v = range container {
     ...
}

can become:

trait := TraitContainer(reflect.TypeOf(container))
for __iter := trait.Iter(container); __iter.Next(); {
    k, v = __iter.Key().(trait.Key), __iter.Value().(trait.Value)
    ...
}

@xushiwei xushiwei closed this Aug 24, 2020
@xushiwei
Copy link
Member

#563

@liyue201
Copy link

In cl package we can introduce ContainerTrait and container Iterator:

func TraitContainer(containerTy reflect.Type) ContainerTrait

type ContainerTrait struct {
    Key, Value reflect.Type // Key can be nil, and it means the Iterator only has Value(), not has Key()
    Iter interface{} // func(c interface{}) Iterator or func(c interface{}) ValueIterator
}

type Iterator interface {
     Next() bool
     Key() interface{}
     Value() interface{}
}

type ValueIterator interface {
     Next() bool
     Key() reflect.Value
     Value() reflect.Value
}

And then

for k, v = range container {
     ...
}

can become:

trait := TraitContainer(reflect.TypeOf(container))
for __iter := trait.Iter(container); __iter.Next(); {
    k, v = __iter.Key().(trait.Key), __iter.Value().(trait.Value)
    ...
}

这个转化很巧妙,没看到这个帖子之前,光看代码一脸懵逼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants