Xcode 9 beta 3 - Swift 3.2: comparing optional - xcode9-beta

Project which compiles fine in Xcode 8.3.2 shows many compilation errors around comparing optional/non optional value with ==
What I have found is an older proposal for removing coparison <> for the same:
https://github.com/apple/swift-evolution/blob/master/proposals/0121-remove-optional-comparison-operators.md
So now in Xcode 9, I can not even compare two optionals:
let xxx: String? = "A"
let yyy: String? = "B"
if xxx == yyy { //ERROR: Ambiguous use of operator '=='
}
Could someone point me to a resource where this would be explained?
Or is it just a temporary bug?
Thanks

Using XCode 9 beta (9M136h) and Swift 4, your instruction compiles.
Variants of == and != which accept optional operands are still useful, and their results unsurprising, so they will remain.
Remove the versions of <, <=, >, and >= which accept optional operands.
So your instruction should works either in Swift 3.2 or Swift 4.
Check the swift version you are using in the build settings -> Swift Language Version.

Related

How to assert size of `usize` to drop support for incompatible platforms?

Problem
I've just caught my self writing dynamic assertion that depends on a constant usize::MAX.
I wrote:
u128::try_from(letters.len())
.expect("No suppor for platform with `max pointer value` >= 2**128.")
where letters.len() is an instance of usize.
Instead, I'd like my code to fail to compile on so rare (if existing) platforms with "pointers size >= 2**128".
I already know/read:
I've read a similar question (suggested by community).
It shows methods to assert concrete size of pointer.
eg. #[cfg(not(target_pointer_width = "64"))]
I want my code to be very flexible. It's enough for usize::MAX<=u128::MAX && usize::MIN>=u128::MIN to be true.
Small reflection (maybe solution)
After some thinking, I concoct a solution that works quite good, both in my head & on the currently tested Debian with x64 architecture and Rust 1.60.
Code: const _:()= assert!(usize::BITS<=u128::BITS);
Do you know any cleaner & edge-case-proof resolution?
assert!() is fine. assert!() is only available in const contexts since Rust 1.57.0, so if you need to support older versions you can use the static_assertions crate. The idea is to replace assert!() with the following constant expression:
const _: [(); 0 - (!(usize::BITS <= u128::BITS)) as usize] = [];
Now, if the expression evaluates to true, its negation evaluates to false, and false as usize is zero. So we're declaring a zero-length array and everything is fine.
However, if the expression evaluates to false, its negation is true, and true as usize == 1. 0usize - 1usize overflows - and that is an error.
The downside is that it generates a pretty obscure error message:
error[E0080]: evaluation of constant value failed
--> src/lib.rs:1:15
|
1 | const _: [(); 0 - (!(usize::BITS > u128::BITS)) as usize] = [];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
(I swapped the condition so it will be false).

How to safely cast the result of readLine() to prevent a Type Mismatch using Kotlin

Many Kotlin tutorials I have watched / read have this line of code:
var number = Integer.valueOf(readLine())
And while it clearly worked before, it is now throwing a compiler error while using Android studio and Kotlin version 1.3.50.
It indicates a type mismatch where the found is String? and the required is String.
Granted, I understand why this is happening, I get that a user could pass null or empty values in via the console and therefore it needs to have the optional null declaration, but I would like to understand how to fix the compiler error and keep similar code without changing too much.
While I can use both of these lines of code:
var number = Integer.valueOf(readLine()!!)
and
var number = Integer.valueOf(readLine() as String)
I believe those can throw different exceptions as outlined here
I know I am able to 'fix' this problem by using this code:
var number : String? = readLine();
if(number == null){
number = "0"
}
val number2 = Integer.valueOf(number);
But it seems horribly inefficient. Is there a shorter way to do this using native Kotlin code?
If we simply call toInt() on the result from readLine(), we will get an exception if the value provided isn't an actual Integer. In order to avoid an exception, we can use toIntOrNull() from the Kotlin Standard Library.
val x= readLine()?.toIntOrNull() ?: 0
In this case, we read the line (as a String?) and if it is non-null, call toIntOrNull() on it. If that is non-null, we have our answer. Otherwise, we use 0 as the default.
Even though I am primarily a Swift developer, this is a very similar concept. In Swift it is called a nil-coalescing operator, but apparently in Kotlin it is called the Elvis Operator (uh-huh).
The docs are here So your code would look like this:
var num : String = readLine() ?: "0";
If the value before the Elvis operator ?: is not null, it uses that, otherwise it uses the second default value you provide.

