Skip to content

cgen: fix for in mut val of struct (fix #12361) - #12368

Merged
medvednikov merged 1 commit into
vlang:masterfrom
yuyi98:fix_for_mut_in
Nov 2, 2021
Merged

cgen: fix for in mut val of struct (fix #12361)#12368
medvednikov merged 1 commit into
vlang:masterfrom
yuyi98:fix_for_mut_in

Conversation

@yuyi98

@yuyi98 yuyi98 commented Nov 2, 2021

Copy link
Copy Markdown
Member

This PR fix for in mut val of struct (fix #12361).

  • Fix for in mut val of struct.
  • Add test.
struct Struct {
mut:
	array [][]int
}

fn (s Struct) rows() StructsRowIterator {
	return StructsRowIterator{
		array: s.array
		position: 0
	}
}

struct StructsRowIterator {
	Struct
mut:
	position int
}

fn (mut s StructsRowIterator) next() ?[]int {
	if s.position >= s.array.len {
		return error('out of range')
	}
	defer {
		s.position++
	}
	return s.array[s.position]
}

fn main() {
	mut s := Struct{
		array: [[1, 2, 3], [4, 5, 6]]
	}
	println(s)
	mut si := s.rows()
	println(si)

	mut rets := []string{}
	for mut row in si {
		println(row)
		rets << '$row'
	}
	assert rets.len == 2
	assert rets[0] == '[1, 2, 3]'
	assert rets[1] == '[4, 5, 6]'
}

PS D:\Test\v\tt1> v run .
Struct{
    array: [[1, 2, 3], [4, 5, 6]]
}
StructsRowIterator{
    Struct: Struct{
        array: [[1, 2, 3], [4, 5, 6]]
    }
    position: 0
}
[1, 2, 3]
[4, 5, 6]

@medvednikov
medvednikov merged commit 3fdbfca into vlang:master Nov 2, 2021
@yuyi98
yuyi98 deleted the fix_for_mut_in branch November 3, 2021 09:13
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.

for loop over mutable elements in costume iterator that returns an array of strings

2 participants