(1) Isn't using a class function just one more symbol? <br><br> let a = Key.forRSA()<br><br>(2) Wouldn't this syntax be more logical:<br>Key {<br> init(forRSA: Void) { /* */ }<br> }<br><br>Key(forRSA:)<br>The only change compared to what we have now would be auto-generation of (). <br><br><div class="gmail_quote"><div dir="ltr">On Sun, Dec 13, 2015 at 15:50 Drew Crawford via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Oftentimes I want to have multiple parameterless constructors. In the immediate case, I want a Key for various different cryptographic algorithms:<br>
<br>
let a = Key(forRSA: true) //2048-bits<br>
let b = Key(forCurve25519: true) //256-bits<br>
let c = Key(forAES: true) //128-bits<br>
<br>
There is no "parameter" to pass here; in particular the user should not be expected to know off the top of their head what the key size for Curve25519 is.<br>
<br>
With the current limits of the language, I am generally forced to vend a parameter and associated label (I typically use type Bool) and pass a "dontcare" value in, e.g. `true` in the above.<br>
<br>
I propose to eliminate this, so I can write<br>
<br>
Key {<br>
init(forRSA) { /* */ }<br>
}<br>
let a = Key(forRSA)<br>
<br>
This eliminates the parameter, and the associated mystery about what happens if you pass `false` to one of these constructors.<br>
<br>
FAQ:<br>
<br>
Q: Can't you have one constructor that takes an enum parameter?<br>
A: Enum parameters cannot have cases with distinct access modifiers, as constructors often are. Also, combining unrelated code into one constructor makes me sad.<br>
<br>
Q: Can't you create subclasses RSAKey, Curve25519Key etc. with distinct constructors?<br>
A: Well first of all, Key is probably a `struct`, so no, you can't create subclasses of it. Also, the only thing differentiating them is the constructor, not e.g. other overridden methods or variables, so subclassing feels an unnecessarily heavy abstraction.<br>
<br>
Q: Can we extend this to support arbitrary labels e.g. `init(random length: 2000)` vs `init(zeroed length: 2000)`, `mkdir(path, creatingIntermediateDirectoriesIfRequired)` and many more?<br>
A: Yes, although these are probably more controversial proposals than simply supporting multiple parameterless constructors.<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>