Should you declare two private variables in differnet files with the same name?

In Swift, this is a compilation error:

// file1.swift

private let testVar = 2

// file2.swift

private let testVar = 3

      

Specifically, one of the declarations will be highlighted "Invalid reuse of testVar".


The documentation says that:

Private access restricts the use of an object in its own defining source file. Use private access to hide implementation details of a specific functionality.

When discussing raw values, it says:

Types used for any source values ​​or associated values ​​in an enumeration definition must have an access level at least as high as an enumeration access level. For example, you cannot use a private type as an internally accessed raw enumeration value type.

And regarding constants:

A constant, variable, or property cannot be more public than its type. For example, it is illegal to write a public property with a private type. Likewise, an index cannot be more open than its index type or return type.


To me, none of these passages suggest that you shouldn't have variables private

with the same name in separate files.

Indeed, the first quote tells me that you should be able to; otherwise, you will miss the implementation details (i.e. the fact that you used a named variable testVar

).


Before I write a bug report, I wanted to check that I am not misunderstanding the semantics. If possible?

+3


source to share


2 answers


This has been fixed in Xcode 6.1 beta 2:



Issues resolved in Xcode 6.1 Beta strong>

Swift language

Private objects with the same name and the same type will no longer clash if they are defined in different files of the same module. (17632175)

+2


source


The quick documentation states:

By default, variables, constants, and other named declarations declared at the top level of a source file are available to code in every source file that is part of the same module.



You will need to have your files in another module or have variables inside the class.

0


source







All Articles