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

use in operator to check if character is in string #761

Closed
davekch opened this issue Jun 28, 2019 · 2 comments
Closed

use in operator to check if character is in string #761

davekch opened this issue Jun 28, 2019 · 2 comments
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@davekch
Copy link

davekch commented Jun 28, 2019

In the documentation you state

In V, a string is a read-only array of bytes

so it would be intuitive to use the in operator on strings.

Right now,

fn main(){
    mystr := 'abcd'
    if 'a' in mystr {
        println('a is in $mystr')               
    }
}

results in

pass=2 fn=`main`
panic: substring.v:3
`in` requires an array
@davekch davekch added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label Jun 28, 2019
@ntrel
Copy link
Contributor

ntrel commented Jun 29, 2019

First, 'a' is a string, use backticks for (byte) characters (they ought to produce runes instead though).

in is an O(n) operator, so a function would make this clearer (see #277). If we keep in, it needs to be overloadable for structs so the string struct can support it. Even then it can't find both a substring and a byte/rune unless the method takes a generic type.

@alexesprit
Copy link
Member

The in operator now can be used for strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants