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

[SR-12043] Top-level variables in REPL #54479

Open
swift-ci opened this issue Jan 16, 2020 · 5 comments
Open

[SR-12043] Top-level variables in REPL #54479

swift-ci opened this issue Jan 16, 2020 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12043
Radar rdar://problem/58819915
Original Reporter svanimpe (JIRA User)
Type Bug
Environment

Swift 5.1.3

Additional Detail from JIRA
Votes 3
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 3aa2a9d5cebddd4796f89d36aa578bf6

Issue Description:

I've noticed that the REPL assigns default values when declaring (but not initializing) a variable:

  1> var d: Double
d: Double = 0
  2> var i: Int
i: Int = 0
  3> var t: (Int, Int)
t: (Int, Int) = {
  0 = 0
  1 = 0
}
  4> var f: (Int) -> Int
f: (Int) -> Int =
  5> var b: Bool
b: Bool = false

Is this behavior documented somewhere? Is this even intentional, or is it just showing the contents of memory?

I believe this is a recent change, as I remember seeing a "top-level variable must be initialized" error for at least some of these examples with Swift 4.

@CodaFi
Copy link
Member

CodaFi commented Jan 23, 2020

It's not documented, and it allows for the creation of undefined behavior: the value you get initialized with is usually the zero bitpattern, so you can create ill-typed null pointers out of thin air.

@CodaFi
Copy link
Member

CodaFi commented Jan 23, 2020

@swift-ci create

@dcci
Copy link
Mannequin

dcci mannequin commented Jan 23, 2020

Yes, this is intentional.
The whole story is a little messy here, we should really reconsider if we get to reply the REPL to properly match the semantics of the language.

@dcci
Copy link
Mannequin

dcci mannequin commented Jan 23, 2020

More generally, the REPL does a lot of stuffs that are not really kosher from a language perspective because it was originally thought as a tool to "experiment". If you ask me, the semantics of the REPL and the one of the language should be a 1:1 mapping, but it's really hard to get to that point without breaking, e.g. existing playgrounds.

@swift-ci
Copy link
Collaborator Author

Comment by Steven Van Impe (JIRA)

If Swift is going to support more platforms in the future, the REPL will become an important teaching tool, as the point of entry for new learners.

With that in mind, I hope the REPL will evolve to match the language 1:1 and become more reliable, as issues like this, and SR-8690, and SR-1743, can easily leave a bad impression on new learners.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

2 participants