以另一个函数作为参数调用一个函数会引发错误

时间:2019-07-18 18:12:45

标签: julia

我有一个函数,期望另一个函数作为参数,但是当我通过传递第二个函数来调用第一个函数时,会引发错误

using Random: RandomDevice, randstring

alphabet = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
size = 21

function generate(byte_gen::Function, alphabet::String=alphabet, size::Int64=size)::String
    len = length(alphabet)

    mask = 1
    if len > 1
        mask = 2 << log(2, (len-1)) - 1       
    end

    step = ceil((1.6 * mask * size) / len)

    id = ""
    while true
        bytes = byte_gen(size)

        for i = 0:(step-1)
            byte = bytes[i] & mask
            if alphabet[i]
                id += alphabet[i]
                if length(id) == size
                    return id
                end
            end
        end
    end
end


function randBytestring(size::Int64)::String
    rs = randstring(RandomDevice(), 'a':'z', size)
    ba = Vector{UInt8}(rs)
    return join([join(("\\x", split(repr(cu), "x")[2]), "") for cu in ba], "")
end


generate(randBytestring)

我得到了回溯

ERROR: LoadError: MethodError: no method matching <<(::Int64, ::Float64)
Closest candidates are:
  <<(::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, !Matched::Union{UInt128, UInt16, UInt32, UInt64, UInt8}) at int.jl:443
  <<(::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, !Matched::Int64) at int.jl:450
  <<(::Integer, !Matched::Unsigned) at operators.jl:568
  ...
Stacktrace:
 [1] generate(::typeof(randBytestring), ::String, ::Int64) at /home/palash25/Dev/julia/nano.jl:11
 [2] generate(::Function, ::String) at /home/palash25/Dev/julia/nano.jl:7 (repeats 2 times)
 [3] top-level scope at none:0
 [4] include at ./boot.jl:326 [inlined]
 [5] include_relative(::Module, ::String) at ./loading.jl:1038
 [6] include(::Module, ::String) at ./sysimg.jl:29
 [7] exec_options(::Base.JLOptions) at ./client.jl:267
 [8] _start() at ./client.jl:436
in expression starting at /home/palash25/Dev/julia/nano.jl:40

当我将函数签名更改为此并再次调用它时,出现另一个错误generate(byte_gen::Function, alphabet::String=alphabet, size::Int64=size)::String

ERROR: LoadError: MethodError: no method matching <<(::Int64, ::Float64)
Closest candidates are:
  <<(::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, !Matched::Union{UInt128, UInt16, UInt32, UInt64, UInt8}) at int.jl:443
  <<(::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, !Matched::Int64) at int.jl:450
  <<(::Integer, !Matched::Unsigned) at operators.jl:568
  ...
Stacktrace:
 [1] generate(::typeof(randBytestring), ::String, ::Int64) at /home/palash25/Dev/julia/nano.jl:11
 [2] generate(::Function, ::String) at /home/palash25/Dev/julia/nano.jl:7 (repeats 2 times)
 [3] top-level scope at none:0
 [4] include at ./boot.jl:326 [inlined]
 [5] include_relative(::Module, ::String) at ./loading.jl:1038
 [6] include(::Module, ::String) at ./sysimg.jl:29
 [7] exec_options(::Base.JLOptions) at ./client.jl:267
 [8] _start() at ./client.jl:436
in expression starting at /home/palash25/Dev/julia/nano.jl:40

更新:我已经编辑了代码片段以包含我的所有代码,以便该示例现在可以运行。

0 个答案:

没有答案