Read(“count_heads.jl”, String) conversion error

I am going through Parallel Loops and Maps and I am seeming to have difficulty with even accessing the file count_heads.jl

I saved it in my working directory

function count_heads(n)
	c::Int = 0
	for i = 1:n
		c += rand(Bool)
	end
	c
end

And I can verify it’s existence in that directory:

filter(r"count_heads.jl", readdir())
#1-element Array{String,1}:
#"count_heads.jl"

Yet when I attempt the next step in the tutorial.

@everywhere include_string(Main, $(read("count_heads.jl", String)), "count_heads.jl")
ERROR: MethodError: Cannot `convert` an object of type Type{String} to an object of type Array{UInt8,1}
This may have arisen from a call to the constructor Array{UInt8,1}(...),
since type constructors fall back to convert methods.

Looking at just the read("count_heads.jl", String) function I get the source of the error (I think).

x-ref: Julia: read("count_heads.jl", String) conversion error - Stack Overflow

Yeah, I don’t know what the issue is. Maybe not a good SO question.

I think read("count_heads.jl", String) is valid only for Julia v0.7.

For earlier versions, you can either use readstring("count_heads.jl"), which is deprecated in v0.7.
Or to use the new syntax, using Compat; read("count_heads.jl", String)

It works on 0.6. @EconometricsBySimula are you on 0.5? Then you should use the appropriate version of the documentation.

Are you sure?

AFAICT, readstring was deprecated to read(io, String), but that PR (Deprecate readstring in favor of read(io, String) by jpfairbanks · Pull Request #22864 · JuliaLang/julia · GitHub) didn’t make it into v0.6.

On v0.6 or earlier, you can either use readstring("count_heads.jl"), or write using Compat before the new syntax read("count_heads.jl", String).

Also note that read(filename, String) should work in Juno, since Juno is implicitly using Compat.

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-apple-darwin13.4.0

julia> @everywhere include_string(Main, $(read("count_heads.jl", String)), "count_heads.jl")

works, at least.

Very strange.

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-w64-mingw32

Julia-0.6.0> @everywhere include_string(Main, $(read("count_heads.jl", String)), "count_heads.jl")
ERROR: MethodError: Cannot `convert` an object of type Type{String} to an object of type Array{UInt8,1}
This may have arisen from a call to the constructor Array{UInt8,1}(...),
since type constructors fall back to convert methods.
read(::IOStream, ::Type{T} where T) at .\io.jl:528
open(::Base.##190#191{Tuple{DataType}}, ::String) at .\iostream.jl:152
(::##1#3)() at .\distributed\macros.jl:102
Stacktrace:
 [1] sync_end() at .\task.jl:287
 [2] macro expansion at .\distributed\macros.jl:112 [inlined]
 [3] anonymous at .\<missing>:?

Perhaps a difference in Windows and Mac?

You don’t have using Compat in your .juliarc.jl? (can see if Compat is loaded by running whos())

1 Like

Well, what do you know, that’s it :slight_smile: I have using OhMyREPL in my .juliarc.jl file, and that loads Compat.