Unsigned 为什么不能作为形参类型

如题,谢谢!

julia> mutable struct Point{T <: Real}
           point::Array{T, 1}
           
           function Point{T}(dim::Integer) where T
               point = Array{T, 1}(undef, dim)
               new{T}(point)
           end
       end

julia> function compare(p1::Point, p2::Point, dim::Unsigned)
           @info "hello..."
       end
compare (generic function with 1 method)

julia> compare(Point{Int64}(2), Point{Int64}(2), 2)
ERROR: MethodError: no method matching compare(::Point{Int64}, ::Point{Int64}, ::Int64)
Closest candidates are:
  compare(::Point, ::Point, ::Unsigned) at REPL[2]:2
Stacktrace:
 [1] top-level scope at none:0

julia> function compare(p1::Point, p2::Point, dim::Integer)
           @info "hello..."
       end
compare (generic function with 2 methods)

julia> compare(Point{Int64}(2), Point{Int64}(2), 2)
[ Info: hello...


######################################################

julia> f(x::Unsigned)=println(typeof(x))
f (generic function with 1 method)

julia> g(x::Integer)=println(typeof(x))
g (generic function with 1 method)

julia> t(x::Signed)=println(typeof(x))
t (generic function with 1 method)

julia> f(1)
ERROR: MethodError: no method matching f(::Int64)
Closest candidates are:
  f(::Unsigned) at REPL[1]:1
Stacktrace:
 [1] top-level scope at none:0

julia> g(1)
Int64

julia> t(1)
Int64

DeepinScreenshot_select-area_20180820124058

论坛支持代码高亮

```
你的代码
```

这样方便别人复现你的问题。

这里f的参数是Unsigned,然后

julia> Unsigned <: Integer
true

julia> 1 isa Unsigned
false

julia> 1 isa Integer
true

所以,你可以试试f(Unsigned(1))

谢谢你的回复,我更新了一下帖子。

可能我前面没有说得很清楚。

f(x::Unsigned)=println(typeof(x))

这里定义了一个只能接收Unsigned类型的函数f,而1的类型是Int64,并不是Unsigned(也不是其子类),所以执行f(1)的时候就会报错,找不到f(::Int64)函数。你需要显式地自己转换,执行f(Unsigned(1))即可。

也就是说随便输入的一个入参 ‘x’ 都是 ‘Signed’ 的是吧,非常感谢!