我可以将关键字args传递给julia中的do-blocks吗?

时间:2019-01-03 13:34:08

标签: julia

下面是一个示例:

def(f::Function) = f

f1 = def() do x
    x
end

f2 = def() do x, y
    x, y
end

f3 = def() do x, y; z # this syntax is accepted
    x, y, z
end

f1(1)
f2(1, 2)
f3(1, 2, z=3) # ERROR: LoadError: function #13 does not accept keyword arguments

以下示例不是我的实际用例,但它说明了问题。我应该如何在do-block中处理kwarg?

2 个答案:

答案 0 :(得分:3)

据我所知这是不可能的。如果查看/src/julia-parser.scm文件,则会发现在parse-do here中定义了do块的解析。您会看到do-block被重写为一个匿名函数,该函数只能接受doargs变量中的位置参数,并用逗号分隔。

this行中,您可以看到;明确终止了do块的参数解析。这意味着:

julia> z = "hello"
"hello"

julia> f3 = def() do x, y; println(z) # this syntax is accepted
           x, y, z
       end
#24 (generic function with 1 method)

julia> f3(100, 1000)
hello
(100, 1000, "hello")

;之后的代码视为do块主体的一部分。

您可以尝试将()放在这样的参数周围:

julia> f3 = def() do (x, y; z)
           x, y, z
       end
ERROR: syntax: invalid assignment location "; z"

julia> f3 = def() do (y; z)
           y, z
       end
ERROR: syntax: "begin
    y
    # REPL[52], line 1
    z
end" is not a valid function argument name

这将通过解析器,但随后julia-syntax.scm将引发错误。可以通过以下方式对此进行改进:

f3 = def(function(x, y; z)
    x, y, z
end)

正常工作(您可以考虑在https://github.com/JuliaLang/julia/issues上为此打开一个问题)。

在解决此问题之前,您唯一可以做的就是使用我上面给出的匿名函数定义方法(尽管它不是很干净)。

答案 1 :(得分:0)

您可以这样做,只需在function planTrail() { //do this if the values check out.. var large_dashboard = document.createElement('div'); var large_dashboard = document.createElement('div'); large_dashboard.id = "large_dash"; large_dashboard.style.backgroundColor = "#515151"; large_dashboard.style.border = "solid 2px black"; large_dashboard.style.height = "58vh"; large_dashboard.style.position = "fixed"; large_dashboard.style.borderRadius = "1em"; large_dashboard.style.right = "10vw"; large_dashboard.style.top = "25vh"; large_dashboard.style.width = "8vw"; var current_section = document.getElementById("first_part_page"); document.body.insertBefore(large_dashboard, current_section); var large_dash = document.getElementById("large_dash"); large_dash.style.setProperty("-webkit-transition", "width 2s linear 0s"); large_dash.style.setProperty("-moz-transition", "width 2s linear 0s"); large_dash.style.setProperty("-o-transition", "width 2s linear 0s"); large_dash.style.setProperty("transition", "width 2s linear 0s"); setTimeout(function () { large_dash.style.width = "80vw"; }, 0); } 之前通过kwargs

do