Skip to content

v: add typeof(var).indirections and T.indirections - #22805

Merged
spytheman merged 3 commits into
vlang:masterfrom
felipensp:add_typeof_indirections_field
Nov 9, 2024
Merged

spytheman merged 3 commits into
vlang:masterfrom
felipensp:add_typeof_indirections_field

Conversation

@felipensp

@felipensp felipensp commented Nov 8, 2024 •

Copy link
Copy Markdown
Member

This PR adds a way to retrieve the number of indirections related to typeof(expr) and T generic type.

fn indirections[T](val T) int {
	return T.indirections
}

fn test_main() {
	a := 0
	assert typeof(a).indirections == 0
	assert indirections(a) == 0
	b := &a
	assert typeof(b).indirections == 1
	assert indirections(b) == 1
	c := [1]
	assert typeof(c).indirections == 0
	assert indirections(c) == 0
	d := &c
	assert typeof(d).indirections == 1
	assert indirections(d) == 1
}

Related to #22792

Huly®: V_0.6-21251

@felipensp
felipensp marked this pull request as ready for review November 8, 2024 21:48
Comment thread vlib/v/ast/ast.v
Comment on lines 288 to 289
typ
unaliased_typ

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use type and unaliased_type now that enums allow it.

Comment thread vlib/v/gen/js/js.v
g.write('new int(')
g.write('${int(g.unwrap_generic(it.name_type))}')
g.write(')')
g.write(')')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good find

assert indirections(c) == 0
d := &c
assert typeof(d).indirections == 1
assert indirections(d) == 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add an assert for dd := &d and bb := &b .

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman
spytheman merged commit ee53a99 into vlang:master Nov 9, 2024
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.

2 participants