# Macro problem with v0.6

**URL:** https://discourse.julialang.org/t/macro-problem-with-v0-6/1581
**Category:** Internals & Design
**Created:** [January 19, 2017, 7:08am UTC](https://discourse.julialang.org/t/macro-problem-with-v0-6/1581 "2017-01-19T07:08:22Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)
#### Post date: [January 19, 2017, 7:08am UTC](https://discourse.julialang.org/t/macro-problem-with-v0-6/1581/1 "2017-01-19T07:08:22Z")

</div>

The following macro (which is used in a wrapper for a C library) is no longer working with Julia v0.6. Does anyone have an idea how to fix that?

```julia
macro ArrayToVector(ctype, data)
    return :( convert(Vector{$ctype}, vec($data)) )
end

function myfunc(data)
  _data = [Float32(x) for x in data]
  println(@ArrayToVector(Float32, _data))
end

data = randn(10,10)
myfunc(data)

```

The error is:

```julia
ERROR: LoadError: UndefVarError: _data not defined
Stacktrace:
 [1] myfunc(::Array{Float64,2}) at /Users/jheinen/Home/Developer/GR.jl/src/t.jl:7
 [2] include_from_node1(::String) at ./loading.jl:532
 [3] include_from_node1(::String) at /usr/local/Applications/Julia-0.6.app/Contents/Resources/julia/lib/julia/sys.dylib:?
 [4] include(::String) at ./sysimg.jl:14
 [5] include(::String) at /usr/local/Applications/Julia-0.6.app/Contents/Resources/julia/lib/julia/sys.dylib:?
 [6] process_options(::Base.JLOptions) at ./client.jl:308
 [7] _start() at ./client.jl:374
 [8] _start() at /usr/local/Applications/Julia-0.6.app/Contents/Resources/julia/lib/julia/sys.dylib:?
while loading /Users/jheinen/Home/Developer/GR.jl/src/t.jl, in expression starting on line 11

```

---

<div class="post-metadata">

### Author: ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)
#### Post date: [January 19, 2017, 7:51am UTC](https://discourse.julialang.org/t/macro-problem-with-v0-6/1581/2 "2017-01-19T07:51:03Z")

</div>

Probably it will suffice to escape the arguments, i.e.

```julia
macro ArrayToVector(ctype, data)
    return :( convert(Vector{$(esc(ctype))}, vec($(esc(data)))) )
end

```

This change is required because a macro hygiene bug when a macro is used in its own module has been fixed in 0.6.

---

<div class="post-metadata">

### Author: ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)
#### Post date: [January 19, 2017, 7:53am UTC](https://discourse.julialang.org/t/macro-problem-with-v0-6/1581/3 "2017-01-19T07:53:37Z")

</div>

@fengyang.wang : Thanks a lot !

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 19, 2017, 1:25pm UTC](https://discourse.julialang.org/t/macro-problem-with-v0-6/1581/4 "2017-01-19T13:25:38Z")

</div>

Is there a reason not to just use a standard function for this? A macro does not seem to be necessary at a glance.

---

<div class="post-metadata">

### Author: ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)
#### Post date: [January 19, 2017, 1:43pm UTC](https://discourse.julialang.org/t/macro-problem-with-v0-6/1581/5 "2017-01-19T13:43:32Z")

</div>

I’m trying to use the **same** wrapper code vor Julia v0.4, v0.5 and v0.6. This is another example I’m struggling with:

```julia
V = randn(10,10)
values = round.(UInt16, (V-minimum(V)) / (maximum(V)-minimum(V)) * (2^16-1))

```

Works fine with 0.5 and 0.6, but **not** with 0.4. I tried something like:

```julia
values = UInt16[round(UInt16, (_-minimum(_)) / (maximum(_)-minimum(_)) * (2^16-1)) for _ in V]

```

… but this leads to an `InexactError()`

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 19, 2017, 5:42pm UTC](https://discourse.julialang.org/t/macro-problem-with-v0-6/1581/6 "2017-01-19T17:42:05Z")

</div>

> [@jheinen](#):
>
> `UInt16[round(UInt16, (_-minimum(_)) / (maximum(_)-minimum(_)) * (2^16-1)) for _ in V]`

This has some bugs (you are computing the `minimum` and `maximum` of the individual elements, not of the whole array). I think you want something like

```julia
function foo(V)
    m = minimum(V)
    s = typemax(UInt16) / (maximum(V) - m) 
    return [round(UInt16, (v-m) * s) for v in V]
end

```

---

<div class="post-metadata">

### Author: ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)
#### Post date: [January 19, 2017, 6:30pm UTC](https://discourse.julialang.org/t/macro-problem-with-v0-6/1581/7 "2017-01-19T18:30:38Z")

</div>

Should have taken a closer look - thanks a lot

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [January 25, 2017, 7:00pm UTC](https://discourse.julialang.org/t/macro-problem-with-v0-6/1581/8 "2017-01-25T19:00:20Z")

</div>

This is a bit more efficient since it computes the extrema in a single pass:

```julia
function foo(V)
    m, M = extrema(V)
    s = typemax(UInt16)/(M-m) 
    return [round(UInt16,(v-m)*s) for v in V]
end

```

---

<div class="post-metadata">

### Author: ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)
#### Post date: [January 26, 2017, 6:41am UTC](https://discourse.julialang.org/t/macro-problem-with-v0-6/1581/9 "2017-01-26T06:41:06Z")

</div>

Thanks, Good to know that there is something like `extrema()` …
