This question already has an answer here:
How does CoffeeScript's existential operator work?
(1 answer)
Closed 8 years ago.
Is there a way to force Coffeescript to always convert
if x?
console.log "hello"
to
if (typeof x !== "undefined" && x !== null) {
console.log("hello");
}
The reason I'm asking is because if we have the following:
x = "hello"
if x?
console.log "hello"
It gets converted to:
var x;
x = "hello";
if (x != null) {
console.log("hello");
}
While this is not a problem in the code above, it is problematic in functions to which an undefined variable is passed.
x != null also covers x != undefined so the converted code should not be entering the if statement if x is undefined.
See more in this answer
Related
i have replaced my code with x, y, and z.
At the moment my code runs like this but i have a lot of lines to check x. is there a way of instead of using this
if x != y && x != z
using something like this instead
if x != y,z
sorry everyone I should have explained it better. Here is a piece of the updated code using contains which was very helpful thank you!
if contactCreation.checkAllowed(newContact.contactDetails){
if{contactCreation.blockedCreation.contains(newContact.contactDetails){
watchList.addAttempt(newContact.contactDetails)
}
}else{
newAccount.showReasons()
}
I guess my question was more is there a way to check if x is y and if x is z but not if x is y + z. basically I wanted to call
if x != y && x != z && x != d && != e
without having to get x every-time in one if statement.
so something like this
if x != y && != z && != d && != e
thanks for all the help!
A slightly shorter version is to add an overload to the pattern matching operator (~=):
func ~=<T: Equatable>(pattern: [T], value: T) -> Bool {
return pattern.contains(value)
}
if !([y,z] ~= x) {
}
If that expression looks a little unwieldy and you write it a lot, you can define your own operator:
infix operator !!= : ComparisonPrecedence
func !!=<T: Equatable>(lhs: T, rhs: [T]) -> Bool {
return !rhs.contains(lhs)
}
if x !!= [y,z] {
}
I wouldn't suggest this with what you've shown for your code, but you could use tuples:
let testCase:(String,String) = ("hello","world")
if x != testCase {
x = ("did not", "match")
}
You can rewrite this condition using an array aggregate and contains method, like this:
if ![y, z].contains(x) {
...
}
This does not help much when x is a simple variable, but it lets you remove some code duplication when x is a complex expression.
This question already has answers here:
How to compare one value against multiple values - Swift
(8 answers)
Closed 6 years ago.
I have a situation where I have to make a check like this:
if foo == a || foo == b || foo == c {
//do stuff
}
is there any way to chain these operands into something smaller like IDK
foo == a||b||c
Try this:
if [a, b, c].contains(foo) {
//do stuff
}
I'm very confused even after looking through similar questions of what the(!) operator does when it is prefixed on a variable or other object in if statements, functions, etc?
Example:
mutating func add(value: T)
{
if !contains(items, value)
{
items.append(value)
}
}
the exclamation mark ! is used for two purposes. When you see it appear at the beginning of an object, such as in !contains(items, values), it means "NOT". For example...
let x = 10
let y = 5
if x == y {
print("x is equal to y")
} else if x != y {
print("x is NOT equal to y")
}
The above code will print => "x is NOT equal to y" .
The logical NOT (!) operator can be used to reverse boolean values. For example...
var falseBoolValue = false
falseBoolValue = !falseBoolValue
print(falseBoolValue)
The above code will print => "true"
In addition to the usage as the logical NOT operator, the exclamation mark is also used to implicitly unwrap optional values. Whenever you see the exclamation mark appear at the end of an object name, such as in someVariable!, it is being used to implicitly unwrap an optional value. Read about optionals to gain a better understanding of how ! is used with optional values.
it is NOT prefix there. Meaning your if looks for "items NOT containing value".
I have an if statement with a variable, however the if statement does not work correctly and I am guessing it is because of an optional value on the variable.
The statement goes something like this
If (x == 6) {
}
x does = 6 but I cannot get the if statement to work.
When I do a "print x", the result is
Optional("6")
So I know the number is 6 but it seems that the optional value is making this if statement not work. I cannot get this unwrapped so I'm looking for another option.
See the double quotes? It means that x is String type not Int. You can do this way to make be more standard
if let x = x where x == "6" {
}
If you are getting:
Optional("6")
it means that the value is actually a string and not an int. If it was an int you would get:
Optional(6)
To double check you can try:
if x == "6"
{
}
I hope that helps.
How have you defined x
You’ll not get an optional if it is like
let x = 6 \\or var x = 6
if x == 6 {
print(x)
}
Will return you 6 not Optional("6")
I am Scala beginner. I could not understand the Scala expression below :
x = if(true) x+1 else x-1
Well the initial value of x is 3, after executing the above expression it becomes 4.
I am not able to understand if(true), what is it actually evalutating ?
if(true) x else y will always execute the if branch because the condition is true. Thus, the result of your if expression will be always x + 1.
With a nod to #TillRohrmann, for us Scala newbies, things are sometimes not as clear as they are to seasoned programmers. Here is my attempt at detail, not just for you but for others who might have a similar question in future.
x = if (true) x+1 else x-1
Think of your expression as
x = function_if ( argument=true ) {
if (argument = true ){
return x+1
}else{
return x-1
}
}
Since the argument is always true, the answer is always x+1.
Hope this helps!