This proposes you should add a warning if a programmer names the variable 
with the name of a type - what do you think?

Motivation
Ahead of the recent gradual typing introduction, I was having fun exploring 
types and was trying to come up with cases that are definitely a type 
mistake but that the current version of Elixir wouldn't catch as a warning 
or error.

One case I found is something a programmer probably wouldn't do 
intentionally, and could be worth warning about.

Currently, you can name a varaible as a built-in type, for example this is 
correct code:

# Set the variable named "integer" to the string "42". Despite its name it 
is a string.
integer = "42"

# This is now valid:
# Define a map with string keys
profile = %{
  "age" => integer,
}

# Access data from the map
age = profile["age"]
IO.puts("Age: #{age}")

Try it.  It will print:
iex(9)> IO.puts("Age: #{age}")
Age: 42
:ok

It's a string all along and someone might wonder why they're seeing bizarre 
code like that, but as more types got introduced it would be more likely 
that programmers learning the syntax would *accidentally* name a variable 
by the name of a type.  Polymorphic code could eventually lead to tricky 
issues.

What I think should happen is that the line:

integer = "42"

should warn:

Warning: variable named with the name a type. This is probably not what you 
want.  Consider changing the name of a variable so that it is not also the 
name of a type, for example rather than
        integer = "42" 
you could name it:
          my_string = "42"
In this case "my_string" is no longer the name of a type.
"

What do you think about this proposed warning?

1.  Do you think anyone would purposefully name a variable a type? (false 
positives.)
2.  Do you think it could be useful for catching future syntax errors, 
especially as the type system becomes more fleshed-out and there is more 
polymorphic code? (true positives)

The warning could also be useful once more types are used in the future.  A 
local variable should not accidentally shadow the name of a type without 
warning.

This warning can be added to the current version of Elixir and does not 
depend on gradual typing.

Best regards,
Rob

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/5f713616-b9e8-4ad5-b2eb-6a51327c8bf4n%40googlegroups.com.

Reply via email to