Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use an or ( || ) in a where clause?

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?

like image 346
Caleb Kleveter Avatar asked Feb 27 '17 17:02

Caleb Kleveter


People also ask

Can WHERE clause have 2 conditions?

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.

Which conditions can we use with WHERE clause?

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.

Can we use and operator in WHERE clause?

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.


1 Answers

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
}
like image 86
GetSwifty Avatar answered Nov 10 '22 01:11

GetSwifty