----- Original Message -----
> From: Alexandre Oliveira <xinay...@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]
_______________________________________________
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to