Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capacity for seq and string #460

Closed
juancarlospaco opened this issue May 5, 2022 · 8 comments
Closed

capacity for seq and string #460

juancarlospaco opened this issue May 5, 2022 · 8 comments

Comments

@juancarlospaco
Copy link
Contributor

juancarlospaco commented May 5, 2022

Abstract

Allow read-only access to cap of seq and string.
The name is suggested to be capacity instead of cap.
It is a very simple and tiny RFC.

Motivation

It is a nice information to design macros and metaprogramming,
like unrollings, setLen, newStringOfCap, newSeqOfCap, etc.

len and setLen of seq and string is usable, but cap is not.

Examples

I imagine something kinda like

func capacity*(self: string): int

(Pseudocode-ish)

Change would require 3 ~ 5 lines at most (?).

Before

cap is not accessible, it is always unknown.

This can not be implemented with third-party Nimble modules, in a non-hacky way at least.

After

myString.capacity returns an int with the allocated capacity.

Backward incompatibility

No backward-incompatible changes, because it is a new proc.

@Araq
Copy link
Member

Araq commented May 6, 2022

Since this probably yet another system.nim extension and it is rather rare to come up, can we name it capacity and setCapacity?

@juancarlospaco
Copy link
Contributor Author

Yes, makes sense, I do not have a suggestion about the naming.

@juancarlospaco juancarlospaco changed the title cap for seq and string capacity for seq and string May 6, 2022
@c-blake
Copy link

c-blake commented May 6, 2022

"space" and "room" might be short, whole word alternatives, but maybe they also get used in video games which, at one time, was a popular Nim arena. :-) (EDIT: but I don't think the name matters much. Transparency to better time/avoid allocations is a good thing.)

@Araq
Copy link
Member

Araq commented May 6, 2022

Neither "space" nor "room" is as spot on as "capacity" is though. But of course, it's subjective.

@c-blake
Copy link

c-blake commented May 6, 2022

(I was just brainstorming - it's already been "*OfCap" forever which would be the strongest consistency argument in favor of just getCap and setCap or .cap= rather than capacity...)

@Vindaar
Copy link

Vindaar commented May 6, 2022

I'd stick to something related to "capacity". Whether it's the short form "cap" or the full word, I personally don't care too much though.

@arnetheduck
Copy link

related: nim-lang/Nim#19727

@planetis-m
Copy link

One thing to consider that came up with my work, is that when exposing internal state, like capacity to user code, is that when fuzzing code like that it would be impossible to correctly reproduce the conditions for a crash. That's because when copying or serializing the capacity is not saved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants