@mkborregaard: I just wanted to have that flexibility in case people would ask. I love Julia language, and I would like to give people no chance to discard the fact that this is an awesome language Thanks.
Itās not about the REPL or Atom. It is simply that there are two ways of defining a function:
f(x) = println(x)
# or
function f(x)
println(x)
end
They are equivalent. But you tried a mix between the two, e.g. you tried something like this:
f(x)
println(x)
end
That doesnāt work.
Iāve actually written a little package called RequiredKeywords.jl that implements this solution using a macro. You can use it like
julia> using RequiredKeywords
julia> @required_keywords f(x; y) = x*y
f (generic function with 1 method)
julia> f(2,y=2)
4
julia> f(2)
ERROR: Unassigned Keyword: Required keyword y::Int64 not included.
Stacktrace:
[1] f(::Int64) at ./REPL[5]:1
It also works for the longform function
style definitions, and you can still specify types for the optional keyword arguments.
@adamslc: Great. Thanks.
Just a quick question. When we write function this way, how do we handle function if the code lines extend multiple lines, but not s single one?
Thanks.
f(x) = (y = x + x; println(y))
@mkborregaard: OK. Thanks.
Or with beginā¦ end
I think I saw somewhere that
f(x) = begin
y = x + x
println(y)
end
was deprecated as recommended style in favour of
function f(x)
y = x + x
println(y)
end
so I thought only the really compact format was recommended?
I donāt think the former was ever recommended style, but I donāt see anything wrong with it either. It can be nice for consistency if a file primarily contains pure functions defined from short form syntax. Scala encourages the =
syntax for pure functions too, and I think thatās a good convention: it is weird to see foo!(x) = bar!(x)
because mutation is not typically mathematically expressed in definitions.
How could I reduce the length of my function definitions if I have multiple named arguments? For example, I want to do something like:
err = z -> throw(ArgumentError("missing x"))
f(;x::Int64=err) = x
f()
but this gives a method-not-found error.
I see that there are easier ways for v1 (i.e., just doing f(; x::Int)
), but how could I do for v0.6?
EDIT: can do with
err() = throw(ArgumentError("missing x"))
f(;x::Int64=err()) = x
f()
Just use the Compat package and put @compat
in front of the function declaration, and then you can use the Julia 1.0 syntax for required-keyword arguments.
A post was merged into an existing topic: Struct with fixed array sizes
Sorry to revive this. Using Compat 1.3.0
, and Julia v0.6.4, I get the following error:
julia> using Compat
INFO: Recompiling stale cache file /homes/USER/.julia/lib/v0.6/Compat.ji for module Compat.
julia> Pkg.status("Compat")
- Compat 1.3.0
julia> @compat f(; x::Int64) = x
ERROR: syntax: invalid keyword argument syntax "x::Int64" (expected assignment)
julia> @compat function(; x::Int64)
return x
end
ERROR: syntax: invalid keyword argument syntax "x::Int64" (expected assignment)
What am I messing up?
Looks like a https://github.com/JuliaLang/Compat.jl/pull/586 ā it didnāt handle the case of typed keyword arguments.