Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a no parameter type have "defaults"

Tags:

types

julia

I essentially want to do the following:

typealias CVODE_BDF CVODE_BDF{:Newton,:Dense}

that is, allow a user to pass to my function CVODE_BDF and, if they did not set any of the type parameters, interpret it as CVODE_BDF{:Newton,:Dense}. I know that the typealias doesn't actually work, but is there a way to simulate this? Like in the function, read T.parameters[1] and somehow see that it was empty?

like image 559
Chris Rackauckas Avatar asked Nov 18 '16 23:11

Chris Rackauckas


People also ask

Can parameters have default values?

Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.

Which kind of parameters Cannot have a default?

variadic keyword parameters cannot have default values.

How do you set a parameter to default value?

The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.

Can all the parameters of a function can be default parameters?

All the parameters of a function can be default parameters.


1 Answers

You can do this if the function accepts objects of that type, rather than the type itself. E.g.

type MyObject{T}
end


const DefaultMyObject = MyObject{Int64}()

f{T}(x::MyObject{T}=DefaultMyObject) = T

f(), f(MyObj{Float64}())

gives

(Int64,Float64)
like image 130
David P. Sanders Avatar answered Sep 19 '22 17:09

David P. Sanders