7
votes

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?

1
new() is used to define the inner constructor method. There may be other uses I am unaware of, hence why this is a comment and not an answer. - Colin T Bowers
I'm pretty sure that there are no other uses, it's really a keyword, not a function, only allowed inside a type/immutable. - Scott Jones
I think Julia currently has a pretty bad documentation. I could not find in the documentation where new() is clearly explained. Some people say I can look at the source codes to see what is going on. I disagree. - Po C.
I've found that the biggest problem with the documentation is not that things are not documented, but with the documentation system that is currently being used (search barely works!), better to just use Google on the documentation. - Scott Jones

1 Answers

7
votes

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:

  1. It is declared inside the block of a type declaration, rather than outside of it like normal methods.
  2. It has access to a special locally existent function called new that creates objects of the block’s type.