Skip to content

Unclear error when return value ignored inside a conditional #429

@raulgrell

Description

@raulgrell

The statement

if(input.keyDown[W]) GAME_DATA.position.offset(vec2(0, -deltaTime));

fails with

error: incompatible types: '&Vec2T(f32)' and 'void'
    if(input.keyDown[W]) GAME_DATA.position.offset(vec2(0, -deltaTime));
    ^
note: type '&Vec2T(f32)' here
    if(input.keyDown[W]) GAME_DATA.position.offset(vec2(0, -deltaTime));
                                                                 ^
note: type 'void' here
    if(input.keyDown[W]) GAME_DATA.position.offset(vec2(0, -deltaTime));
    ^

Where

pub fn offset(self: &Self, other: &const Self) -> &Self {
    self.data[0] += other.data[0];
    self.data[1] += other.data[1];
    return self;
}

Fixed by

// Ignoring the return value
if(input.keyDown[W]) _ = GAME_DATA.position.offset(vec2(0, -deltaTime));

or

// Wrapping in braces , which reveals the return value ignored error.     
if(input.keyPressed[W) { GAME_DATA.position.offset(vec2(0, -deltaTime)); }

or

// Redefining offset to not return the reference to self
pub fn offset(self: &Self, other: &const Self) {
    self.data[0] += other.data[0];
    self.data[1] += other.data[1];
}

The behaviour is correct, I just think the error is unergonomic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions