#Error when using swizzle function

1 messages · Page 1 of 1 (latest)

errant bronze
#

I am new to odinlang and want to try out by looking overview odinlang website.

when i want to try swizzle operator like .xyzw it works fine but when i use the function one it throwing error like this, i don't why??

can someone explain why my code doesn't works?

outer tusk
#

2.3 is not a valid array index

real nimbus
#

the indices to swizzle are much like x, y, z, w, but integers instead:

import "core:fmt"

main :: proc() {
    x: [4]f32 = { 2.3, 7.7, 1.9, 9.0 }
    y := swizzle(x, 2, 1, 0)
    fmt.println(y)
}
outer tusk
#

indeed, swizzle takes some array, and some indices into that array

#

so

x := [3]f32{10,4,1}
y := swizzle(x, 0, 0, 1, 0, 2, 1, 2)

will be [10, 10, 4, 10, 1, 4, 1]

#

the second two errors are just cascading errors from the first one, they'll go away once you fix the swizzle call

errant bronze
#

So swizzle only can takes integers value and cannot use float numbers?

real nimbus
#

just run my example and you can see what it does

errant bronze
#

Okay ty

outer tusk
#

what is the 2.3rd item of an array?

errant bronze
outer tusk
#

you misunderstand what the function does then

#

swizzle accepts indices

#
y := swizzle(x, 1, 2)

is equivalent to doing

y := [2]f32{x[1], x[2]}
#

thus if you used a float inside swizzle, you'd be doing x[2.3]

errant bronze
#

You right

#

It only for 0..3 right?

outer tusk
#

you can use any number

#

it just has to be a valid index on the array

errant bronze
#

yeahh