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

Method called twice when mixed with "as"operator #19453

Closed
dospro opened this issue Sep 27, 2023 · 0 comments · Fixed by #19462
Closed

Method called twice when mixed with "as"operator #19453

dospro opened this issue Sep 27, 2023 · 0 comments · Fixed by #19462
Labels
Bug This tag is applied to issues which reports bugs. Unit: Type System Bugs/feature requests, that are related to the V types system.

Comments

@dospro
Copy link

dospro commented Sep 27, 2023

Describe the bug

Pretty similar bevavior than this issue: #19435
This time it happens when casting return values using the keyword as.

When we have a struct which has a field of a sum type, and a method that returns this field, when calling the method and using as to cast it to a specific type, the method gets called twice

Reproduction Steps

  1. Create a new project using command v new some_project
  2. In the main file copy and paste the following code
type Numbers = int | string

struct Values {
	value Numbers
}

struct Wrapper {
	element []Values
mut:
	index int
}

fn (mut instance Wrapper) get() Values {
	println("Gets called")
	return instance.element[0]
}

fn main() {
	mut wrapper := Wrapper{
		element: [
			Values{
				value: 1
			},
			Values{
				value: '2'
			},
		]
	}
	println(wrapper.get().value as int)
}
  1. Run the file using v run .
  2. It prints the following output
Gets called
Gets called
1

Expected Behavior

The method should only be called once.
The output should be

Gets called
1

Current Behavior

The method is called twice.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.1 3d2731f

Environment details (OS name and version, etc.)

V full version: V 0.4.1 https://github.com/vlang/v/commit/28234c76c66034124a8d8b476d452e70b98667b8.3d2731f
OS: macos, macOS, 13.5.2
Processor: 10 cpus, 64bit, little endian, Apple M1 Max
CC version: Apple clang version 15.0.0 (clang-1500.0.40.1)
thirdparty/tcc status: thirdparty-macos-arm64 a668e5a0

Note

You can vote for this issue using the 👍 reaction. More votes increase the issue's priority
for developers.

Take into account that only the 👍 reaction counts as a vote.
Only reactions to the issue itself will be counted as votes, not comments.

@dospro dospro added the Bug This tag is applied to issues which reports bugs. label Sep 27, 2023
@ArtemkaKun ArtemkaKun added the Unit: Type System Bugs/feature requests, that are related to the V types system. label Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: Type System Bugs/feature requests, that are related to the V types system.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants