Odin
Odin copied to clipboard
Negating array literal in default argument does not negate argument
Context
Odin: dev-2025-04-nightly OS: Ubuntu 24.10, Linux 6.11.0-26-generic CPU: AMD Ryzen 7 6800H with Radeon Graphics RAM: 14703 MiB Backend: LLVM 20.1.1
package main
import "core:fmt"
WORLD_WIDTH :: 100
WORLD_HEIGHT :: 100
main :: proc()
{
region_to_world_space({0, 0})
}
region_to_world_space :: proc(coord: [2]f32, region: [2]f32 = {-1, -1}) -> [2]f32
{
fmt.println(region) // Prints: {1, 1}
return coord + ({WORLD_WIDTH, WORLD_HEIGHT} * region)
}
Expected Behavior
Value of region parameter should be: {-1, -1}
Current Behavior
Current value is: {1, 1}
I'm unable to replicate this on Linux: [-1, -1]
I also get [-1, -1].
I can't even replicate it on the stated version of dev-2025-04.
Oops, I copied the code with my fix and forgot to change it back. Try this
package main
import "core:fmt"
WORLD_WIDTH :: 100
WORLD_HEIGHT :: 100
main :: proc()
{
region_to_world_space({0, 0})
}
region_to_world_space :: proc(coord: [2]f32, region: [2]f32 = -{1, 1}) -> [2]f32
{
fmt.println(region) // Prints: {1, 1}
return coord + ({WORLD_WIDTH, WORLD_HEIGHT} * region)
}
Confirmed with the new sample.
f: [2]f32 = -{1,1}
This works [-1, -1] as a standalone statement, so presumably it should work in a procedure default argument too.
It looks like the unary - isn't evaluated in the default parameter:
package main
@(require) import "core:fmt"
WORLD_WIDTH :: 100
WORLD_HEIGHT :: 100
main :: proc() {
region_to_world_space({0, 0})
r : [2]f32 = -{1, 1}
fmt.println(r) // [-1, -1]
}
region_to_world_space :: proc(coord: [2]f32, region: [2]f32 = -{1, 1}) -> [2]f32 {
fmt.println(region) // [1, 1]
return coord + ({WORLD_WIDTH, WORLD_HEIGHT} * region)
}
Seems like in general expression stops after finding first array (only for array types)
package main
import "core:fmt"
main :: proc() {
first() // [2, 2]
first2() // [1, 1]
second() // [5, 5]
third() // 2
fourth() // 2
}
first :: proc(a: [2]f32 = 2 + {1, 1} + 3) {
fmt.println(a)
}
first2 :: proc(a: [2]f32 = {1, 1} + 2 + 3) {
fmt.println(a)
}
second :: proc(a: [2]f32 = 2 + 3 + {1, 1}) {
fmt.println(a)
}
third :: proc(a: f32 = 1 + [2]f32{1, 1}.x) {
fmt.println(a)
}
fourth :: proc(a: f32 = (1 + [2]f32{1, 1}).x) {
fmt.println(a)
}