I myself have been hitting my head against the wall that is
meta-programming in Julia. I think I can answer your first question at
least.
Q1: This is because the line poly = emptyPoly doesn't create a new copy of
a ploygon but a reference to the empty one so that both poly and emptyPoly
refer
to the same data. You need to do poly = deepcopy(emptyPoly) .
On Thursday, May 8, 2014 9:56:27 AM UTC+3, Stéphane Laurent wrote:
>
> Hello everybody,
>
> Below I define two new types : Line and Poly. The Poly type is intended
> for stacking some lines.
>
> type Line
>
> a::Float64 # intercept
>
> b::BigFloat # slope
>
> x1::BigFloat # x-coordinate of first vertex
>
> y1::BigFloat # y-coordinate of first vertex
>
> x2::BigFloat # x-coordinate of second vertex
>
> y2::BigFloat # y-coordinate of second vertex
>
> typ::Bool # type of the line (true:upper, false:lower)
>
> end
>
>
> type Poly
>
> a::Vector{Float64}
>
> b::Vector{BigFloat}
>
> x1::Vector{BigFloat}
>
> y1::Vector{BigFloat}
>
> x2::Vector{BigFloat}
>
> y2::Vector{BigFloat}
>
> typ::Vector{Bool}
>
> end
>
>
>
> I also define the empty Poly:
>
> emptyPoly = Poly(Array(Float64,0), Array(BigFloat,0), Array(BigFloat,0),
> Array(BigFloat,0), Array(BigFloat,0), Array(BigFloat,0), Array(Bool,0));
>
>
>
> And a function to generate a new Line:
>
> function newLine(a::Float64, b::BigFloat, typ::Bool)
>
> return Line(a, b, BigFloat(Inf), BigFloat(Inf), BigFloat(Inf),
> BigFloat(Inf), typ)
>
> end
>
>
>
> Now I define some macros but it seems that I'm doing something bad :
>
> macro addLine(poly, line)
>
> for op = (:a, :b, :x1, :y1, :x2, :y2, :typ)
>
> @eval $poly.$op = [$poly.$op, $line.$op]
>
> end
>
> end
>
>
> macro removeLine(poly, index)
>
> for op = (:a, :b, :x1, :y1, :x2, :y2, :typ)
>
> @eval splice!($poly.$op, $index)
>
> end
>
> end
>
>
>
> There's something bad when I'm doing this:
>
> D = newLine(0.4, BigFloat("1.5"), false)
>
> poly = emptyPoly;
>
> @addLine poly D
>
>
>
> *Question 1:* I don't understand why the previous code changes the value
> of emptyPoly, as if poly and emptyPoly were linked:
>
> julia> emptyPoly
>
> Poly([0.4],[1.5e+00 with 256 bits of precision],[inf with 256 bits of
> precision],[inf with 256 bits of precision],[inf with 256 bits of
> precision],[inf with 256 bits of precision],[false])
>
>
>
> *Question 2:* There's another problem when I'm using the removeLine macro
> inside a loop :
>
> @addLine poly D
>
> @addLine poly D
>
> for index = [1 2]
>
> @removeLine(poly, index)
>
> end
>
>
> This code doesn't work and generates the error message "index not found".
>
>
>