Commit 42e40e90 authored by Tom Almeida's avatar Tom Almeida Committed by AdminXVII
Browse files

feat: Print an error when attempting to assign to a string index

Why is this needed?
Well, at the moment nothing happens when the user tries something along
the lines of `let some_string[3] = 'v'`, even though we don't allow it.
The variable itself doesn't change, and we don't print any errors.
This patch just makes the function that would assign return an error
instead of silently failing.

Why not implement modifying strings by index?
Sure, we could do that, but it's not quite as obvious as it seems. We
support utf8 strings, so modifying the `i`th byte obviously doesn't
work, and could potentially make the string invalid. Another option
would be to set the `i`th character to whatever is passed in. This is
fine in theory, but it would be an `O(N)` operation in the average
case, and we'd have to shuffle all the following chars either further up
or further down the string.
While this may be a desirable ability to eventually have, it will
require significantly more work and thought around strings in ion than
simply returning an error.
parent 47bb7068
......@@ -487,6 +487,9 @@ impl<'a> Shell<'a> {
}
Ok(())
}
Value::Str(_) => {
Err("cannot assign to an index of a string".into())
}
_ => Ok(()),
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment