Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function definition overwriting in if statement #33198

Closed
federicoizzo opened this issue Sep 9, 2019 · 1 comment
Closed

function definition overwriting in if statement #33198

federicoizzo opened this issue Sep 9, 2019 · 1 comment

Comments

@federicoizzo
Copy link

Hi everyone, sorry to bother. I encountered a weird behaviour recently, and I wanted to show it.

I define a function in an if statement; it is defined in one way if "flag" is true and in another otherwise. But when I run and the code goes through the first case, the second definition is used even if that code is not passed through at all.

Example: the output of this code

function test_bug_1(flag)
c=987;
if flag
    d(x)=x
else
    d(x)=x-c
end
println(d(1))
end

test_bug_1(true)

The output is -986 instead of 1. If I "correct" the code in the following way it works as intended:

function test_bug_2(flag)
c=987;
if flag
    d1(x)=x
    d=d1
else
    d2(x)=x-c
    d=d2
end
println(d(1))
end

test_bug_2(true)

Output: 1

Here are the info from versioninfo():
julia> versioninfo()
Julia Version 1.2.0
Commit c6da87f (2019-08-20 00:03 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-6.0.1 (ORCJIT, broadwell)

Is this something I should have predicted in some way? Or is something weird happening? I greatly appreciate all the help.

@StefanKarpinski
Copy link
Member

Dup of #15602.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants