小编Noz*_*rum的帖子

朱莉娅未能多次派遣

v06我想写一个期望2到3个参数的签名.第一个是Integer或Vector of Integer.第二个是整数向量或整数矩阵.第三个是整数向量或未指定.

我第一次尝试这样

function foo(
a::Union{Integer, Vector{Integer}},
b::Union{Vector{Integer}, Matrix{Integer}},
c::Union{Void, Vector{Integer}} = nothing)
Run Code Online (Sandbox Code Playgroud)

当我这样称呼这个时,foo(3, [0o7, 0o5])我收到一个错误,告诉我它无法匹配.

ERROR: LoadError: MethodError: no method matching foo(::Int64, ::Array{UInt8,1})
Closest candidates are:
  foo(::Union{Array{Integer,1}, Integer}, !Matched::Union{Array{Integer,1}, Array{Integer,2}}) at ...
  foo(::Union{Array{Integer,1}, Integer}, !Matched::Union{Array{Integer,1}, Array{Integer,2}}, !Matched::Union{Array{Integer,1}, Void}) at ...
Run Code Online (Sandbox Code Playgroud)

现在我明白了为什么朱莉娅无法Array{UInt8} <: Array{Integer} == false与之匹敌,但这似乎并不是朱莉娅的聪明.

然后我尝试了这个

foo(a::Union{Z1, Vector{Z1}},
    b::Union{Vector{Z2}, Matrix{Z2}},
    c::Union{Void, Vector{Z3}} = nothing
    ) where {Z1 <: Integer, Z2 <: Integer, Z3 <: Integer}
Run Code Online (Sandbox Code Playgroud)

现在朱莉娅甚至没有告诉我什么不匹配!

ERROR: LoadError: MethodError: no method matching foo(::Int64, ::Array{UInt8,1}, …
Run Code Online (Sandbox Code Playgroud)

julia

3
推荐指数
1
解决办法
144
查看次数

本征不受支持的张量到本征矩阵

Eigen::Tensor<std::complex, 2>对张量更大的张量进行了一些操作。是否有一种简单的方法可以Eigen::MatrixXcf从此Tensor对象创建,还是必须手动复制值?

eigen3

3
推荐指数
1
解决办法
1760
查看次数

在scala中包含参数和方法的枚举

使用java多年后,我试图进入scala.可以说我有一个像这样简单的枚举

public enum Foo{
  Example("test", false),
  Another("other", true);

  private String s;
  private boolean b;

  private Foo(String s, boolean b){
    this.s = s;
    this.b = b;
  }

  public String getSomething(){
    return this.s;
  }

  public boolean isSomething(){
    return this.b;
  }
}
Run Code Online (Sandbox Code Playgroud)

随着文档和stackoverflow的一些帮助,我得到了:

object Foo extends Enumeration
{
  type Foo = Value
  val Example, Another = Value

  def isSomething( f : Foo) : Boolean = f match {
    case Example => false
    case Another => true
  }

    def getSomething( f : Foo) : …
Run Code Online (Sandbox Code Playgroud)

enums scala

2
推荐指数
1
解决办法
1968
查看次数

与朱莉娅代码生成

我想为我自己的类型覆盖默认的字符串方法(因为我发现它们很难看).

此函数将用于生成字符串

function prettyPrint(value::Any)
  names::Vector{Symbol} = fieldnames(value)
  nameCount::Int64 = length(names)
  stringBuilder::IOBuffer = IOBuffer()
  print(stringBuilder, string(typeof(value).name) *"(")
  for (index, name) in enumerate(names)
    print(stringBuilder, string(name) * "=" * string(getfield(value, name)))
    if index != nameCount
      print(stringBuilder, ", ")
    end
  end
  print(stringBuilder, ")")
  takebuf_string(stringBuilder)
end
Run Code Online (Sandbox Code Playgroud)

让我们定义一个样本类型

type Foo
  a::Int64
  b::String
  c::Float64
end
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用这段代码生成字符串函数

import Base.string

for dataType in (:Foo)
  eval(quote
       function string(dt::$dataType)
        prettyPrint(dt)
       end
       end)
end

foo = Foo(1, "2", 3.0)
println(string(foo))
Run Code Online (Sandbox Code Playgroud)

这会因为一条冗长的错误消息而崩溃,告诉我什么都不能使用.

ERROR: LoadError: MethodError: no method matching start(::Symbol)
Closest candidates are:
  start(!Matched::SimpleVector) …
Run Code Online (Sandbox Code Playgroud)

julia

2
推荐指数
1
解决办法
184
查看次数

标签 统计

julia ×2

eigen3 ×1

enums ×1

scala ×1