0

in swift, the default modifier for `init()` of a struct is `internal`, regardless what the struct's modifier is. why can't they just change it to inherit the modifier from the struct? why would anybody want to define a public struct and yet keep the `init()` internal????

Comments
  • 0
    Ah, finally, a language that's sensible about constructors.

    I think it's because not all structs can be initialized synchronously, so even when it is possible, making this part of the public interface is not always a good idea, therefore the language level default should be that constructors are private and the programmer should decide whether to

    - make the constructor public thereby committing to the synchronicity of initialization or
    - expose a factory function which conceals whether and when the constructor is called.
  • 0
    @lorentz the longer story is, if you don't explicitly specify a init(), they will by default create one implicitly for you, that default one has "internal" modifier, meaning that will be accessible from within the same project. not sure if this is what you want as my understanding is that you want it actually to be `private`. This default init() is actually quite convenient, it allows you to initialise all the none private properties as init parameters. but the trouble is, because the default `internal` modifier, there is NO way to expose the implicit default init() to external project, hence you will have to manually repeatitively creating the init() explicitly for each struct you want to expose to external project, even tho you've already gave it a modifier 'public'.
  • 0
    @lorentz what's even worse is, once you've done the explicit init(), because the swift language will stop creating the implicity init() for you, so starting from now you are responsible for making sure the params in `init(...params) ` is always up-to-date with every changes of public properties defined in the struct. combined with no tracing GC and only ARC, i had a feeling that I merely a miserable robot that keep doing meaningless repetitions as instructed by genius apple engineers.
Add Comment