"new(...)"在Julia中做什么? [英] What does `new(...)` do in Julia?

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

问题描述

Julia中new()的功能是什么?这个问题是否足够具体?

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所使用的模块中定义,但随后我认为我可以从REPL中使用Mocha.new了解new().

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天全站免登陆