Skip to content

function returning both result and fixed array syntax ! collision #25968

Description

@jorgeluismireles

Describe the bug

A function returning both result and fixed array can't use as last return ! since fixed array values won't be correct. Omitting ! is a workaround working by now, though is confusing and is hard to debug lines like this.

Reproduction Steps

import strconv

pub fn valid_triangle(a u8, b u8, c u8) ![3]u8 {
	if int(a) + int(b) <= int(c) {
		return error('Invalid: a + b <= c')
	}
	if int(b) + int(c) <= int(a) {
		return error('Invalid: b + c <= a')
	}
	if int(c) + int(a) <= int(b) {
		return error('Invalid: c + a <= b')
	}
	return [a, b, c]!
}

pub fn triangle_from_string(sides string) ![3]u8 {
	s := sides.split(',')
	if s.len != 3 {
		return error('Invalid: number of sides')
	}
	a := strconv.atou8(s[0])!
	b := strconv.atou8(s[1])!
	c := strconv.atou8(s[2])!
	// warning: don't append `!` to next call 
	// will confuse with fixed and return `garbage`.
	return valid_triangle(a, b, c)!
}

fn main() {
	dump(triangle_from_string('3,4,5')!)
}

Expected Behavior

[code.v:30] triangle_from_string('3,4,5')!: [3,4,5]

Current Behavior

[code.v:30] triangle_from_string('3,4,5')!: [192, 0, 0]

where 192 is random

Possible Solution

No response

Additional Information/Context

A workaround is omit ! in last line of method triangle_from_string

-	return valid_triangle(a, b, c)!
+	return valid_triangle(a, b, c)

V version

V 0.4.12 dc0b0f5

Environment details (OS name and version, etc.)

https://play.vlang.io/p/a9ae34e637

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions