Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 535
David Parks Avatar asked May 23 '16 01:05

David Parks


People also ask

What is struct in Julia?

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)

How do functions work in Julia?

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.

What is the difference between import and using in Julia?

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.

What is a constructor in Julia?

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.


1 Answers

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.
like image 131
Fredrik Bagge Avatar answered Sep 18 '22 13:09

Fredrik Bagge