Swift error: prefix/postfix '=' is reserved [duplicate] - ios

This question already has answers here:
Is this response from the compiler valid?
(3 answers)
Closed 8 years ago.
I am getting error message:
prefix/postfix '=' is reserved
for below simple in swift.
var c=0,a=2,b=4
c= a+b
any idea why I am getting this error?

Check this:
Is this response from the compiler valid?
Swift isn't entirely whitespace-agnostic like C... in particular, it uses whitespace to distinguish prefix from postfix operators (because ++i++ in C is a grammar oddity). But it's not ridiculously strict about whitespace like Python either.
P.S. So you have to add whitespace before =.

If I use a single space after variable "c" name, this error is get removed.
c = a+b

Related

Ruby: <<- operator [duplicate]

This question already has answers here:
What are <-- Ruby Strings called? And how do I insert variables in them?
(3 answers)
Closed 6 years ago.
I'm working on Rails. In my code base, I see a line that using Arel::SqlLiteral like this:
result = Arel::Nodes::SqlLiteral.new(<<-SQL
CASE WHEN condition1 THEN calculation1
WHEN condition2 THEN calculation2
WHEN condition3 THEN calculation3
ELSE default_calculation END
SQL)
I understand what this code piece do. The thing I don't understand is its grammar, at this point:
Arel::Nodes::SqlLiteral.new(<<-SQL
...
SQL
)
So in ruby, what is the grammar of <<- follow by name, and then at last block we call that name.
thanks
The keyword you're looking for is "Heredoc".
https://ruby-doc.org/core-2.2.0/doc/syntax/literals_rdoc.html#label-Here+Documents
It's mainly used to prettify large texts and common practice for shells/shellscripts. The marker on top indicates the beginning of a heredoc and the marker on bottom (which must not be indented unless you place a “-” before the opening marker) specifies the end.

What does ?* mean [duplicate]

This question already has answers here:
what is "?" in ruby
(3 answers)
Closed 7 years ago.
I am trying to understand what this means
variableName * ?*
I understand that VariableName is being multiplied with something, but what does ?* mean? Is this regex and does it mean that I'm appending '?' and anything that comes after it?
?c is not a regex, is the short syntax for '*'. That is, ?a is 'a', ?b is 'b' etc...
What is going on in your program is probably something like:
["ab","cd","ef"] * ?*
#=> "ab*cd*ef"

?: in Objective-C [duplicate]

This question already has answers here:
Is this ternary conditional ?: correct (Objective) C syntax?
(4 answers)
Closed 8 years ago.
In this iOS tutorial, there is a line of code with a ? followed by a :. In the context of the comment for the code, I thought it was some sort of ternary operation, however, that's obviously not the syntax for a ternary operator. Is there a name for what is happening in this code with the ?:?
// Initialize the list of weather items if it doesn't exist
NSMutableArray *array = self.xmlWeather[#"weather"] ?: [NSMutableArray array];
It's a GCC extension:
6.7 Conditionals with Omitted Operands
The middle operand in a conditional expression may be omitted. Then if the first operand is nonzero, its value is the value of the conditional expression.
Therefore, the expression
x ? : y
has the value of x if that is nonzero; otherwise, the value of y.
This example is perfectly equivalent to
x ? x : y
In this simple case, the ability to omit the middle operand is not especially useful. When it becomes useful is when the first operand does, or may (if it is a macro argument), contain a side effect. Then repeating the operand in the middle would perform the side effect twice. Omitting the middle operand uses the value already computed without the undesirable effects of recomputing it.

Regex with multiple conditions [duplicate]

This question already has an answer here:
Regular expression for password complexity
(1 answer)
Closed 8 years ago.
I'm trying to have the following rules for NSString validation using regular expression:
8 characters minimum length
at least 1 digit
at least 1 uppercase
at least 1 lowercase
I'm only able to do the following to get the first rule like this:
^[a-zA-Z0-9]{8,}$
Which if i understand correctly check for minimum 8 characters length with lower/uppercase and digit
Thank you
Use a lookahead for each assertion:
(?=.*\d)(?=.*[A-Z])(?=.*[a-z])^.{8,}$

Erlang Tuple to String [duplicate]

This question already has answers here:
Convert erlang terms to string, or decode erlang binary
(2 answers)
Closed 9 years ago.
Is there a way how to convert a tuple to a string ?
Consider I have the following list :
[{atom,5,program},{atom,5,receiving},{nil,5}]
I wish to convert this into the following string:
"{atom,5,program},{atom,5,receiving},{nil,5}"
I have tried using erlang:tuple_to_list on each element in the list, which returns
A = [atom,5,program]
Eventually, I can't concatenate that with "{" ++ A ++ "}"
Any ideas how I can turn that to a string ?
Term = [{atom,5,program},{atom,5,receiving},{nil,5}].
lists:flatten(io_lib:format("~p", [Term])).

Resources