traits

How to deep copy classes with traits mixed in

扶醉桌前 提交于 2019-12-10 20:04:24
问题 Here's some sample scala code. abstract class A(val x: Any) { abstract def copy(): A } class b(i: Int) extends A(i) { override def copy() = new B(x) } class C(s: String) extends A(s) { override def copy() = new C(x) } //here's the tricky part Trait t1 extends A { var printCount = 0 def print = { printCount = printCount + 1 println(x) } override def copy = ??? } Trait t2 extends A { var doubleCount = 0 def doubleIt = { doubleCount = doubleCount + 1 x = x+x } override def copy = ??? } val q1 =

traits.api error in python

无人久伴 提交于 2019-12-10 19:37:41
问题 I downloaded the Enthought Tool Suite through this website: http://code.enthought.com/downloads/. When I write put this code in the python script: from traits.api import HasTraits, Str, Int from traitsui.api import View, Item from traitsui.menu import OKButton, CancelButton class SimpleEmployee(HasTraits): first_name = Str last_name = Str department = Str employee_number = Str salary = Int view1 = View(Item(name = 'first_name'), Item(name = 'last_name'), Item(name = 'department'), buttons =

How to change empty string to a null using Laravel 5.1?

ぃ、小莉子 提交于 2019-12-10 18:41:25
问题 While using Laravel 5.1, I am trying to check every value before it is saved in the database using Eloquent ORM. My logic is, first trim the value, if the value is an empty string "" , then to convert it to null instead of just an empty string. I was advised to create a Trait which will override the setAttribute method for that. So here is what I have done I have a new folder "app\Traits" inside of a file called TrimScalarValues.php which contains the following code <?php namespace App\Traits

Check for function signature also for inherited functions

橙三吉。 提交于 2019-12-10 18:12:33
问题 I need to check, if a containers erase function returns the iterator. I'd normally check for the function signature via e.g. boost. But in case of a boost class (e.g. flat_set) erase is inherited and thus not found by the check. But I really need it. SFINAE to check for inherited member functions only shows a C++11 solution which I can't use yet. I tried something like this: template <typename T> class has_member_func_erase_it_constit { typedef typename T::iterator iterator; typedef typename

Mocking Scala traits containing abstract val members

自作多情 提交于 2019-12-10 17:09:22
问题 I am writing a Swing application following Martin Fowler's Presentation Model pattern. I create traits that contain abstract declarations of methods already implemented by Swing components: trait LabelMethods { def setText(text: String) //... } trait MainView { val someLabel: LabelMethods def setVisible(visible: Boolean) // ... } class MainFrame extends JFrame with MainView { val someLabel = new JLabel with LabelMethods // ... } class MainPresenter(mainView: MainView) { //... mainView

Is there a way to hint to the compiler to use some kind of default generic type when using Option::None?

ε祈祈猫儿з 提交于 2019-12-10 16:47:57
问题 I need a function that gets an Option of an generic type T that implements the trait std::iter::IntoIterator . A naive implementation could look like the following (yes, the unwrap would panic on None ): fn main() { let v = vec![1i32, 2, 3]; print_iter(Some(v)); print_iter(None); } fn print_iter<T: IntoIterator<Item = i32>>(v: Option<T>) { for e in v.unwrap() { println!("{}", e); } } Test on playground. This works as expected for Some(...) , but fails for None with: error[E0282]: type

Is there a trait for scalar-castable types?

与世无争的帅哥 提交于 2019-12-10 16:27:17
问题 I'm stuck with this code for operator overloading. use std::ops::Add; struct Test<T: Add>{ m:T } impl<T:Add> Add for Test<T>{ type Output = Test<T>; fn add(self, rhs: Test<T>) -> Test<T> { Test { m: (self.m + rhs.m) as T } } } I cannot cast (self.m + rhs.m) to T because it is a non-scalar cast . Is there a trait for types scalar-castable to T ? 回答1: No, there isn't a trait covering this functionality. Only some casts are possible and their full list can be found in The Book. As for your Add

Getting value from a collection without using the Clone trait

为君一笑 提交于 2019-12-10 16:13:13
问题 Is it possible to get a value from a collection and apply a method to it which accepts only self and not &self ? Minimal Working Example What I would like to write is something akin to: use std::collections::HashMap; fn get<B>(key: i32, h: HashMap<i32, Vec<(i32, B)>>) -> i32 where B: Into<i32> { let v: &Vec<(i32, B)> = h.get(&key).unwrap(); let val: &B = v.first().unwrap().1; // Do something to be able to call into // I only need the value as read-only // Does B have to implement the Clone

Scala : get mixin interfaces at runtime

早过忘川 提交于 2019-12-10 16:00:05
问题 I need to get all the interfaces at runtime from a given Class (all loaded in a ClassLoader). For instance, if a class has been declared this way : trait B trait C trait D class A extends B with C with D I want to get this information at runtime : A depends on B and C and D . The java getInterfaces() (or the interfaces() from the clapper library) methods gives only the first dependency, namely: A depends on B . Is there a way to achieve that ? I guess by reflection but I don't know how ? 回答1:

Is there a way to have private functions in public traits?

纵然是瞬间 提交于 2019-12-10 15:13:57
问题 I have an implementation for a public trait that repeats some work over multiple functions, so I'd like to DRY it up with a function that does the shared work, to be called from the functions actually meant to be used. So I have: fn do_private_thing() fn do_pub_1() fn do_pub_2() I don't want do_private_thing() to be exposed in docs or used directly, because it doesn't do anything useful from the perspective of a user of the trait/implementation. But functions in public traits are not private.