Where T <: Any

I am trying to rewrite

julia> function permutations(v::Vector{T}) where T <: Any
                  nbase= length(v)
                  results= Vector{Vector{T}}(0)
                  for it=0:(nbase^nbase-1)
                      push!( results, v[digits( it; base=nbase, pad=nbase ) .+ 1] )
                  end#for
                  results
              end#function##

Under 0.7, I got the helpful warnings, that led me to write a function that no longer works under 1.0 (and that readers should now ignore, given the solution):

julia> function permutations(v::Vector{undef})
                   nbase= length(v)
                   results= Vector{typeof(v)}(0)
                   for it=0:(nbase^nbase-1)
                       push!( results, v[digits( it; base=nbase, pad=nbase ) .+ 1] )
                   end#for
                   results
               end#function##
permutations (generic function with 2 methods)

julia> permutations( [ 'a', 'b' ] )
4-element Array{Array{Char,1},1}:
 ['a', 'a']
 ['b', 'a']
 ['a', 'b']
 ['b', 'b']

Alas, under 1.0, the function gives

julia> permutations( [ 'a', 'b' ] )
ERROR: MethodError: no method matching permutations(::Array{Char,1})
Closest candidates are:
  permutations(::Array{array initializer with undefined values,1}) at REPL[1]:2
Stacktrace:
 [1] top-level scope at none:0

(PS: this is illustrative, but if this function already exists in julia, please let me know.)

What is the helpful warning you’ve got? Can you paste and follow that exactly? Your “fix” appears to be wrong. (It should be a warning about the constructor while you changed the type signature in a strange way).

I’m amused that it didn’t error but the reason your “fix” seems to work is that you didn’t actually removed the previous method, you are still calling the method with the depwarn which isn’t printing it anymore after the first call.

1 Like

Do note that femtocleaner will do all the deprecations in this example quite professionally.

1 Like

the helpful warning in 0.7 was

julia> permutations( [ 'a', 'b' ] )
┌ Warning: `Array{T, 1}(m::Int) where T` is deprecated, use `Array{T, 1}(undef, m)` instead.
│   caller = permutations(::Array{Char,1}) at REPL[94]:3
â”” @ Main ./REPL[94]:3

which allowed me to guess the function that worked under 0.7. nice. alas, not under 1.0.

maybe I do need to discover femtocleaner… :slight_smile: well, actually, I am not using github, so this is not going to work for me.

I assume you realized that Array{T, 1} is Vector{T} so it’s asking you to change the call to the method Vector{T}(m::Int) where T to Vector{T}(undef, m) instead.

  1. The first one is a method signature, the second is the call syntax, that’s why the where T is not there. It has nothng to do with removing where T from the code.
  2. The undef is added in () not {}.
1 Like

hi yuyichao—going too fast. can you please write out the correct version for me? /iaw

I think what yuyichao and the error message means is that in your original function,
results= Vector{Vector{T}}(0) should be results = Vector{Vector{T}}(undef, 0).

oh, I see. I was staring at the code that worked fine under 0.7, and was thinking that was the one that needed a correction. Thank you.

No, as I said in the first post, it didn’t work. The whole thing works because you are not using it.