-
Notifications
You must be signed in to change notification settings - Fork 13
Foundations/argumentsの翻訳 #285
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
base: main
Are you sure you want to change the base?
Changes from all commits
5ec5cea
6c86a5e
bf4ddf7
1bd37fe
8ef4d31
c2a9e26
bb15c21
2a0082f
6f52f6d
8a66702
444a6ec
d1b4c67
c4aa95f
1994b3f
40ac405
357e9da
83e158d
b6427e1
11e3c28
0ab3efd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,13 +9,11 @@ use crate::foundations::{ | |||||
cast, func, repr, scope, ty, Array, Dict, FromValue, IntoValue, Repr, Str, Value, | ||||||
}; | ||||||
|
||||||
/// Captured arguments to a function. | ||||||
/// 関数にキャプチャーされた引数。 | ||||||
/// | ||||||
/// # Argument Sinks | ||||||
/// Like built-in functions, custom functions can also take a variable number of | ||||||
/// arguments. You can specify an _argument sink_ which collects all excess | ||||||
/// arguments as `..sink`. The resulting `sink` value is of the `arguments` | ||||||
/// type. It exposes methods to access the positional and named arguments. | ||||||
/// # 引数シンク | ||||||
/// 組み込み関数と同様に、カスタム関数も可変個の引数を受け取れます。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// 余分にある引数をすべてまとめて受け取る _引数シンク_(キッチンシンクのようにさまざまなものが流れ込む先)は、`..sink`の形で指定できます。このとき生成される`sink`の値は`arguments`型になります。この型は、位置引数と名前付き引数の両方にアクセスするためのメソッドを提供しています。 | ||||||
/// | ||||||
/// ```example | ||||||
/// #let format(title, ..authors) = { | ||||||
|
@@ -29,9 +27,8 @@ use crate::foundations::{ | |||||
/// #format("ArtosFlow", "Jane", "Joe") | ||||||
/// ``` | ||||||
/// | ||||||
/// # Spreading | ||||||
/// Inversely to an argument sink, you can _spread_ arguments, arrays and | ||||||
/// dictionaries into a function call with the `..spread` operator: | ||||||
/// # 引数の展開 | ||||||
/// 引数シンクとは逆に、`..spread`演算子を使うと、関数呼び出しにおいて引数や配列、辞書を展開して渡すことができます。 | ||||||
/// | ||||||
/// ```example | ||||||
/// #let array = (2, 3, 5) | ||||||
|
@@ -43,15 +40,14 @@ use crate::foundations::{ | |||||
#[derive(Clone, Hash)] | ||||||
#[allow(clippy::derived_hash_with_manual_eq)] | ||||||
pub struct Args { | ||||||
/// The callsite span for the function. This is not the span of the argument | ||||||
/// list itself, but of the whole function call. | ||||||
/// 関数呼び出し箇所のスパン。これは引数リスト自体のスパンではなく、関数呼び出し全体のものです。 | ||||||
pub span: Span, | ||||||
/// The positional and named arguments. | ||||||
pub items: EcoVec<Arg>, | ||||||
} | ||||||
|
||||||
impl Args { | ||||||
/// Create positional arguments from a span and values. | ||||||
/// スパンと値から位置引数を作成します。 | ||||||
pub fn new<T: IntoValue>(span: Span, values: impl IntoIterator<Item = T>) -> Self { | ||||||
let items = values | ||||||
.into_iter() | ||||||
|
@@ -64,20 +60,20 @@ impl Args { | |||||
Self { span, items } | ||||||
} | ||||||
|
||||||
/// Attach a span to these arguments if they don't already have one. | ||||||
/// 引数にスパンがアタッチされていない場合はアタッチします。 | ||||||
pub fn spanned(mut self, span: Span) -> Self { | ||||||
if self.span.is_detached() { | ||||||
self.span = span; | ||||||
} | ||||||
self | ||||||
} | ||||||
|
||||||
/// Returns the number of remaining positional arguments. | ||||||
/// 残りの位置引数の個数を返します。 | ||||||
pub fn remaining(&self) -> usize { | ||||||
self.items.iter().filter(|slot| slot.name.is_none()).count() | ||||||
} | ||||||
|
||||||
/// Insert a positional argument at a specific index. | ||||||
/// 指定したインデックスに位置引数を挿入します。 | ||||||
pub fn insert(&mut self, index: usize, span: Span, value: Value) { | ||||||
self.items.insert( | ||||||
index, | ||||||
|
@@ -89,7 +85,7 @@ impl Args { | |||||
) | ||||||
} | ||||||
|
||||||
/// Push a positional argument. | ||||||
/// 位置引数をプッシュします。 | ||||||
pub fn push(&mut self, span: Span, value: Value) { | ||||||
self.items.push(Arg { | ||||||
span: self.span, | ||||||
|
@@ -98,7 +94,7 @@ impl Args { | |||||
}) | ||||||
} | ||||||
|
||||||
/// Consume and cast the first positional argument if there is one. | ||||||
/// 最初の位置引数がある場合、それを取り出してキャストします。 | ||||||
pub fn eat<T>(&mut self) -> SourceResult<Option<T>> | ||||||
where | ||||||
T: FromValue<Spanned<Value>>, | ||||||
|
@@ -113,7 +109,7 @@ impl Args { | |||||
Ok(None) | ||||||
} | ||||||
|
||||||
/// Consume n positional arguments if possible. | ||||||
/// 可能ならn個数の位置引数を取り出します。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
pub fn consume(&mut self, n: usize) -> SourceResult<Vec<Arg>> { | ||||||
let mut list = vec![]; | ||||||
|
||||||
|
@@ -133,10 +129,9 @@ impl Args { | |||||
Ok(list) | ||||||
} | ||||||
|
||||||
/// Consume and cast the first positional argument. | ||||||
/// 最初の位置引数を取り出してキャストする。 | ||||||
ultimatile marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
/// | ||||||
/// Returns a `missing argument: {what}` error if no positional argument is | ||||||
/// left. | ||||||
/// 位置引数が残っていなければ、`missing argument: {what}`エラーを返します。 | ||||||
pub fn expect<T>(&mut self, what: &str) -> SourceResult<T> | ||||||
where | ||||||
T: FromValue<Spanned<Value>>, | ||||||
|
@@ -147,7 +142,7 @@ impl Args { | |||||
} | ||||||
} | ||||||
|
||||||
/// The error message for missing arguments. | ||||||
/// 引数が足りない場合のエラーメッセージ。 | ||||||
fn missing_argument(&self, what: &str) -> SourceDiagnostic { | ||||||
for item in &self.items { | ||||||
let Some(name) = item.name.as_deref() else { continue }; | ||||||
|
@@ -295,9 +290,9 @@ impl Args { | |||||
|
||||||
#[scope] | ||||||
impl Args { | ||||||
/// Construct spreadable arguments in place. | ||||||
/// 展開可能な引数をその場で生成します。 | ||||||
/// | ||||||
/// This function behaves like `{let args(..sink) = sink}`. | ||||||
/// この関数は、`{let args(..sink) = sink}`のように動作します。 | ||||||
/// | ||||||
/// ```example | ||||||
/// #let args = arguments(stroke: red, inset: 1em, [Body]) | ||||||
|
@@ -306,27 +301,23 @@ impl Args { | |||||
#[func(constructor)] | ||||||
pub fn construct( | ||||||
args: &mut Args, | ||||||
/// The arguments to construct. | ||||||
/// 作成する引数。 | ||||||
#[external] | ||||||
#[variadic] | ||||||
arguments: Vec<Value>, | ||||||
) -> Args { | ||||||
args.take() | ||||||
} | ||||||
|
||||||
/// Returns the positional argument at the specified index, or the named | ||||||
/// argument with the specified name. | ||||||
/// 指定したインデックスの位置引数、または指定した名前の名前付き引数を返します。 | ||||||
/// | ||||||
/// If the key is an [integer]($int), this is equivalent to first calling | ||||||
/// [`pos`]($arguments.pos) and then [`array.at`]. If it is a [string]($str), | ||||||
/// this is equivalent to first calling [`named`]($arguments.named) and then | ||||||
/// [`dictionary.at`]. | ||||||
/// キーが[整数型]($int)の場合、それはまず[`pos`]($arguments.pos)メソッドを呼んでから、次に[`array.at`]を呼ぶのと同等です。キーが[文字列型]($str)である場合、まず[`named`]($arguments.named)メソッドを呼び、次に[`dictionary.at`]を呼ぶのと同等です。 | ||||||
#[func] | ||||||
pub fn at( | ||||||
&self, | ||||||
/// The index or name of the argument to get. | ||||||
/// 取得する引数のインデックスまたは名前。 | ||||||
key: ArgumentKey, | ||||||
/// A default value to return if the key is invalid. | ||||||
/// キーが無効な場合に返すデフォルト値。 | ||||||
#[named] | ||||||
default: Option<Value>, | ||||||
) -> StrResult<Value> { | ||||||
|
@@ -336,7 +327,7 @@ impl Args { | |||||
.ok_or_else(|| missing_key_no_default(key)) | ||||||
} | ||||||
|
||||||
/// Returns the captured positional arguments as an array. | ||||||
/// キャプチャーした位置引数を配列の形で返します。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
#[func(name = "pos", title = "Positional")] | ||||||
pub fn to_pos(&self) -> Array { | ||||||
self.items | ||||||
|
@@ -346,7 +337,7 @@ impl Args { | |||||
.collect() | ||||||
} | ||||||
|
||||||
/// Returns the captured named arguments as a dictionary. | ||||||
/// キャプチャーした名前付き引数を辞書の形で返します。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
#[func(name = "named")] | ||||||
pub fn to_named(&self) -> Dict { | ||||||
self.items | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
キャプチャーは私が指摘してそのように変更してもらったのですが、キャプチャーだと不適切でしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以前の議論を見落としていました。
他のテクニカルタームと比べると「キャプチャー」は日本語の技術文書において浸透度が低く感じたため、和訳する修正提案をさせていただきましたが、あまり強い自信はありません。
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
キャプチャーされた関数外の変数は
is_mutating_method
を除いて関数内で変更できないので、通常の変数の渡し方とは違いを意識する必要はあると思いますが、「関数にキャプチャーされた引数。」でどれくらい伝わるかは微妙な気がしています。「キャプチャ(ー)」は一般的な機能・用語なので個人的にはそこまで違和感はないです。相対的にマイナーなタームと言われるとそうかもしれないです。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gomazarashi @kimushun1101 こちらの件について意見があれば伺いたいです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここに関しては同じ感想をいだきました。私の界隈でもメジャーではありません。
これは
..sink
で関数が可変長の引数を受け取る形式のことを指していますかね?is_mutating_method
があまりわかっていないのですが、なにか違いがあるんだろうなぁくらいに雰囲気で感じている程度の理解度です。そのために「渡す」という訳を使わないというのはわかります。一方でこれを伝えたいため「キャプチャー」という訳にするというのは、目的に一致していない印象です。両者を取るという意味では、たとえば、「取り込む」や「保持する」などの別の言葉に置き換えるという案もありそうです。
いずれにしても個人的にはそこまで強いこだわりはありません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_mutating_method
は細かい例外なので一旦忘れても良いと思います(入力された配列にpop
は可能、が例です)が、言いたいことは関数は純粋関数なので入力値に対して変更を加える副作用は(原則)許されないという話です。キャプチャー渡しにすると入力値に変更ができなくて純粋性を保証する機能の一つになっているという感じです。foundations/functionで純粋関数に関する説明はあるので、無理に毎回キャプチャー渡しを強調する必要もない気がします。ただわざわざfoundations/argumentsを読みにきた読者に対して隠蔽して良い内容なのかという気持ちもあります。
キャプチャーの機能そのものは一般的で現代的な言語は基本的に実装されている一方で、キャプチャーというテクニカルタームがあまり一般的ではないのは、おそらくC++(だけ?)がユーザーにどうキャプチャーするかの選択の余地があり、その存在が意識されるのだと思います。Typstにおいては原則キャプチャー渡しになるので、ユーザーは意識する必要はないという観点に立つと「渡された」に戻すのが妥当かと思いました。
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
個人的には「渡された」のほうがしっくり来るように思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
みなさまありがとうございました。
@sbtseiji 「キャプチャー」は使わない方針になったので、申し訳ないですが「渡された」に戻してもらえますか?