17

"/path/to/my/file.swift:143:34: error: cannot convert return expression of type 'String.SubSequence' (aka 'Substring') to return type 'String'"

Ah yes, I love it when Substring isn't String

Comments
  • 6
    And this is why I dislike heavily-enforced strong typing. (C-style types are fine, but past that they get increasingly irritating)

    I remember getting an error in VB when I was like 10 stating some ridiculously long type consisting of s chain of objects (like "...blah.blah.ocx.blah.commonctl.blah.blah.TextBox.text") was not a string.

    I much prefer Ruby's typing. Everything still has strong types, but converting between them is often easy, and Ruby allows "duck typing." This means if something looks like it's e.g. an enumerable, you can use enumerable methods on it. As an example: you can use the same methods to iterate over a Hash, array, string, ARel results set, range, ....
  • 4
    Of course substring is not a string because strings are value types in Swift, and you don't want to cause a copy before you're done. Besides, substring references the original memory, so if you keep going with that, the original string can't be garbage collected as long as the substring is around.

    That's why substring is only used for intermediate operations, and when you're done, you stringify that.
  • 2
    @Fast-Nop Yes, of course I understand that, but it's annoying either way
  • 1
    as a Rustacean I can only sat you're lucky it isnt a Substring<'a, String>
  • 3
    Actually, strings always suck one way or the other, and that's because strings are a hard problem. Even worse now with unicode and graphems and stuff.
Add Comment