string value in erlang

I am a newbie in erlang. I have to maintain and implement new feature of an an backend project built in erlang. I have strong background in c/c++, php, ruby, python and javascript. I have had problem in implementing a feature to remove some of the prefixes from list of phone number
-module(reminder_group).
-export([address_with_no_prefix/1]).
-define(PREFIXES, [
'855',
'+855',
'0',
'+0',
'+'
]).
address_with_no_prefix(Address) ->
address_with_no_prefix(Address, ?PREFIXES).
address_with_no_prefix(Address, []) -> Address;
address_with_no_prefix(Address, Prefixes) ->
[Prefix|H] = Prefixes,
Length = string:len(Prefix),
AddressPrefix = string:substr(Address, 1, Length),
if
Prefix == AddressPrefix ->
string:substr(Address, Length);
true ->
address_with_no_prefix(Address, H)
end.
After compiling I run
reminder_group:address_with_no_prefix("0123445")
I got the following error:
1> reminder_group:address_with_no_prefix("0123445").
** exception error: bad argument
in function length/1
called as length('855')
in call from string:len/1 (string.erl, line 66)
in call from reminder_group:address_with_no_prefix/2 (src/models/reminder_group.erl, line 34)
It seems like problem of Length = string:len(Prefix) however I test to run
string:len("855").
it works fine with the result of 3. Is there anything wrong with my list of string value ?
Please help.
You're using single quotes (rather than double quotes) to denote your prefixes. I assume you meant for these to be strings, not atoms (hence applying string:len)?
Your test worked because you correctly used double quotes to construct a string literal.

string1.Equals(string2) in if statement - no such member as Equals and expression must be BOOLEAN

I am trying to make a if statement where I want to compare 2 strings, whether they are equal or not in condition.
This is what I have
if vysledok.Equals(meno) then
Application.MessageBox('Zadane meno existuje, zadajte prosím iné meno','DUPLICITNÝ UŽÍVATEĽ',0)
else
...
However vysledok.Equals(meno) is underlined and it says this:
'string' does not contain a member named 'Equals' at line ...
Type of expression must be BOOLEAN at line ...
I have to mention that I am new to delphi :)
Thank you for advice
In modern Delphi, the helper for the string type, defined in SysUtils, provides an Equals method. So, in XE3 or later, if you use SysUtils, your code will compile. From which we can surmise that you are using an older version of Delphi, or have not used SysUtils.
In older versions of Delphi you compare strings using the equality operator:
if vysledok = meno then
In fact, the implementation of the Delphi string helper Equals method does nothing more than compare using this equality operator.
Should you want a case insensitive comparison use SameText():
if SameText(vysledok, meno) then
This should work too:
if AnsiUpperCase(vysledok) = AnsiUpperCase(meno) then

Is it possible to lookahead in ANTLR4 without actually matching a token?

I am writing a compiler by translating JavaCC to ANTLR4 and one of the rules involve passing parameters and getting return values from it.
I have to do something like the following for a rule 'term':
Term term(ReadOptions options, int priority):
{
int p = options.operatorSet.getNextLevel(priority);
Term t;
}
{
(
LOOKAHEAD({p==0})
t = simpleTerm(options)
|
LOOKAHEAD(<NAME_TOKEN>,{priority==1201 && is1201Separator(2)})
t = name()
|
t = operatorTerm(options, p)
)
{return t;}
}
The problem is that how do I match sub-rules on the basis of the value of 'p'. In the previous versions of ANTLR I could have used => and my problem would have solved but what do I do in ANTLR4 ?
The => operator in previous versions of ANTLR is no longer necessary in ANTLR 4.
ANTLR 4 does not support syntactic predicates because its lookahead algorithm fully supports infinite lookahead. If you used the form (x) => y previously, in ANTLR 4 you can simply use y.
Semantic predicates are still supported, but in ANTLR 4 all semantic predicates work like gated semantic predicates in ANTLR 3. If you used the form {x}? => y previously, then in ANTLR 4 you can simply use {x}? y.

Resources