Re: [Vala] Ternary operator and nullable values in return statements



Dru Moore píše v Ne 28. 11. 2010 v 18:22 +0000:
Hi all,

Was surprised to see the following fail to compile with an "error:
Incompatible expressions" in Vala 0.10.0 (simplified test case to
illustrate):

public static int main (string[] args) {
  int? x = test (12);
  return 0;
}

public int? test (int number) {
  return (0 == number) ? null : 1;
}

Am I missing something obvious of misunderstanding something equally
clear?

Yep, 1 is not of a nullable type, so you have either null or some value
and Vala can't infer what they have in common (well, strictly speaking,
they have nothing). You need to cast it like this:
  (0 == number) ? null : (int?) 1

or
  int? i = 1;
  (0 == number) ? null : i

Attachment: signature.asc
Description: This is a digitally signed message part



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