【发布时间】:2018-09-16 07:16:03
【问题描述】:
我对 Julia 1.0.0 REPL 中 typeof 的以下结果感到困惑:
# This makes sense.
julia> typeof(10)
Int64
# This surprised me.
julia> typeof(function)
ERROR: syntax: unexpected ")"
# No answer at all for return example and no error either.
julia> typeof(return)
# In the next two examples the REPL returns the input code.
julia> typeof(in)
typeof(in)
julia> typeof(typeof)
typeof(typeof)
# The "for" word returns an error like the "function" word.
julia> typeof(for)
ERROR: syntax: unexpected ")"
Julia 1.0.0 documentation 表示 typeof
"获取 x 的具体类型。"
typeof(function) 的例子确实让我感到惊讶。我希望 function 成为 Julia 中的一流对象并具有类型。我想我需要了解 Julia 中的类型。
有什么建议吗?
编辑
根据下面的一些评论问题,这里是一个基于小函数的示例:
julia> function test() return "test"; end
test (generic function with 1 method)
julia> test()
"test"
julia> typeof(test)
typeof(test)
根据这个例子,我预计typeof(test) 会返回generic function,而不是typeof(test)。
【问题讨论】:
-
您对
typeof(function)的期望是什么?即,您期望返回的值是多少? -
某种类型名称,函数是其类型。
-
我不是 Julia 专家,但
function不只是开始定义函数的关键字,而不是函数本身吗? -
typeof对实际命名函数或匿名函数显示什么? -
不是
function关键字是一等公民。 functions 是 Julia 中的 first-class citizens。function关键字不是 a 函数。您所期望的是 the 关键字function是 a 函数,但事实并非如此。甚至有些函数式编程语言没有这样的function关键字。
标签: julia