"参数太多"完美的功能

时间:2017-07-01 23:13:36

标签: methods types rust traits

我的代码类似于:

pub trait WorldImpl {
    fn new(size: (usize, usize), seed: u32) -> World;
    fn three() -> bool;
    fn other() -> bool;
    fn non_self_methods() -> bool;
}
pub type World = Vec<Vec<UnitOfSpace>>;
// I'm doing this because I want a SPECIAL version of Vec<Vec<UnitOfSpace>>, so I can treat it like a struct but have it be a normal type underneath.
impl WorldImpl for World {
    fn new(size: (usize, usize), seed: u32) -> World {
    // Code
        vec![/* vector stuff */]
    }
    // Implement other three methods
}
let w = World::new((120, 120), /* seed from UNIX_EPOCH stuff */);

我得到了这个错误,这显然是错误的:

error[E0061]: this function takes 0 parameters but 2 parameters were supplied
  --> src/main.rs:28:28
   |
28 |     let world = World::new((120 as usize, 120 as usize),
   |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 0 parameters

我在想两件事:

  1. 这不是惯用语,Rust从未打算以这种方式使用。在这种情况下,我需要知道如何真正做到这一点。

  2. 这是一个我错过的愚蠢错误。

  3. 当我在操场上尝试类似的代码时,它工作得很好,没有错误。我没有在其他地方找到任何关于此类错误的信息,所以我发现我只是使用错误的语言并不会感到惊讶。我对我的任何代码都没有特别的附件,所以请告诉我这个成语是什么!

2 个答案:

答案 0 :(得分:3)

你要做的事情并没有多大意义。您已将dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in设为Class Claim { var dbRef:FIRDatabaseReference! //create a reference to Firebase database `brevCustomer`, not the one from .plist file override func viewDidLoad() { super.viewDidLoad() let app = FIRApp(named: "brevCustomer") let dbRef = FIRDatabase.database(app: app!).reference().child("Users") startObservingDB() // observe the database for value changes } func startObservingDB() { //it crashes on the line below dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in //iterate over each user node child for user_child in snapshot.children { print(user_child)} }, withCancel: { (Error: Any) in print(Error) }) } // end of startObservingDB() }//end of Claim class class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Use Firebase library to configure APIs for the initial project with .plist file saved in Xcode FIRApp.configure() /** 1. create a Firebase options object to hold the configuration data for the second Firebase Project */ let secondaryOptions = FIROptions(googleAppID: "1:82424687545:ios:71df5d45218ad27", bundleID: "com.vivvdaplar.Brev", gcmSenderID: "8201647545", apiKey: "AIzaSyCNtyUf2T3UunH6-ci_WyvOqCl_RzXI", clientID: "8200687545-42vklp94reavi6li6bolhcraoofc6.apps.googleusercontent.com", trackingID: nil, androidClientID: nil, databaseURL: "https://brev-72e10.firebaseio.com", storageBucket: "com.vivvdaplar.Brev", deepLinkURLScheme: nil) // Configure the app FIRApp.configure(withName: "brevCustomer", options: secondaryOptions!) return true } } //end of AppDelegate 的类型别名,因此它们完全可以互换 - 您为其中一个添加的实现将适用于另一个,反之亦然。

如果您想以不同方式处理此类型,请将其换成newtype:

World

现在这是与Vec<Vec<UnitOfSpace>>不同的类型,但运行时开销为零。

您的实际错误是因为您已将struct World(Vec<Vec<UnitOfSpace>>); 方法添加到Vec<Vec<UnitOfSpace>>,作为new实施的一部分,但WorldWorldImpl已经有World方法(没有args!)。

答案 1 :(得分:1)

您的类型WorldVec<Vec<UnitOfSpace>>的别名。 Vec<T>提供了一个名为new的固有关联函数,它不带任何参数。编译器更喜欢为特征中定义的相关函数选择固有的相关函数,因此它选择没有参数的固有new而不是你自己的new,它带有2个参数。

以下是一些解决此问题的方法:

  1. 明确调用特征的相关功能:

    let w = <World as WorldImpl>::new((120, 120), /* seed from UNIX_EPOCH stuff */);
    
  2. World成为新类型(struct World(Vec<Vec<UnitOfSpace>>);),这将让您定义固有的相关函数(但Vec的固有方法将无法在{{World上使用1}})。

  3. WorldImpl::new重命名为Vec上固有相关功能未使用的名称。