Julia 使用另一个函数作为参数调用函数会引发错误
我有一个函数需要另一个函数作为参数,但是当我通过传递第二个函数来调用第一个函数时,它会引发一个错误Julia 使用另一个函数作为参数调用函数会引发错误,julia,Julia,我有一个函数需要另一个函数作为参数,但是当我通过传递第二个函数来调用第一个函数时,它会引发一个错误 using Random: RandomDevice, randstring alphabet = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" size = 21 function generate(byte_gen::Function, alphabet::String=alphabet, size::
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)
更新:我已经编辑了代码片段以包含我的所有代码,这样示例现在就可以运行了。请包含一个完整的、可以运行的、重现问题的示例。错误消息说调用
@DNF编辑的帖子有问题,请看一看。错误告诉您出了什么问题:您不能使用,但是,为什么您认为这与将函数作为参数传递有关?你的两个例子和回溯有什么不同?他们看起来和我一样?请包括一个完整的,可运行的例子,再现问题。错误消息说调用@DNF编辑的帖子有问题,请看一看。错误告诉您出了什么问题:您不能使用,但是,为什么您认为这与将函数作为参数传递有关?你的两个例子和回溯有什么不同?他们看起来和我一模一样?
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
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