Variable -1 OOP approach?

So, I really new to elixir and starting to use resources on youtube etc.
Working through Live Learning Elixir part 1 @ around 90 minutes…

He adds this and, it compiles.

  def convert_input_to_index(integer_input) do
    integer_input -1
  end

On my machine, it does not. The guy is coming to elixir from ruby but I know, variables, lists etc are immutable right?

I get this error

$ iex "tic_tac_toe.exs"
Erlang/OTP 22 [RELEASE CANDIDATE 2] [erts-10.3.1] [source-52dd4f4842] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [hipe]

warning: variable "integer_input" is unused (if the variable is not meant to be used, prefix it with an underscore)
  tic_tac_toe.exs:25

** (CompileError) tic_tac_toe.exs:26: undefined function integer_input/1
    (elixir) src/elixir_locals.erl:108: :elixir_locals."-ensure_no_undefined_local/3-lc$^0/1-0-"/2
    (elixir) src/elixir_locals.erl:109: anonymous fn/3 in :elixir_locals.ensure_no_undefined_local/3
    tic_tac_toe.exs:3: (file)

I now underscore the 'variable _integer_input

Now I get a new error message

$ iex "tic_tac_toe.exs"
Erlang/OTP 22 [RELEASE CANDIDATE 2] [erts-10.3.1] [source-52dd4f4842] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [hipe]

** (CompileError) tic_tac_toe.exs:26: "_integer_input -1" looks like a function call but there is a variable named "_integer_input", please use explicit parentheses or even spaces
    tic_tac_toe.exs:25: (module)

Now, is this just the fact he is using an older version and this way is depreciated? Or, is the variable -1 flawed from the start because I understood functional programming required making copies of lists etc and you couldn’t directly modify them as per ruby etc and OOP.

If this approach is wrong, i rather stop trying to fix it now TBH.

Thanks

Due to the fact, that functions and variables live in a separate namespace and the fact that parenthesis are optional, the compiler thinks that you try to call integer_input(-1).

To actually subtract 1 from the input, you need to have a space () between - and 1. Then the parser will realise it as the binary minus, which again then will make it recognize integer_input as the variable rather than a function name.

Also, the error:

warning: variable "integer_input" is unused (if the variable is not meant to be used, prefix it with an underscore)
  tic_tac_toe.exs:25

Wanted you to prefix the name in the functions argument list, not its usage in the functions body, eg: def foo(_unused), do: …

PS: I’m not sure what this has to do with “OOP”?

4 Likes

I will try that thanks.

The guy who did the video is a rubyist and he is explaining everything from an OOP perspective.
This is why I thought var - 1 was as illegal as var++.

It’s crazy that a space is needed. Something I haven’t read about yet so thanks.

The parser needs that space to distinguish unary and binary minus, it comes up here every now and then :wink:

And why should var - 1 not be valid? It creates a new value and leaving var as is. Nothing different to most OO languages (unless some very nasty stiff has been done via operator overloading).

2 Likes

Yes, one space away from a compile. I can’t believe formatters aren’t alert to this.

Lesson learned. Many thanks.

My misunderstanding. I thought, all lists and values could not be changed, only new copies applied to new values.

You’re correct. This function is not changing var it is simply returning the integer that is one less than var

3 Likes