modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.27k stars 2.59k forks source link

[Feature Request] Make `StringRef.find` call `StringSlice.find` #3294

Open gabrieldemarmiesse opened 3 months ago

gabrieldemarmiesse commented 3 months ago

Review Mojo's priorities

What is your request?

Currently the body of the two functions is repeated and that should not be the case. To avoid duplication, calling StringSlice.find should do the trick, though at the time of writing, the compiler doesn't allow it, because StringSlice.find is not callable at compile time.

When the compiler has a bit more mature, we should remove the implementation in StringRef.find and make it call StringSlice.find instead.

What is your motivation for this change?

The context behind this feature request is here: https://github.com/modularml/mojo/pull/3228/files#r1676277336

Any other details?

No response

JoeLoser commented 1 month ago

Writing the implementation of StringRef.find as

var str_slice = StringSlice[ImmutableAnyLifetime](
   unsafe_from_utf8_strref=self
)
var substr_slice = StringSlice[ImmutableAnyLifetime](
   unsafe_from_utf8_strref=substr
)
return str_slice.find(substr, start)

fails to infer the implicit is_mutable argument of substr. I think this is a compiler bug? Thoughts @connorg?