[vala] Support creation of arrays with nullable elements
- From: Luca Bruno <lucabru src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala] Support creation of arrays with nullable elements
- Date: Sat, 10 Dec 2011 19:11:32 +0000 (UTC)
commit bce3e8474a8238ff5a0bb697a0f94a54c2d959d3
Author: Aaron Andersen <aaron fosslib net>
Date: Sat Dec 10 19:58:18 2011 +0100
Support creation of arrays with nullable elements
Partially fixes bug 571486.
tests/basic-types/arrays.vala | 7 +++++++
vala/valaarraytype.vala | 4 ++++
vala/valaparser.vala | 12 ++++++++++++
3 files changed, 23 insertions(+), 0 deletions(-)
---
diff --git a/tests/basic-types/arrays.vala b/tests/basic-types/arrays.vala
index 708f246..5d4d0c4 100644
--- a/tests/basic-types/arrays.vala
+++ b/tests/basic-types/arrays.vala
@@ -41,6 +41,13 @@ void test_integer_array () {
// in expressions
assert (23 in a);
assert (!(-1 in a));
+
+ // nullable elements
+ int?[] d = new int?[2];
+ d[0] = 10;
+ d[1] = null;
+ assert (d[0] == 10);
+ assert (d[1] == null);
}
void test_string_array () {
diff --git a/vala/valaarraytype.vala b/vala/valaarraytype.vala
index 040df21..d69a623 100644
--- a/vala/valaarraytype.vala
+++ b/vala/valaarraytype.vala
@@ -188,6 +188,10 @@ public class Vala.ArrayType : ReferenceType {
return false;
}
+ if (element_type.nullable != target_array_type.element_type.nullable) {
+ return false;
+ }
+
if (element_type.compatible (target_array_type.element_type)
&& target_array_type.element_type.compatible (element_type)) {
return true;
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 45db404..76ea0b9 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -844,7 +844,12 @@ public class Vala.Parser : CodeVisitor {
var expr = parse_object_creation_expression (begin, member);
return expr;
} else {
+ bool is_pointer_type = false;
while (accept (TokenType.STAR)) {
+ is_pointer_type = true;
+ }
+ if (!is_pointer_type) {
+ accept (TokenType.INTERR);
}
if (accept (TokenType.OPEN_BRACKET)) {
rollback (begin);
@@ -898,8 +903,15 @@ public class Vala.Parser : CodeVisitor {
expect (TokenType.NEW);
var member = parse_member_name ();
DataType element_type = UnresolvedType.new_from_expression (member);
+ bool is_pointer_type = false;
while (accept (TokenType.STAR)) {
element_type = new PointerType (element_type, get_src (begin));
+ is_pointer_type = true;
+ }
+ if (!is_pointer_type) {
+ if (accept (TokenType.INTERR)) {
+ element_type.nullable = true;
+ }
}
expect (TokenType.OPEN_BRACKET);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]