0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ada のランデブー機構は、たぶん Alt-JS にあると良い

Last updated at Posted at 2025-05-06

プロうグラミング言語の機能についてあれこれと脈絡なく考えていてふと、Ada のランデブー機構の select accept - or delay を思い出して、それが JavaScript とか Alt-JS の言語にはないなというのが気になった。

Ada
select
    accept Task_A;
or
    delay 50ms;
    IO.print("Timeout occurred");
end select;

これが TypeScript では、どうやらこうなるらしい。自分は TypeScript には全く詳しくないので Copilot に変換してもらっただけなんだけど...。このコード量と可読性の違いは無視できないよね!

TypeScript
async function taskA(): Promise<void> {
    return new Promise((resolve) => {
        setTimeout(() => {
            console.log("Task_A accepted");
            resolve();
        }, Math.random() * 100); // 任意の時間で完了
    });
}

async function timeout(): Promise<void> {
    return new Promise((resolve) => {
        setTimeout(() => {
            console.log("Timeout occurred");
            resolve();
        }, 50);
    });
}

async function selectTask() {
    await Promise.race([taskA(), timeout()]);
}

selectTask();

『TypeScript は JavaScript の欠点を補うものとして設計されたわりには、非同期処理の仕組みが洗練されていないのは不思議だなと。TypeScript の設計者に Ada の知識がある人がいたらもっとエレガントな仕組みを導入していたのでしょうね。』

というと、Copilot は

『確かにそうですね。TypeScript は JavaScript の型安全性を強化することに主眼を置いて設計されましたが、非同期処理に関しては基本的に JavaScript の構造を踏襲 する形になっています。そのため、Promise や async/await を活用しながら処理を組み立てる必要があり、シンプルでエレガントというよりは、少し煩雑に感じる部分がありますね。』

と。

まあそれでも、やっぱり select accept - or delay があるほうが良いだろね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?