#cast const to var

1 messages · Page 1 of 1 (latest)

karmic falcon
#
src/lib/vexlib.zig:655:26: error: expected type '*lib.vexlib.String', found '*const lib.vexlib.String'
            const j = key.indexOf(str.charAt(i));

Is there a way I can cast a const to a var without declaring it as a seperate variable?

maiden sierra
#

You can make your lib return a pointer of the right constness

#

Just use anytype as the receiver param and then do some comptime stuff to figure out the return type

#

Much better than doing @constCasts to get rid of the constness after the fact

tawdry cedar
#

That's often caused by const key = ... instead of var key = ...

#

If it's a proc param though, consider changing charAt to fn charAt(self: *const String, index: usize)

#

Otherwise, yes, you'll need var copy = param; or whathaveyou. But it sounds odd to require a mutable string in order to know what char is at a particular index, so I'd probably change that anyway.