File: classes.nice

package info (click to toggle)
nice 0.9.13-3.2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 7,292 kB
  • ctags: 6,893
  • sloc: java: 42,767; xml: 3,508; lisp: 1,084; sh: 742; makefile: 670; cpp: 21; awk: 3
file content (90 lines) | stat: -rw-r--r-- 2,195 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

package regtest.basic;

String pToString(?Point);

class Point 
{
  int x; int y;

  pToString() = "(x=" + this.x + ", y=" + this.y + ")";

  int twoArgs(int x, int y);
  twoArgs(x, y) = 0;

  // override a native method
  toString() = "Point";

  Point sum(alike);
  sum(other@Point) = new Point(x: this.x + other.x, y: this.y + other.y);
}

// check that a constructor is also available from functions
// which are resolved earlier than methods
// and even if the function appears before the class to be constructed
void test_new()
{
  Point p = new ColoredPoint(x:0,y:0,c:0);
}

class ColoredPoint extends Point
{
  int c;
  pToString() = "(x=" + this.x + ", y=" + this.y + ", c=" + this.c + ")";

  // override a native method
  toString() = "Colored Point: color=" + this.c;

  sum(other@ColoredPoint) = new ColoredPoint
    (x: this.x + other.x, y: this.y + other.y, c: (this.c + other.c) / 2);
}

/*
  A second, unrelated pToString is defined
  to test overloading resolution of method bodies with additional patterns
*/
String pToString(?String);
pToString(null(String)) = "Null string";
pToString(s@String) = "String";

<T> boolean isNull(T);
isNull(x) = false;
isNull(null) = true;

pToString(null(Point)) = "Origin";

boolean isSimplePoint(Point);
isSimplePoint(#Point) = true;  // takes precedence on point
isSimplePoint(@Point) = false; // matches all non-null point

void test_classes();
test_classes()
{
  println ("### Testing classes");
  println(pToString(new Point(x:0, y:0)));
  println(pToString(new ColoredPoint(x:0,y:0,c:0)));
  ?Point p = null;
  println(pToString(p));

  ?String s = null;
  println(pToString("titi") + ", " + pToString(s));

  println(isSimplePoint(new Point(x:0,y:0)));
  println(isSimplePoint(new ColoredPoint(x:0,y:0,c:0)));

  println(new Point(x:0,y:0).toString());
  println(new ColoredPoint(x:0,y:0,c:0).toString());
  Point pt = new ColoredPoint(x:0,y:0,c:0);
  println(pt.toString());

  let test = new TestFieldInitializers();
}

class TestFieldInitializers
{
  ?String[] s = new String[0];
}

// Local Variables:
// nice-xprogram: "$HOME/Nice/bin/nicec -e -r -d \"$HOME/Nice/classes\" --classpath=\"$HOME/Nice:$HOME/Nice/classes\""
// End: