How to default to empty string if variable unspecified to a function

I’d love to use Julia as a replacement for my Unix shell at least some of the time so that I can become more familiar and comfortable with the language. One function I’m having trouble defining is:

function vjus(path)
     juliaDir = "/path/to/main/julia-repo/$path"
     vim(juliaDir)
end

(where vim is a function I’ve previously defined). Now the reason I’m having difficulty with this is that I want it so that if path is unspecified and left blank, vjus will just open Vim to /path/to/main/julia-repo/. I know how to set a default for a positional argument in a function, namely using function vjus(path=default), but if I leave default as empty (which is what I want to be), I get an error (namely ERROR: LoadError: syntax: unexpected ")" for vjus(path=) and ERROR: LoadError: syntax: invalid empty character literal for vjus(path=''). Is there a way around this issue?

Thanks for your time.

Can it be that you are just looking for

function vjus(path="")
...
end

?

1 Like

You’re correct. I tried path='' and that didn’t work (error message mentioned in my original post), but path="" seems to work fine. Thanks!

1 Like

In addition you can use multiple distpach so you can just type vjus() without arguments

vjus()=vjus(path="")

2 Likes

In this case path would be a keyword argument, what you mean is:

function vjus(path)
...
end
vjus() = vjus("")
1 Like

Yes, that is what I mean :sweat_smile:

1 Like

Note that ' is for specifying Char

julia> 'a'
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> '🐳'
'🐳': Unicode U+1F433 (category So: Symbol, other)

julia> typeof(ans)
Char

julia> sizeof(ans)
4

A 4-byte object. As such, we can do things like reinterpret back and fourth between numbers and Chars:

julia> reinterpret(Char, 0x61000000) # or Int32(1627389952)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> reinterpret(Char, 0xf09f90b3) # or Int32(-257978189)
'🐳': Unicode U+1F433 (category So: Symbol, other)

While a String is a collection of variable numbers of characters, and definitely not of fixed size.

You can see that your error message doesn’t come interpolating a Char, but from the fact that we can’t create an empty one:

julia> ''
ERROR: syntax: invalid empty character literal
Stacktrace:
 [1] top-level scope at none:1

julia> ""
""
4 Likes