Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next: 省略可能引数と初期値付き引数を追加 #475

Merged
merged 17 commits into from
Jun 8, 2024

Conversation

FineArchs
Copy link
Member

@FineArchs FineArchs commented Nov 18, 2023

一応破壊的変更なのでnextに入れます。

関数の引数定義において、各引数名に?を後置できるようにします。
?付きの引数は省略可能となり、省略された場合nullを格納します。
また、引数に=式を後置することで引数の初期値を設定できるようにします。
いずれでもない引数が省略された場合エラーを発生させます。

@FineArchs FineArchs changed the title next: ?付き引数を追加 next: 省略可能引数と初期値付き引数を追加 Nov 18, 2023
@salano-ym
Copy link
Member

デフォルト値の定義時に評価して使い回す方式ですかね
説明に書いておいた方がいいかも

@FineArchs
Copy link
Member Author

とりあえずsyntax.mdに追加しました

src/node.ts Outdated Show resolved Hide resolved
if (result.args[i]!.default) {
result.args[i]!.default = visitNode(result.args[i]!.default!, fn) as Ast.Fn['args'][number]['default'];
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この処理のiはindexではないと思います

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for-inなのでindexで間違いないと思いますが…

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for-inなので対象がオブジェクトだと勘違いしてました..
配列にもfor-inって使えるんですね
一般的な用法なんでしょうか

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一般的かどうかはわからないです

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for-ofの方が推奨されているようです

for(let value of array)
for(let index of array.keys())
for(let [index, value] of array.entries())

Copy link
Member Author

@FineArchs FineArchs Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for...inはプロトタイプから継承したものも列挙してしまうそうですね。
for(let index of array.keys())を使った形に修正しました。

@marihachi marihachi changed the title next: 省略可能引数と初期値付き引数を追加 Next: 省略可能引数と初期値付き引数を追加 Mar 9, 2024
@FineArchs FineArchs requested review from syuilo, marihachi and ikasoba June 4, 2024 07:35
Comment on lines 29 to 38
let optional = false;
if ((s.getKind() as TokenKind) === TokenKind.Question) {
s.next();
optional = true;
}
let defaultExpr;
if ((s.getKind() as TokenKind) === TokenKind.Eq) {
s.next();
defaultExpr = parseExpr(s, false);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@f(arg? = defaultValue) {}のような冗長な書き方は出来ないほうがいいかな~思いました。
?=を一つのシンボルとして扱って、デフォルトを指定する場合でも?を必須にするでもいいかもしれない?

Copy link
Member Author

@FineArchs FineArchs Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あー確かに 両方つくケースのことは失念していました

?=を一つのシンボルとして扱って、デフォルトを指定する場合でも?を必須にするでもいいかもしれない?

これちょっと迷いますね
JavaScriptのnull合体代入を連想させる書き方ですが、実際やってることはnull合体代入に似ていますしアリではありますね

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uzmoi
@f(arg? = defaultValue) {}を禁止する方向にしました。(迷った結果Typescriptに合わせました)

@FineArchs
Copy link
Member Author

条件を満たしたのでマージします。

@FineArchs FineArchs merged commit a3e4dad into aiscript-dev:aiscript-next Jun 8, 2024
4 checks passed
@FineArchs FineArchs deleted the args branch June 8, 2024 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants