3
votes

I'm encountering a weird behaviour in Elixir when defining for example function default arguments or using head|tail in a list definitions.

This does not work and results in an error unexpected token: " ":

def a(b \\ "test") do
  b
end

But this one does:

def a(b \\"test") do
    b
end

The difference being the whitespace character " " preceding the default string argument "test"

Also this does not work and results in an error unexpected token: " ":

[0 | [1,2,3,4,5]]

But this one does work:

[0 |[1,2,3,4,5]]

Once again the difference being the whitespace character " " preceding the tail list definition [1,2,3,4,5]

The problem exists in IEX and compiled code. I'm running Elixir 1.4. My system is macOS Sierra and I'm using iTerm as my terminal app.

So the question is: is this the correct behaviour or is there something wrong for example in my environment and what it could possibly be? All the examples and guides allow whitespace in these positions but for some reason my environment does not. Is there something I can do about this?

Thank you in advance!

1
What if you copy paste the code snippets from this question to iex? Do you still get the same error? (All 4 snippets are working fine for me.)Dogbert
@Dogbert Well I'll be damned. Indeed when copy-pasting these examples it seems to work fine. Also in tmux these seems to work fine when writing straight to the iex. But when I write these to iex in bare terminal without tmux I get an error unexpected token: " " (column 5, codepoint U+00A0) It seems like the whitespace without tmux is somehow invalid.veturi
U+00A0 is a non-breaking space character: en.wikipedia.org/wiki/Non-breaking_space Not sure why you're getting that when typing a space.Dogbert
@Dogbert I figured out the problem. macOS produces Non-breaking space instead of a normal space when pressing alt+space. This occurred sometimes when I wrote code following | character since that one requires alt-key to be pressed and I just wasn't releasing the alt-key soon enough. And in case of macOS it produced 00A0 instead of normal space character.veturi

1 Answers

7
votes

Issue got resolved as stated in the comments.

On macOS alt+space provides Non-breaking space character instead of normal space. The issue described occurred most of the times after inserting any character with alt-combination following whitespace because I just wasn't fast enough to release the alt-key and thus wrong whitespace was provided.

For instructions to resolve this on macOS (in case if you want to disable the alternative space) check out this question: https://superuser.com/questions/78245/how-to-disable-the-option-space-key-combination-for-non-breaking-spaces