# Read(“count\_heads.jl”, String) conversion error

**URL:** <https://discourse.julialang.org/t/read-count-heads-jl-string-conversion-error/6253>\
**Category:** Julia at Scale\
**Tags:** documentation, parallel\
**Created:** [October 4, 2017, 9:45pm UTC](https://discourse.julialang.org/t/read-count-heads-jl-string-conversion-error/6253 "2017-10-04T21:45:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![EconometricsBySimula](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/econometricsbysimula/32/1255_2.png) [@EconometricsBySimula](https://discourse.julialang.org/u/EconometricsBySimula)\
**Post date:** [October 4, 2017, 9:45pm UTC](https://discourse.julialang.org/t/read-count-heads-jl-string-conversion-error/6253/1 "2017-10-04T21:45:31Z")

</div>

I am going through [Parallel Loops and Maps](https://docs.julialang.org/en/latest/manual/parallel-computing#Parallel-Map-and-Loops-1) 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).

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [October 4, 2017, 9:57pm UTC](https://discourse.julialang.org/t/read-count-heads-jl-string-conversion-error/6253/2 "2017-10-04T21:57:00Z")

</div>

x-ref: [Julia: read("count\_heads.jl", String) conversion error - Stack Overflow](https://stackoverflow.com/questions/46573241/julia-readcount-heads-jl-string-conversion-error)

---

<div class="post-metadata">

**Author:** ![EconometricsBySimula](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/econometricsbysimula/32/1255_2.png) [@EconometricsBySimula](https://discourse.julialang.org/u/EconometricsBySimula)\
**Post date:** [October 4, 2017, 10:03pm UTC](https://discourse.julialang.org/t/read-count-heads-jl-string-conversion-error/6253/3 "2017-10-04T22:03:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [October 5, 2017, 5:38am UTC](https://discourse.julialang.org/t/read-count-heads-jl-string-conversion-error/6253/4 "2017-10-05T05:38:54Z")

</div>

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)`

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [October 5, 2017, 9:33am UTC](https://discourse.julialang.org/t/read-count-heads-jl-string-conversion-error/6253/5 "2017-10-05T09:33:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [October 8, 2017, 8:33pm UTC](https://discourse.julialang.org/t/read-count-heads-jl-string-conversion-error/6253/6 "2017-10-08T20:33:55Z")

</div>

> [@mkborregaard](#):
>
> It works on 0.6.

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](http://github.com/JuliaLang/julia/pull/22864)) 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`.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [October 8, 2017, 8:53pm UTC](https://discourse.julialang.org/t/read-count-heads-jl-string-conversion-error/6253/7 "2017-10-08T20:53:35Z")

</div>

```julia
               _
   _ _ _(_)_ | 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.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [October 8, 2017, 9:45pm UTC](https://discourse.julialang.org/t/read-count-heads-jl-string-conversion-error/6253/8 "2017-10-08T21:45:29Z")

</div>

Very strange.

```julia
               _
   _ _ _(_)_ | 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()`)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [October 9, 2017, 3:43am UTC](https://discourse.julialang.org/t/read-count-heads-jl-string-conversion-error/6253/9 "2017-10-09T03:43:36Z")

</div>

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