IS4Code / PawnPlus

A SA-MP plugin enhancing the capabilities of the Pawn programming language
MIT License
102 stars 17 forks source link

Constant Iterators #44

Closed duydang2311 closed 3 years ago

duydang2311 commented 3 years ago

Hello IllidanS4, I would like to ask if is there any way to create a constant iterator pointing to an element in dynamic container like this std::vector::cbegin() in C++?

IS4Code commented 3 years ago

Hello, no, iterators currently don't have specific tags or functionality that would restrict the access. Is this necessary in your case?

duydang2311 commented 3 years ago

I'm writing modular scripts so I have a function that returns an iterator to a map in file A. When another file uses that function, I just want to make sure that it can only read the element that the returned iterator points to. So it's not really must have, I know I can write code more carefully to make sure not changing the element's value but I'm just curious if there's one constant iterator. Thanks for replying!

IS4Code commented 3 years ago

Actually, I just remembered that it is possible to use iter_project to create a new iterator that applies an expression to values obtained from the inner operator. With identity, you may be able to derive at what you need:

new Iter:iter = map_iter(iter);
iter = iter_project(iter, expr_arg_pack(0));

I haven't checked it, but this iterator should yield the same values, but raise an error if it is modified.