In Scala; should I use the App trait?

The problem with the Application trait is actually described in its documentation: (1) Threaded code that references the object will block until static initialization is complete. However, because the entire execution of an object extending Application takes place during static initialization, concurrent code will always deadlock if it must synchronize with the enclosing object. This … Read more

Is there any trait that specifies numeric functionality?

You can use num or num-traits crates and bound your generic function type with num::Float, num::Integer or whatever relevant trait: use num::Float; // 0.2.1 fn main() { let f1: f32 = 2.0; let f2: f64 = 3.0; let i1: i32 = 3; println!(“{:?}”, sqrt(f1)); println!(“{:?}”, sqrt(f2)); println!(“{:?}”, sqrt(i1)); // error } fn sqrt<T: Float>(input: T) … Read more

Find out whether a C++ object is callable

I think this trait does what you want. It detects operator() with any kind of signature even if it’s overloaded and also if it’s templatized: template<typename T> struct is_callable { private: typedef char(&yes)[1]; typedef char(&no)[2]; struct Fallback { void operator()(); }; struct Derived : T, Fallback { }; template<typename U, U> struct Check; template<typename> static … Read more

How do closures infer their type based on the trait they’re required to implement?

It seems to be a glitch. The Rust-analyzer LSP is able to infer what the type is supposed to be, but for whatever reason the compiler can’t. From what I can tell, this code doesn’t compile on any version of Rust, and cannot be automatically fixed with cargo fix. Interestingly, the compiler does seem to … Read more

How to read (std::io::Read) from a Vec or Slice?

While vectors don’t support std::io::Read, slices do. There is some confusion here caused by Rust being able to coerce a Vec into a slice in some situations but not others. In this case, an explicit coercion to a slice is needed because at the stage coercions are applied, the compiler doesn’t know that Vec<u8> doesn’t … Read more