Julia中的构造函数:根据其他命名字段的输入值初始化命名字段

glw*_*art 1 constructor composite-types julia

想象一个构造函数,它接受两个参数并使用两个参数的值初始化3个命名字段.像这样的东西:

type test1
   a
   b
   c
   test1(a,b) = new(a,b,a/b)
end
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作,但如果c的值不是这么简单的表达式呢?如果它超过一两行怎么办?或者是一个复杂的列表理解?将表达式c直接粘贴到表达式中new()是不实用的,并使代码更难以阅读(IMO).我宁愿做这样的事情:

type test1
   a
   b
   c = a/b
   test1(a,b) = new(a,b,c)
end
Run Code Online (Sandbox Code Playgroud)

ab没有定义,直到呼叫test1(a,b)明显,所以这是行不通的.也许我只是在寻找语法糖.无论如何,当构造函数的参数值变得​​已知并且在调用之前可以使用它们时,我想更好地理解new().

有没有更好的方法(比第一个例子更好)做我在第二个例子中尝试做的事情?

(我认为以下问题和它的答案足够相关,但我仍然是一个Julia新手在Julia构建一个非默认构造函数)

编辑:冒着过于具体的风险,我想我会把这个问题出现的实际用例包括在内.我正在做一个自适应集成方案.跨越整合边界的每个体积元素被进一步细分.我对"立方体"类型的定义如下.我的学生在python中编写了一个工作原型,但我试图在julia中重写它以获得性能提升.

using Iterators
# Composite type defining a cube element of the integration domain
type cube
    pos # floats: Position of the cube in the integration domain
    dx  # float: Edge length of the cube
    verts # float: List of positions of the vertices 
    fvals::Dict # tuples,floats: Function values at the corners of the cube and its children
    depth::Int # int: Number of splittings to get to this level of cube
    maxdepth::Int # Deepest level of splitting (stopping condition)
    intVal # float: this cube's contribution to the integral

    intVal = 0

    cube(pos,dx,depth,maxdepth) = new(pos,dx,
           [i for i in product(0:dx:dx,0:dx:dx,0:dx:dx)],
           [vt=>fVal([vt...]) for vt in [i for i in product(0:dx:dx,0:dx:dx,0:dx:dx)]],
           depth,maxdepth,intVal)
end
Run Code Online (Sandbox Code Playgroud)

Mat*_* B. 5

内部构造函数只是出现在类型块内的函数,其名称与类型相同.这意味着您可以使用任一函数语法来定义它们.您可以使用简短格式(如上所述),也可以使用更详细的块语法:

type test1
   a
   b
   c
   function test1(a,b)
      c = a/b
      return new(a,b,c)
   end
end
Run Code Online (Sandbox Code Playgroud)

调用new甚至不需要是方法中的最后一个表达式; 您可以将其结果分配给中间变量,然后将其返回.


更多细节:type块就像普通的Julia范围,但有一些例外:

  • 单独出现或带有类型注释的任何符号都将成为该类型的字段.
  • 函数可以访问特殊的new内置函数来实例化类型,与类型相同的函数成为内部构造函数.

当您在类型块中放置任何其他赋值时,它们只是在该范围内创建一个局部变量.它不是字段或类型的一部分,而只是可以在构造函数的方法中使用的变量.也就是说,它不是很有用,将来可能会发生变化.