[swift-evolution] Idea: Let Generic Parameters Have Labels & Default Values

David Sweeris davesweeris at mac.com
Sat Jan 23 14:38:52 CST 2016


The title seems fairly self-explanatory. Does anyone else think this could be useful?
struct True : BooleanType {…} // Just part of the example… not in the proposal (although I do like it)
struct False : BooleanType {…}  // Same
// This is where the actual idea starts
struct BigInt <BaseType: T = Int, CanEqualZero: U = Yes where T: IntegerArithmeticType, U: BooleanType> {…}

The first parameter label could be skipped (or not), depending on whatever the rules for functions parameter labels ends up being (either way, they should be the same IMHO). Then variables could be declared like this:
let foo =   BigInt()	    // BigInt<Int, No>()
let bar =   BigInt<Int32>() // For when your data will be processed on a 32-bit platform or something
let divisor = BigInt<CanEqualZero: False>()

(The obvious follow-up suggestion is to then allow a generic type’s definition to change based on the results of logical operations performed purely on the types that are passed in, but I think that’s getting into “macro system” territory, and should probably be its own thing.)

Anyway, thoughts?

- Dave Sweeris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160123/c176370b/attachment.html>


More information about the swift-evolution mailing list