`new(...)` 在 Julia 中有什么作用? [英] What does `new(...)` do in Julia?

查看:22
本文介绍了`new(...)` 在 Julia 中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

new()在Julia中的作用是什么?这个问题是否足够具体?

What is the function of new() in Julia? Is this question even specific enough?

我正在查看 Mocha 模块,其中 new(...) 非常常用,但我没有看到 new(),只是使用它,我在 Julia 文档中也没有找到它的引用.

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.

我认为它可能会在 Mocha 正在使用的模块中定义,但后来我想我可以通过 Mocha.new 了解 new()来自 REPL,但返回的是 ERROR: UndefVarError: new not defined.

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.

对于我的一生,我无法弄清楚 new(...) 在做什么.如果这听起来不像 Julia 的共同点,我能做些什么来尝试追踪它的定义位置?

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?

推荐答案

来自 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. 它是在类型声明的块内声明的,而不是像普通方法一样在它之外.
  2. 它可以访问一个名为 new 的特殊本地存在函数,该函数创建块类型的对象.
  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.

这篇关于`new(...)` 在 Julia 中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