Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia function with NULL argument

Tags:

r

julia

In R you can use NULL arguments in functions like myfun = function (a=NULL, b, c) can you do this in Julia? I'm asking because I'd like variable a as a condition : if a = NULL do this, else do that. I can just write different functions of course but they would mostly repeat each other. I can also just assign arbitrary numbers but it seems using NULL is more clear. Thanks!.

like image 835
JianghuiDu Avatar asked Dec 04 '17 07:12

JianghuiDu


People also ask

Is null in Julia?

There is no predefined value called null in Julia, so it is not clear what you want here. Rather than using ' ' for missing values, missing or nothing (depending on the semantics you need) would allow you to interface with more functionality in the ecosystem.

What is the type of a function in Julia?

Method Tables Every function in Julia is a generic function. A generic function is conceptually a single function, but consists of many definitions, or methods. The methods of a generic function are stored in a method table.

What is :: In Julia?

A return type can be specified in the function declaration using the :: operator. This converts the return value to the specified type. This function will always return an Int8 regardless of the types of x and y .


1 Answers

One of the most common questions from people coming to a new language is how to code exactly like in the language they come from. But testing for NULL is not likely to be the best way to implement this in Julia. Mostly you'll define two methods for your function:

function myfun(a, b, c)
    ...
end

function myfun(b, c)
    ...
end

With the different behaviours.

like image 145
Michael K. Borregaard Avatar answered Oct 16 '22 17:10

Michael K. Borregaard