#83Allow function expressions to capture scope

  • avatar~lucsoftcommented · 2 years ago
    Author

    User Story

    Database Developer

    As a Library developer for a database client i want allow users to create transactions blocks where they can run scoped actions based on the created transaction

    fn main() {
         let db = createDatabase();
         startTransaction(db, () -> {
               increment("deleted-items")      
               delete("item:5")
         });
         startTransaction(db, () -> {
               // also explicit this to get the current context
               this.increment("user:lepichu:inserted-items")      
               this.set("item:1", "mouse")
         });
    }
    
    
    fn startTransaction(db: Database, runBlock: TransactionBlock.() -> void) {
         val transaction = createTransaction(db);
         runBlock(transaction); // TODO: Handle rollbackTransaction(db);
         commitTransaction(db, transaction);
    })
    

    Frontend Developer

    As a Frontend developer i want to composability of components and layout that should be designed declarative.

    fn InvitedUserList() {
         let userList = query.invitedUsers();
          
         return Task(.{ 
              task: userList,
              loading: () -> {
                   Flexbox(() -> {
                        setGap("5px")
                        LoadingSpinner();
                        Label("Loading");
                   });
              },
              loaded: (data) -> {
                   List(data, (user) -> {
                        Flexbox(() -> {
                             ProfilePicture(user.picture);
                             Flexbox(() -> {
                                  setDirection("rows");
                                  Label(user.name);
                                  Label("Joined" + parseDate(user.joinDate))
                             });
                        });
                   });
              }
          });
    }
    fun <T> List(data: T[], callback: HTMLNode.() -> {
        // [...]
    })
    fun Flexbox(callback: HTMLNode.() -> {
        // [...]
    })
    

    References

    https://kotlinlang.org/docs/lambdas.html#function-literals-with-receiver


    Milestone: 0.3.0.

  • avatar~lucsoft changed the status to Todo · 2 years ago
  • avatar~clovercommented · 2 years ago
    Owner

    I'm renaming #46 to just "function expressions" and those will consider function comptime values, which do not capture values and thus stateless (and therefore, possible to represent as a Value).

    This proposal will consider runtime function values, which can capture state and have multiple instantiations.

    This also has the implication that the following holds

    fn hello() {
      let y = 1 + 2;
      // since `y` is known at comptime, this is a comptime function value
      return fn x { x + y };
    }
    
    fn world(y: int) {
      // since `y` is known at runtime, this is a runtime function value
      return fn x { x + y };
    }
    

    but of course, you can call world at compile time to get a function value:

    let add12 = comptime world(12);