#ask How should we improve Motoko?

Cascading message replies cause delays that add up, that is true. But that is inherent in the message-based architecture of the IC, not something Motoko can change.

1 Like

My understanding is that composite queries are already in the local replica but we don’t have a direct way to access in Motoko yet. Maybe I misunderstand that concept, but I think this would make this kind of access easier…at least on the same subnet.

1 Like

Would it possible to add an optional (likely necessarily synchronous) callback for any declared shared method that would be “reasonably guaranteed to be enqueued/called” (except for in cases when a canister is out of cycles for instance) immediately after that method returns?

Something like the cleanup function that exists for React’s useEffect hook.

1 Like

What would it take to abstract way Iter and have an easier syntax when using for loops?

Being able to write something like this

for (let i = 0; i < 5; i++) {
// Do something
}

Of course, the traditional way using an Iter would still work. It would just be an abstraction, to get a similar feeling to other languages like Python/JavaScript.

Context: I’m asking this because during the Bootcamp I want students to be able to use for loops on their first day without having to introduce a more advanced concept like Iter.

2 Likes

@Seb, C-style for loops are among the most error-prone loop constructs in existence (and discouraged in many modern style guides). Motoko very intentionally does not include them. I don’t think using for (i in range(0, 5)) requires understanding the iterator protocol. For beginners not handicapped by prior Python/JS exposure, I would hope it’s actually easier. :wink:

8 Likes

As a worked example: https://m7sm4-2iaaa-aaaab-qabra-cai.ic0.app

Some of this syntax isn’t intuitive, but I’d think this was pretty simple for a beginner. Honestly, I’d forgotten that range was even in Iter.

Sometimes I wish base was just always there and I could just do base.iter.range or base.buffer or base.nat32.toNat. Maybe the compiler could get smart enough to back fill what is actually used?

Edit: I messed up the paste and the example is lost to history…it was basically


import {range} from "mo:base/Iter";

....and then later....

for(thisItem in range(1,5){
  x += thisItem;
};
2 Likes

would it be possible to add some kind of pipe operator syntax similar to Elixir? eg:

String.split(String.upcase("Elixir rocks"))
"Elixir rocks" |> String.upcase() |> String.split()
["ELIXIR", "ROCKS"]

https://elixirschool.com/en/lessons/basics/pipe_operator

1 Like

Wrote down some thoughts

6 Likes

I’m happy to share that we are getting started on interpolation! The language team also brought up the challenges with stringification, which we also want to tackle, but won’t be in the immediate scope.

5 Likes

Something similar came up in the context of cleaning up side effects (iirc specifically guaranteeing releasing a lock at “the end” of the function), or at least I was reminded by that when reading this article:

Maybe something that could be used to improve Motoko?