David Parks
David Parks

Reputation: 32051

What does `new(...)` do in Julia?

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?

Upvotes: 10

Views: 4193

Answers (1)

Fredrik Bagge
Fredrik Bagge

Reputation: 1381

From https://docs.julialang.org/en/v1/manual/constructors/#man-inner-constructor-methods

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.

Upvotes: 10

Related Questions