What is wrong with this function

julia> function setBit{I<:Integer}(i::I,bit::Int64)
           return (i | (1 << (bit-1)))::I
       end
ERROR: UndefVarError: `setBit` not defined
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

This function is from a github repository. The purpose apparently is to set some bit to 1.

where does the code go wrong?

Normal functions in julia 1.0+ can not have type parameters in that position.
only constructors for objects.
IIRC there was a version of julia 0.x where they could but they worked like where clauses.

You probably intended:

julia> function setBit(i::I, bit::Int64) where {I<:Integer}
           return (i | (1<< (bit-1)))::I
       end
setBit (generic function with 1 method)
1 Like

If you find code written before Julia 1.0 it can help to run it on an old version. Notice the helpful deprecation message.

$ juliaup add 0.7
Installing Julia 0.7.0+0.x64.linux.gnu
$ julia +0.7
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.7.0 (2018-08-08 06:46 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> function setBit{I<:Integer}(i::I,bit::Int64)
           return (i | (1 << (bit-1)))::I
       end
┌ Warning: Deprecated syntax `parametric method syntax setBit{I <: Integer}(i::I, bit::Int64)` around REPL[0]:2.
│ Use `setBit(i::I, bit::Int64) where I <: Integer` instead.
└ @ REPL[0]:2
setBit (generic function with 1 method)
1 Like