Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

207
LINES

< > BotCompany Repo | #1036500 // test_leftArrowScript_objectScope - access instance fields and methods without saying "this"

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (36762L) is out of date.

1  
scope test_leftArrowScript_objectScope
2  
3  
set flag OurSyncCollections.
4  
5  
!include once #1034831 // Gazelle 22 Function Include for Testing
6  
7  
sclass #Class1 {
8  
  S hello(S x) { ret "hello " + x; }
9  
}
10  
11  
svoid test_leftArrowScript_objectScope(LASMultiClassLoader cl default new LASMultiClassLoader(mc())) {
12  
  // Accessing an instance field
13  
  
14  
  S classPrefix = "scriptClasses.";
15  
  embedded O runScript(S code) {  
16  
    var parser = enableScaffolding(new GazelleV_LeftArrowScriptParser()
17  
      .classDefPrefix(classPrefix)
18  
      .lasClassLoader(cl));
19  
    ret leftArrowVerbose(parser, code);
20  
  }
21  
  
22  
  // Access instance field without "this"
23  
  
24  
  assertEqualsVerbose(10, runScript([[
25  
    class X {
26  
      i: int <- 5
27  
      
28  
      def j { i plus i }
29  
    }
30  
    
31  
    new X, j
32  
  ]]));
33  
  
34  
  // Access superclass field without "this"
35  
  
36  
  assertEqualsVerbose(10, runScript([[
37  
    class X {
38  
      i: int <- 5
39  
    }
40  
    
41  
    class Y extends X {
42  
      def j { i plus i }
43  
    }
44  
    
45  
    new Y, j
46  
  ]]));
47  
  
48  
  // Make sure locally declared variables shadow fields
49  
  assertEqualsVerbose(14, runScript([[
50  
    class X {
51  
      i: int <- 5
52  
      
53  
      def j { i: int <- 7; i plus i }
54  
    }
55  
    
56  
    new X, j
57  
  ]]));
58  
  
59  
  // Assign instance field without "this"
60  
  
61  
  assertEqualsVerbose(10, runScript([[
62  
    class X {
63  
      i: int
64  
      
65  
      def j { i <- 10; this }
66  
    }
67  
    
68  
    new X, j, i
69  
  ]]));
70  
  
71  
  // Assign instance field in superclass without "this"
72  
  
73  
  assertEqualsVerbose(10, runScript([[
74  
    class X { i: int }
75  
    
76  
    class Y extends X {
77  
      def j { i <- 10; this }
78  
    }
79  
    
80  
    new Y, j, i
81  
  ]]));
82  
  
83  
  // Call instance method without "this"
84  
  
85  
  assertEqualsVerbose(2, runScript([[
86  
    class X {
87  
      i: int <- 1
88  
      
89  
      def yo { i <- i plus 1 }
90  
      def j { yo; i }
91  
    }
92  
    
93  
    new X, j
94  
  ]]));
95  
  
96  
  // Call instance method without "this" followed by "<"
97  
  
98  
  assertEqualsVerbose(6, runScript([[
99  
    class X {
100  
      def yo n  { 1 plus n }
101  
      def j { yo < 5 }
102  
    }
103  
    
104  
    new X, j
105  
  ]]));
106  
  
107  
  // Special case: field & method with same name
108  
  //   (refers to the field because it's inside an expression)
109  
110  
  assertEqualsVerbose(ll("o", "o"), runScript([[  
111  
    class X {
112  
      bla: S <- "o"
113  
      def bla { "x" }
114  
      def y { ll bla bla }
115  
    }
116  
    new X, y
117  
  ]]));
118  
  
119  
  // Call superclass method without "this"
120  
  
121  
  assertEqualsVerbose(2, runScript([[
122  
    class X {
123  
      i: int <- 1
124  
      
125  
      def yo { i <- i plus 1 }
126  
    }
127  
    
128  
    class Y extends X {
129  
      def j { yo; i }
130  
    }
131  
    
132  
    new Y, j
133  
  ]]));
134  
  
135  
  // Same with arguments and return value
136  
  
137  
  assertEqualsVerbose(6, runScript([[
138  
    class X {
139  
      i: int <- 1
140  
      
141  
      def yo x { i <- i plus x; this }
142  
      def j { yo 5, i }
143  
    }
144  
    
145  
    new X, j
146  
  ]]));
147  
  
148  
  // Same with "<"
149  
  
150  
  assertEqualsVerbose(6, runScript([[
151  
    class X {
152  
      i: int <- 1
153  
      
154  
      def yo x { i <- i plus x; this }
155  
      def j { (yo < 5) i }
156  
    }
157  
    
158  
    new X, j
159  
  ]]));
160  
  
161  
  // Java superclass method with "<"
162  
  
163  
  assertEqualsVerbose("hello world", runScript(replaceIdentifiers([[
164  
    class X extends Class1 {
165  
      def yo { hello "world" }
166  
    }
167  
    
168  
    new X, yo
169  
  ]], "Class1", className(Class1))));
170  
  
171  
  // Method shadows global function (TODO)
172  
  
173  
  /*assertEqualsVerbose(6, runScript([[
174  
    class X {
175  
    }
176  
    
177  
    new X, j
178  
  ]]));*/
179  
  
180  
  // Auto-"this" in initializer
181  
  
182  
  assertEqualsVerbose(6, runScript([[
183  
    class X {
184  
      i: int <- 5
185  
      
186  
      initializer {
187  
        i <- i plus 1
188  
      }
189  
    }
190  
    
191  
    new X, i
192  
  ]]));
193  
  
194  
  // Declare a local variable shadowing an instance field
195  
  // with "var"
196  
  
197  
  assertEqualsVerbose(3, runScript([[
198  
    class X {
199  
      i: int <- 3
200  
      
201  
      def j { var i <- 5; i <- i plus 1; this }
202  
    }
203  
    
204  
    new X, j, i
205  
  ]]));
206  
  
207  
}

Author comment

Began life as a copy of #1035077

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): mowyntqkapby, mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036500
Snippet name: test_leftArrowScript_objectScope - access instance fields and methods without saying "this"
Eternal ID of this version: #1036500/20
Text MD5: a98da8fc774218c61261d9cdbf0953ac
Author: stefan
Category: javax / left arrow script
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-01-28 16:54:59
Source code size: 3960 bytes / 207 lines
Pitched / IR pitched: No / No
Views / Downloads: 102 / 244
Version history: 19 change(s)
Referenced in: [show references]