[swift-evolution] class/struct inner member access scope classifier
Ted F.A. van Gaalen
tedvgiosdev at gmail.com
Mon Sep 26 14:58:34 CDT 2016
Hello! Hope you are all OK!
Using and converting To Swift 3.0 with many advantages
and very little problems.OK, thanks to all !
also for the reasonably smart converter. Yes, yes, yes, I’m
still missing the classical for ;; loop (don’t wake me up
again plse :o) but overall it is quite good!
I also value the -finally correct- meaning of the scope qualifier “private” , thanks,
which is as it should be (imho) inner scope restricting.
As for “fileprivate”: as yet, I haven’t found a case
where I should use “fileprivate” also because to me, the contents of
a file should in principle not have anything to do with the entities
contained in it, as a file should be just an data carrier. Therefore,
it should have no effect to wether or not concatenate source.swift files
into one big file for example, although I would not recommend this.
As far as I can see without binoculars, the “fileprivate” acces modifier
could be dropped, were it not for source compatibility reasons...
Unless I am missing something:
Still. something is not quite right yet, I think.
Just like in a Swift function, I don’t want the inner elements
of a class (or struct ?) to be visible in outer scope!
This is the default case in most OOP languages.
I fail to understand why this is not so in Swift, please enlighten me.
As it is now, and as far as I know, I have to explicitly
declare *all* entities inside a class that should not be accessible
outside the class as private, like in this real-world example:
class TG3DGauge: SCNNode
{
private var needles = [SCNNode]()
private var fmtStr = “" // private should be the default imho.
var value: CGFloat = 0
{
didSet // trigger value change, rotate needles etc.
{
valueChange()
}
}
private var nodeUnitText = SCNNode()
private var nodeValueText = SCNNode()
private var valRange: ClosedRange<CGFloat> = (0...100.0)
var rangeNeedlesActive: Bool = true
{
didSet
{
needles[2].isHidden = !rangeNeedlesActive
needles[3].isHidden = !rangeNeedlesActive
}
}
private var valScaleFactor: CGFloat = 1
// etc. more stuff
.
.
} // end class TG3DGauge
It should (imho) be the other way around: that every member of
a class is private by default - that is invisible outside the scope
were it is declared in. This was (and still is) the case with Objective C,
where you need to explicitly declaring them in the sourcefile.h file
to make them visible and accessible in the outer scope
Everything else, as existing in the sourcefile.m remains hidden, not visible
in the outer scope.
As a solution/suggestion and also to prevent the gruesome
horror (did i already wrote something about that? :o) of source breaking.
I could think of the “closedscope” (or some other word)
access modifier, which states that all things declared inside a class,
are private within the class and thus invisible in the outer scope, unless
preceeded with an overriding acces scope modifier like “public", “internal" or "fileprivate”
closedscope class TG3DGauge: SCNNode
{
var needles = [SCNNode]() // is now private by default
var fmtStr = “" // is now private by default
public var value: CGFloat = 0 // Public!! visible outside class also for “fileprivate" or “internal”
{
didSet // trigger value change, rotate needles etc.
{
valueChange()
}
}
var nodeUnitText = SCNNode() // is now private by default
var nodeValueText = SCNNode() // is now private by default
//etc.
This “closedscope" modifier should only be effective for the current class, not its superclass(es),
allowing one to hide/reveal entities in each class independently being part of the hierarchy.
Also entities declared private should not be visible in descendant classes
Also please note that I do miss the “protected” scope access modifier which allows entities to be exclusively
visible to descendant classes, as in Java. Why not implement it in Swift as well?
AFAICS: All mentioned here would not be a source breaking.
Opinions, remarks welcome.
met vriendelijke groeten
Ted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160926/4e4667aa/attachment.html>
More information about the swift-evolution
mailing list