What is the function of new()
in Julia? Is this question even specific enough?
I am looking through the module Mocha
where new(...)
is used quite commonly, but I don't see any definition of new()
, only uses of it, nor do I find reference to it in the Julia documentation.
I thought it might then be defined in a module that is being used by Mocha
, but then I would think I could learn about new() with Mocha.new
from the REPL, but that comes back with ERROR: UndefVarError: new not defined
.
For the life of me I can't figure out what new(...)
is doing. If it doesn't sound like something common to Julia, what can I do to try to track down where it's defined?
In Julia, the struct keyword defines a new Composite Type, based on given field names, and optionally annotated individual types. By default, structs cannot be modified once initialized (i.e. are inmutable unless explicitly specified as mutable)
A function in Julia is an object that takes a tuple of arguments and maps it to a return value. A function can be pure mathematical or can alter the state of another object in the program.
My guess based on reading the docs: using is used to bring another module into the name-space of the current module. import is used to bring specific types/functions/variables from other modules into the name-space of the current module.
Constructors are functions that create new objects – specifically, instances of Composite Types. In Julia, type objects also serve as constructor functions: they create new instances of themselves when applied to an argument tuple as a function.
From http://docs.julialang.org/en/release-0.4/manual/constructors/
Inner Constructor Methods
While outer constructor methods succeed in addressing the problem of providing additional convenience methods for constructing objects, they fail to address the other two use cases mentioned in the introduction of this chapter: enforcing invariants, and allowing construction of self-referential objects. For these problems, one needs inner constructor methods. An inner constructor method is much like an outer constructor method, with two differences:
- It is declared inside the block of a type declaration, rather than outside of it like normal methods.
- It has access to a special locally existent function called
new
that creates objects of the block’s type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With