gleam-lang / suggestions

📙 A place for ideas and feedback
26 stars 2 forks source link

String concatenation operator #91

Closed sporto closed 3 years ago

sporto commented 3 years ago

String concatenation is a very common operation. It would be great if Gleam had this instead of having to use string.concat E.g. "Hello" ++ "World".

Thanks

lpil commented 3 years ago

I think having an operator for this encourages inefficient binary handling in Elixir, and I think Erlang's approach of not having one encourages more efficient string handling. If we add one it will have to be added with compiler optimisations to rewrite the code into an efficient operation, and we'll have to think carefully about what behaviour is being encouraged.

manuel-rubio commented 3 years ago

Actually, Erlang depends on the kind of strings are you using. If we develop exclusively in Erlang, I could consider using double-quote strings as my easy way to use strings (charlists in this case, of course), but still valid and they have the concatenation from lists ++.

On the other hand, if you want to use only binaries as strings, you can stick with this syntax:

<<A/binary, B/binary>>

It's not a function but of course, it's not an operator so, I'm not sure if the problem is in the parser way, it's only being performed once and not during the system running and you can envelop the way to concatenate in this way. Maybe (for sure), I'm missing something by the way, but for me it looks feasible translate a ++ b to <<A/binary, B/binary>>.

lpil commented 3 years ago

Aye, to be clear we are only talking about binary strings here as Gleam does not support character list strings.

lpil commented 3 years ago

@sporto I've opened a discussion to talk about this idea here -> https://github.com/gleam-lang/gleam/discussions/1086

Thanks for kicking this off!