2017-04-25 10 views
4

私は、方法の引数をselfに変更する方法を見つけようとしています。 GitHub commentによると:メソッドの `self`引数を解体することは可能ですか?

Per today's meeting, we have a different plan to make self arguments destructurable. With universal function-call syntax (UFCS #11938) there will not be any distinction between static methods and instance methods - they will both be 'associated functions'. At that point any function who's first argument is the self type will be callable with method syntax, and self , &self , and &mut self are just sugar for i.e. self: &Self , and destructuring on the self argument can be done as normal by not using the self-sugar.

私は、次のコードを書いたが、私はすべての3つの印刷機能を方法として使用することができるという点で期待どおりに動作しません。

struct Vector { 
    x: i32, 
    y: i32, 
    z: i32, 
} 

impl Vector { 
    fn print1(self: &Self) { 
     println!("{} {} {}", self.x, self.y, self.z); 
    } 

    // destructure self argument 
    fn print2(&Vector{x, y, z}: &Self) { 
     println!("{} {} {}", x, y, z); 
    } 

    // use another name for the first argument 
    fn print3(this: &Self) { 
     println!("{} {} {}", this.x, this.y, this.z); 
    } 
} 

fn main() { 
    let v = Vector{x: 1, y: 2, z: 3}; 

    Vector::print1(&v); // work 
    v.print1();   // work 
    Vector::print2(&v); // work 
    v.print2();   // not work 
    Vector::print3(&v); // work 
    v.print3();   // not work 
} 

print3()はちょうどメソッドの最初の引数にself以外の名前を使用することが可能であるかどうかをテストするために使用されました。

それは、このコンパイルエラーを与える:

error: no method named `print2` found for type `Vector` in the current scope 
    --> 1.rs:27:7 
    | 
27 |  v.print2();   // not work 
    |  ^^^^^^ 
    | 
    = note: found the following associated functions; to be used as methods, functions must have a `self` parameter 
note: candidate #1 is defined in an impl for the type `Vector` 
    --> 1.rs:12:5 
    | 
12 |  fn print2(&Vector{x, y, z}: &Self) { 
    | _____^ starting here... 
13 | |   println!("{} {} {}", x, y, z); 
14 | |  } 
    | |_____^ ...ending here 

error: no method named `print3` found for type `Vector` in the current scope 
    --> 1.rs:29:7 
    | 
29 |  v.print3();   // not work 
    |  ^^^^^^ 
    | 
    = note: found the following associated functions; to be used as methods, functions must have a `self` parameter 
note: candidate #1 is defined in an impl for the type `Vector` 
    --> 1.rs:16:5 
    | 
16 |  fn print3(this: &Self) { 
    | _____^ starting here... 
17 | |   println!("{} {} {}", this.x, this.y, this.z); 
18 | |  } 
    | |_____^ ...ending here 

print2()print3()Vectorの方法として識別されていないようです。

  1. selfメソッドの引数をどのように破壊するか?
  2. コメントによると、selfの名前はちょうど砂糖です。メソッドの最初の引数にself以外の名前を使用できますか?
+0

を。 'self'はキーワードです。 – kennytm

+0

@kennytm彼は「自己」は単なる砂糖であると言っているので、最初の議論の名前は「自己」である必要はないという意味ですか? – Laurence

+2

@Laurence:あなたが探している抜粋は2014年2月のもので、2015年5月15日にRust 1.0が出ました。物事が1.0まで大きく変わったので、Rustの歴史ではあまりにも遠すぎます。あなたの質問に基づいているコメントは単純に時代遅れです。 @MatthieuM。 –

答えて

8

これはもともとユニバーサル関数呼び出しで可能であることを意図したが、それはfn foo(bar: &Self)が突然方法が原因突然登場する新しいメソッドの呼び出しを破ることができるfn foo(self: &Self)、と等価になることを意味するので、後方互換性がありませんました。

あなたが関数本体に結合letと明示selfパラメータdestructureできthis github issue comment

に詳しい根拠:最初のパラメータはノートで述べたように、 `self`、名前を付ける必要があります

let &Vector { x, y, z } = self; 
関連する問題