小编use*_*762的帖子

“这个特征不能被制作成一个对象,因为它需要 `Self: Sized`”,除非它有 Sized

这是错误消息:

error[E0038]: the trait `room::RoomInterface` cannot be made into an object
  --> src\permanent\registry\mod.rs:12:33
   |
12 |     pub fn load(id : String) -> Result<Box<dyn RoomInterface>, String>{
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `room::RoomInterface` cannot be made into an object
   |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
  --> src\permanent\rooms\mod.rs:36:31
   |
36 |     pub trait RoomInterface : Copy + Sized {
   |               -------------   ^^^^   ^^^^^ …
Run Code Online (Sandbox Code Playgroud)

rust

6
推荐指数
1
解决办法
4380
查看次数

(Python)__ new__方法返回类实例以外的东西

是否可能,如果是,那么怎么做,创建一个类的新对象会返回除对象本身之外的东西?

假设,我希望每个新创建的对象都以包含自身的列表开头.

>> class A:
      *magic*

>> a = A()
>> print a

[<__main__.A instance at 0x01234567>]
Run Code Online (Sandbox Code Playgroud)

可能它可以通过__new__某种方式覆盖方法来完成,但是如何?

python

2
推荐指数
1
解决办法
598
查看次数

通过多行模式匹配的函数声明

我希望有这个漂亮,干净的方式来描述具有函数和模式匹配的对象的属性:

data Animal = Cat | Dog | Cow

isBig :: Animal -> Bool
isLoyal :: Animal -> Bool
-- many more possible properties, including complicated non-bools, methods, and whatnot


--- Describing Cat

isBig Cat = False
isLoyal Cat = False
--- more properties


--- Describing Dog

isBig Dog = False
isLoyal Dog = True
--- more properties


--- Describing Cow

isBig Cow = True
isLoyal Cow = False
--- more properties
Run Code Online (Sandbox Code Playgroud)

但是,这会产生有关多个声明的错误.因为,显然,通过模式匹配的函数定义必须在连续的行中完成.

这表明我的方法是错误的,不像Haskell一样吗?或者它只是语言中的一个缺陷?或者我误解了什么?

haskell function pattern-matching

2
推荐指数
1
解决办法
117
查看次数

“该特征不是为自我实现的”,尽管它完全是

看看这个小片段:

pub trait TestTrait {
    fn test_function(&self) {
        generic_function(self);
    }   
}

fn generic_function<T : TestTrait> (x : T) {
    //do something
}
Run Code Online (Sandbox Code Playgroud)

这会产生以下错误消息:

error[E0277]: the trait bound `&Self: TestTrait` is not satisfied
  --> src\main.rs:10:26
   |
10 |         generic_function(self);
   |         ---------------- ^^^^ the trait `TestTrait` is not implemented for `&Self`
   |         |
   |         required by a bound introduced by this call
   |
note: required by a bound in `generic_function`
  --> src\main.rs:14:25
   |
14 | fn generic_function<T : TestTrait> (x : T) …
Run Code Online (Sandbox Code Playgroud)

rust

2
推荐指数
1
解决办法
1157
查看次数

标签 统计

rust ×2

function ×1

haskell ×1

pattern-matching ×1

python ×1