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

Interpreter: allow declaring local vars during a pry session #12180

Merged
merged 4 commits into from
Jul 13, 2022

Conversation

asterite
Copy link
Member

@asterite asterite commented Jul 1, 2022

If you have a program like this:

a = 1
degugger
b = 2

When you run it through the interpreter, this would happen before this PR:

From: foo.cr:3:5 <Program>#foo.cr:

    1: a = 1
    2: debugger
 => 3: b = 2

pry> a
1
pry> c = 3
BUG: can't find closured var or local var c (Exception)

That is, you couldn't declare new local variables during a pry session. I think that reduces the usefulness of pry by a lot!

With this PR:

From: foo.cr:3:5 <Program>#foo.cr:

    1: a = 1
    2: debugger
 => 3: b = 2

pry> c = 3
3
pry> c
3
pry> c = "hello!"
"hello!"
pry> c
"hello!"

That is, not only we can declare new local variables, we can also modify their type. This is similar to how it works when you just do crystal i, outside of pry.

Note that you will get an error if you try to change the type of a, because that's already compiled and it would be wrong to change its type:

From: foo.cr:3:5 <Program>#foo.cr:

    1: a = 1
    2: debugger
 => 3: b = 2

pry> a = 'a'
error in line 1
Error: type must be Int32, not (Char | Int32)
pry> a
1
pry> typeof(a)
Int32

@Blacksmoke16 Blacksmoke16 added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:interpreter labels Jul 1, 2022
@asterite
Copy link
Member Author

asterite commented Jul 5, 2022

Will this make it to 1.5.0?

@cyangle
Copy link
Contributor

cyangle commented Jul 7, 2022

This should definitely be included in 1.6.0

@straight-shoota straight-shoota added this to the 1.6.0 milestone Jul 12, 2022
@asterite asterite merged commit e540dc2 into master Jul 13, 2022
@asterite asterite deleted the interpreter/pry-declare-local-vars branch July 13, 2022 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. kind:feature topic:compiler:interpreter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants