1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 fun foo
do print
"obj"
22 redef fun foo
do print
"a"
28 redef fun foo
do print
"b"
32 redef fun foo
do print
self
35 fun callbacks_with_as_casts
(a
: A
, b
: B
) import B
::foo
, A
::foo
, B
as(A
), A
as(B
) `{
43 printf("Instance of A is not a B.\n");
50 fun callbacks_with_nullable_casts
(a
: A
, b
: nullable B
) import B
as not nullable, A
as nullable, A
as(nullable B
), B
::foo
`{
51 if (!nullable_B_is_a_B(b)) {
52 printf("Instance b is not a B (it is null)\n");
54 B bb = nullable_B_as_B(b);
58 if (!A_is_a_nullable_A(a)) {
59 printf("Instance a is not a nullable A\n");
61 if (!A_is_a_nullable_B(a)) {
62 printf("Instance a is not a nullable B\n");
66 fun callbacks_with_failed_checks
(a
: A
, b
: nullable B
) import B
as not nullable, B
as nullable, A
as(B
), B
as (A
) `{
68 printf("Instance of A is not a B.\n");
70 if (!nullable_B_is_a_B(b)) {
71 printf("Instance of B is null\n");
75 fun callbacks_with_primitives
(o
: Object, i
: Int, ni
: nullable Int) import Object as(Int), Int as(Object), nullable Int as(Int), Int::foo
, Object::foo
`{
79 if (!Object_is_a_Int(o))
80 printf("Instance o is not a Int\n");
82 int oo = Object_as_Int(o);
86 Object ii = Int_as_Object(i);
89 if (!nullable_Int_is_a_Int(ni))
90 printf("Instance ni is not a Int\n");
92 int nni = nullable_Int_as_Int(ni);
98 callbacks_with_as_casts
(new B
, new B
)
100 callbacks_with_as_casts
(new A
, new B
)
102 callbacks_with_nullable_casts
(new B
, new B
)
104 callbacks_with_nullable_casts
(new A
, null)
106 callbacks_with_failed_checks
(new A
, null)
108 callbacks_with_primitives
(1111, 2222, 3333)
110 callbacks_with_primitives
(new A
, 5555, null)