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.

scope test_leftArrowScript_objectScope

set flag OurSyncCollections.

!include once #1034831 // Gazelle 22 Function Include for Testing

sclass #Class1 {
  S hello(S x) { ret "hello " + x; }
}

svoid test_leftArrowScript_objectScope(LASMultiClassLoader cl default new LASMultiClassLoader(mc())) {
  // Accessing an instance field
  
  S classPrefix = "scriptClasses.";
  embedded O runScript(S code) {  
    var parser = enableScaffolding(new GazelleV_LeftArrowScriptParser()
      .classDefPrefix(classPrefix)
      .lasClassLoader(cl));
    ret leftArrowVerbose(parser, code);
  }
  
  // Access instance field without "this"
  
  assertEqualsVerbose(10, runScript([[
    class X {
      i: int <- 5
      
      def j { i plus i }
    }
    
    new X, j
  ]]));
  
  // Access superclass field without "this"
  
  assertEqualsVerbose(10, runScript([[
    class X {
      i: int <- 5
    }
    
    class Y extends X {
      def j { i plus i }
    }
    
    new Y, j
  ]]));
  
  // Make sure locally declared variables shadow fields
  assertEqualsVerbose(14, runScript([[
    class X {
      i: int <- 5
      
      def j { i: int <- 7; i plus i }
    }
    
    new X, j
  ]]));
  
  // Assign instance field without "this"
  
  assertEqualsVerbose(10, runScript([[
    class X {
      i: int
      
      def j { i <- 10; this }
    }
    
    new X, j, i
  ]]));
  
  // Assign instance field in superclass without "this"
  
  assertEqualsVerbose(10, runScript([[
    class X { i: int }
    
    class Y extends X {
      def j { i <- 10; this }
    }
    
    new Y, j, i
  ]]));
  
  // Call instance method without "this"
  
  assertEqualsVerbose(2, runScript([[
    class X {
      i: int <- 1
      
      def yo { i <- i plus 1 }
      def j { yo; i }
    }
    
    new X, j
  ]]));
  
  // Call instance method without "this" followed by "<"
  
  assertEqualsVerbose(6, runScript([[
    class X {
      def yo n  { 1 plus n }
      def j { yo < 5 }
    }
    
    new X, j
  ]]));
  
  // Special case: field & method with same name
  //   (refers to the field because it's inside an expression)

  assertEqualsVerbose(ll("o", "o"), runScript([[  
    class X {
      bla: S <- "o"
      def bla { "x" }
      def y { ll bla bla }
    }
    new X, y
  ]]));
  
  // Call superclass method without "this"
  
  assertEqualsVerbose(2, runScript([[
    class X {
      i: int <- 1
      
      def yo { i <- i plus 1 }
    }
    
    class Y extends X {
      def j { yo; i }
    }
    
    new Y, j
  ]]));
  
  // Same with arguments and return value
  
  assertEqualsVerbose(6, runScript([[
    class X {
      i: int <- 1
      
      def yo x { i <- i plus x; this }
      def j { yo 5, i }
    }
    
    new X, j
  ]]));
  
  // Same with "<"
  
  assertEqualsVerbose(6, runScript([[
    class X {
      i: int <- 1
      
      def yo x { i <- i plus x; this }
      def j { (yo < 5) i }
    }
    
    new X, j
  ]]));
  
  // Java superclass method with "<"
  
  assertEqualsVerbose("hello world", runScript(replaceIdentifiers([[
    class X extends Class1 {
      def yo { hello "world" }
    }
    
    new X, yo
  ]], "Class1", className(Class1))));
  
  // Method shadows global function (TODO)
  
  /*assertEqualsVerbose(6, runScript([[
    class X {
    }
    
    new X, j
  ]]));*/
  
  // Auto-"this" in initializer
  
  assertEqualsVerbose(6, runScript([[
    class X {
      i: int <- 5
      
      initializer {
        i <- i plus 1
      }
    }
    
    new X, i
  ]]));
  
  // Declare a local variable shadowing an instance field
  // with "var"
  
  assertEqualsVerbose(3, runScript([[
    class X {
      i: int <- 3
      
      def j { var i <- 5; i <- i plus 1; this }
    }
    
    new X, j, i
  ]]));
  
}

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: 170 / 329
Version history: 19 change(s)
Referenced in: [show references]