-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Change str
types to _not_ explicitly include the size.
#1060
Comments
So a quick question as I look at this issue, each string length is it's own type (ie str[4] is different from str[6]) so is there a blanket way to use the same implementation for each string length? The second question I have is what is the correct way to access an individual character in a string in Sway? |
I think strings are almost the most neglected part of Sway at the moment. 🙂 At one point I wondered whether Sway even needs them. Comparing strings should be done with the I assume we'll be adopting UTF8 for them which means accessing a specific character is non-trivial, just as with Rust, and requires some work in the compiler. Determining the byte size of the string suitable for Sorry, I haven't really provided any concrete answers here... |
Strings are often a waste of gas and I don't think many Solidity contracts use them outside of error messages, so I definitely get considering leaving them out entirely.
This would put us closer to Java though in that we have a specific method to compare strings, so I don't know if that would go against Sway's philosophy too much. (A bit different though, in just have a std library function like cmp_str(), instead of attached to the string type) There could also be another way using a generic |
Strings are needed for things like verification of EIP-191 signatures, unless you want devs to convert their UTF-8 strings to bytes beforehand manually. |
Interesting, didn't know they saw use for that. Could also be helpful to add some functions help developers move between strings and byte arrays in the future then as well |
I was looking to pick this up as I'm in need of a project. Not sure how to approach the fact (as mentioned above) that every different length of |
Internally strings of different length are definitely of different types. So using As per my comment above to actually compare them you'd use It would make sense for |
So, in order to implement this now in the stdlib:
something along the lines of:
cc @otrho |
The problem above is that pub fn are_strings_eq<S>(a: S, b: S) -> bool
where S: StaticString
{
...
} Do we have @sezna This is where your expertise shines. How shall we use strings of unknown lengths with type safety? |
We don't provide any native support for dynamically sized strings. That's Library Territory™. We'd either need to introduce a mechanism for implementing traits over all strs, or, defer to libraries. I vote for the latter because higher level string support is not an explicit goal of Sway and is not generally a high priority for smart contract development. |
Note that we're not talking about dynamically sized strings at all here. These problems are with Sway's static So you're conceding that |
Except it really makes sense to keep string literals. I honestly think we need to tone down the type system and copy Rust to a degree. We should make Thoughts? |
Is that not the case currently? |
Yep, probably? I was just trying to lay out everything we need to make it all work. @sezna Does the above seem fair? |
I think so... I have WIP for this with tests, but blocked by |
It makes sense, but you're suggesting essentially keeping the restriction that all |
OK, I'm renaming this issue since all of the discussion has been here already. This is a largish change though, as it should go right through from parsing to IR. |
eq
for str
typestr
types to _not_ explicitly include the size.
Feel free to override me, but I'm putting p: medium on this since strings are not blocking any major use cases right now |
This Sway code errors:
The text was updated successfully, but these errors were encountered: