Re: [Vala] How to check if string is empty



On 16/01/2017 17:50, Al Thomas wrote:
----- Original Message -----
From: Alexandre Oliveira <xinayder airmail cc>
Sent: Monday, 16 January 2017, 19:27
Subject: Re: [Vala] How to check if string is empty

I tried this in my method, even replacing 'string' for 'string?', but I
still get the same message in my terminal.


What is your suggestion to suppress these messages?

Don't suppress them, they are warnings from the runtime that things are wrong.
Ideally Vala would have warned you when the program was compiled, but
the valac switch --enable-experimental-non-null is not there yet.

On 16/01/2017 17:23, Guillaume Poirier-Morency wrote:

You should test for nullity before addressing the string.


    return str == null || str.length == 0;

If you expect the string to be potentially 'null', use the 'string?'
type instead.


This is sound advice. Works for me:

void main () {
string? a = null;
if (is_empty (a)) {
print ("string is empty\n");
}
a = "\0";
if (is_empty (a)) {
print ("string is empty\n");
}
}

bool is_empty(string? str) {
return str == null || str == "";
}


Really you should be thinking about avoiding nulls. As C.A.R.Hoare said "I call it my billion-dollar 
mistake. It was the invention of the null reference in 1965. This has led to innumerable errors, 
vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the 
last forty years." [https://en.wikipedia.org/wiki/Tony_Hoare#Apologies_and_retractions]


Thank you guys, finally solved the issue. Sorry for being a bit stupid
on it.

-- 
Alexandre Oliveira
  167F D82F 514A E8D1 2E9E
  C62D 1B63 9D4A 7E9D DA9D


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]