tompark
tompark

Reputation: 638

Why is a Julia function name (without arguments) silently ignored?

I have a script that defines a function, and later intended to call the function but forgot to add the parentheses, like this:

function myfunc()
    println("inside myfunc")
end
myfunc  # This line is silently ignored. The function isn't invoked and there's no error message.

After a while I did figure out that I was missing the parentheses, but since Julia didn't give me an error, I'm wondering what that line is actually doing? I'm assuming that it must be doing something with the myfunc statement, but I don't know Julia well enough to understand what is happening.

I tried --depwarn=yes but don't see a julia command line switch to increase the warning level or verbosity. Please let me know if one exists.

For background context, the reason this came up is that I'm trying to translate a Bash script to Julia, and there are numerous locations where an argument-less function is defined and then invoked, and in Bash you don't need parentheses after the function name to invoke it.

The script is being run from command line (julia stub.jl) and I'm using Julia 1.0.3 on macOS.

Upvotes: 2

Views: 1323

Answers (2)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

It doesn't silently ignore the function. Calling myfunc in an interactive session will show you what happens: the call returns the function object to the console, and thus call's the show method for Function, showing how many methods are currently defined for that function in your workspace.

julia> function myfunc()
           println("inside myfunc")
       end
myfunc (generic function with 1 method)

julia> myfunc
myfunc (generic function with 1 method)

Since you're calling this in a script, show is never called, and thus you don't see any result. But it doesn't error, because the syntax is valid. Thanks to DNF for the helpful comment on it being in a script.

Upvotes: 3

david
david

Reputation: 2638

It does nothing.

As in c, an expression has a value: in c the expression _ a=1+1; _ has the value _ 2 _ In c, this just fell out of the parser: they wanted to be able to evaluate expressions like _ a==b _

In Julia, it's the result of designing a language where the code you write is handled as a data object of the language. In Julia, the expression "a=1+1" has the value "a=1+1".

In c, the fact that

  a=1+1;

is an acceptable line of code means that, accidentally,

  a;

is also an acceptable line of code. The same is true in Julia: the compiler expects to see a data value there: any data value you put may be acceptable: even for example the data value that represents the calculated value returned by a function:

 myfunc()

or the value that represents the function object itself:

myfunc

As in c, the fact that data values are everywhere in your code just indicates that the syntax allows data values everywhere in your code and that the compiler does nothing with data values that are everywhere in your code.

Upvotes: 2

Related Questions