Elixir: is_function(...) output

Created on 18 Feb 2018  路  2Comments  路  Source: elixir-lang/elixir

Environment

Elixir 1.6.1

Current behavior

is_function() works fine on declared variables but in fact, if the variable is not defined in the program, it outputs an error (interactive shell in my case)

Code sample

is_function(sum)
#(CompileError) iex:1: undefined function add/0
# may be it should return false
sum = 1
is_function(sum)
# false
sum = fn ... end
is_function(sum)
#true

Expected behavior

This is a suggestion, let me know if this is not the appropriate place to write suggestions ;)
either output "false" on undeclared variables, Ex:

is_function(sum)
# false

consider supporting atomic input:
is_function(:sum) # ruby way:)
as far as i know the naming conventions for atoms and variables are same, and thus this solution wont have bad 械ffects.

Most helpful comment

Yes, is_function expects a value. You can't pass a variable that was not defined because that's a compilation error.

Ruby conventions doesn't work here because there is no object to ask what the function is. Unless you retrieve the function as an actual value, they do not exist at runtime. So for example, if you are inside a module that defines a function like this:

def add(a, b) do
  a + b
end

In order for it to be a value, you need to retrieve it as &add/2.

All 2 comments

@m13m updated, my bad, miss-clicked and posted issue early)

Yes, is_function expects a value. You can't pass a variable that was not defined because that's a compilation error.

Ruby conventions doesn't work here because there is no object to ask what the function is. Unless you retrieve the function as an actual value, they do not exist at runtime. So for example, if you are inside a module that defines a function like this:

def add(a, b) do
  a + b
end

In order for it to be a value, you need to retrieve it as &add/2.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ckampfe picture ckampfe  路  3Comments

whitepaperclip picture whitepaperclip  路  3Comments

ericmj picture ericmj  路  3Comments

Irio picture Irio  路  3Comments

vothane picture vothane  路  3Comments