I am trying to extend the Array
type, but I only want the functions available if the type is Int
or Float
.
I know I can do this for one type:
extension Sequence where Iterator.Element == Int { }
But can I do it for multiple types? This is sort of what I want:
extension Sequence where Iterator.Element == Int || Iterator.Element == Float { }
Is it possible to accomplish this?
You can specify multiple conditions in a single WHERE clause to, say, retrieve rows based on the values in multiple columns. You can use the AND and OR operators to combine two or more conditions into a compound condition.
The SQL WHERE clause is used to specify a condition while fetching the data from a single table or by joining with multiple tables. If the given condition is satisfied, then only it returns a specific value from the table. You should use the WHERE clause to filter the records and fetching only the necessary records.
The WHERE clause can be combined with AND , OR , and NOT operators. The AND and OR operators are used to filter records based on more than one condition: The AND operator displays a record if all the conditions separated by AND are TRUE.
This doesn't really work conceptually. Using the where in an extension allows you to use Element as the Type you're specifying. If you're saying it can be multiple Types, you might as well not have the where specifier at all.
If you're looking to add specific functionality for multiple types, I would recommend creating an empty protocol and add adherence to the desired Types. e.g:
protocol WorksWithExtension { }
extension Int: WorksWithExtension { }
extension Float: WorksWithExtension { }
extension Sequence where Iterator.Element: WorksWithExtension {
//Do whatever you need to do here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With