I couldn't understand exact meaning of this statement.
let x where x.hasSuffix("pepper")
What is meaning of that?
Note: There is no need of let
use? It makes me confusing.. Is this enough x where x.hasSuffix("pepper")
? because, let x
should be already get assigned.?
Update: From @Jacky comment here, it could be meaning same as below.
let x = vegetable
if (x.hasSuffix("pepper")
......
The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.
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.
In a SQL statement, the WHERE clause specifies criteria that field values must meet for the records that contain the values to be included in the query results. For an overview of Access SQL, see the article Access SQL: basic concepts, vocabulary, and syntax.
T is called type parameter, which can be used as a type of fields, properties, method parameters, return types, and delegates in the DataStore class. For example, Data is generic property because we have used a type parameter T as its type instead of the specific data type. Note.
The where
in that context is used as pattern matching. From the example:
case let x where x.hasSuffix("pepper"):
When the suffix of x
matches "pepper"
it will set the constant vegetableComment
:
let vegetableComment = "Is it a spicy \(x)?"
You can see as well that x
can´t be "celery", "cucumber" or "watercress", otherwise it would give you a different outcome:
case "celery":
let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
let vegetableComment = "That would make a good tea sandwich."
Because those cases are before case let x where x.hasSuffix("pepper"):
. You can try changing the order of them and pass the value "celery" to see a different outcome.
Edit:
From my understanding it creates a constant x
if x
's suffix is "pepper". The goal of creating this constant, is for you to use it after that:
let vegetableComment = "Is it a spicy \(x)?"
Edit 2:
After a bit more research, that's called value binding and it's described as:
switch case can bind the value or values it matches to temporary constants or variables, for use in the body of the case. This is known as value binding, because the values are “bound” to temporary constants or variables within the case’s body.
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/gb/jEUH0.l
case let x where x.hasSuffix("pepper")
The simple explanation is that you cannot match a case, that is of type String
, with .hasSuffix()
because it returns a Bool
. So, they give you this where
pattern matching keyword to use. It works like this:
let x
copies the String value you are passing into the switch to the constant x. where
is a boolean evaluation that will only let the case complete the match if it is given a true bool, just like an if
block.hasSuffix()
returns the bool required by where
.If your string variable passed into the switch is var foo
. You can literally do this instead:
case foo where foo.hasSuffix("pepper")
You can pass a true bool to where like this and it would work, uselessly:
case foo where true
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