traits

How can I take input from either stdin or a file if I cannot seek stdin?

筅森魡賤 提交于 2019-12-12 10:56:19
问题 I am porting some Python to Rust as a learning exercise and need to take input from either a file or stdin. I keep a handle to my input in a struct so I thought I'd just make a Box<io::Read> but I ran into a situation where I need to seek on the input, and seek isn't part of the Read trait. I know you can't seek in pipes, so I'm going ahead and assuming for now that this method only gets called when the input is a file, but my problem is that I can't check that and downcast in Rust. I know

How can I write a Trait in Julia with open-ended types?

一个人想着一个人 提交于 2019-12-12 10:45:27
问题 This is an attempt to simplify one part of the question I asked here: I want to write some code that is guaranteed to work on types that meet certain criteria. Let's say today I write some code: immutable Example whatever::ASCIIString end function step_one(x::Example) length(x.whatever) end function step_two(x::Int64) (x * 2.5)::Float64 end function combine_two_steps{X}(x::X) middle = step_one(x) result = step_two(middle) result end x = Example("Hi!") combine_two_steps(x) Running this works:

In Play 2.4 with DI, how to use a service class in “Secured” trait?

早过忘川 提交于 2019-12-12 10:43:53
问题 Here's an authorisation example from Play Documentation (version 2.0.4; I tried to find a newer version of this document but couldn't): trait Secured { def username(request: RequestHeader) = request.session.get(Security.username) def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.Auth.login) def withAuth(f: => String => Request[AnyContent] => Result) = { Security.Authenticated(username, onUnauthorized) { user => Action(request => f(user)(request)) } } def withUser(f: User =>

Why does the compiler claim that a generic doesn't implement `Display` even though it should?

帅比萌擦擦* 提交于 2019-12-12 10:33:19
问题 I'm building a library that implements string joins; that is, printing all the elements of a container separated by a separator. My basic design looks like this: use std::fmt; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Join<Container, Sep> { container: Container, sep: Sep, } impl<Container, Sep> fmt::Display for Join<Container, Sep> where for<'a> &'a Container: IntoIterator, for<'a> <&'a Container as IntoIterator>::Item: fmt::Display, Sep: fmt::Display, { fn fmt(&self, f: &mut fmt:

Composition: using traits to avoid forwarding functions?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 10:32:31
问题 Let's say we have two classes, A and B . When using composition to model a "has-a" or "is-implemented-in-terms-of" relationship (e.g. B has-a A ), one of the drawbacks vs. inheritance is that B does not the contain public functionality of A that it requires. In order to gain access to A s public functions, it is necessary to provide forwarding functions (as opposed to inheritance, where B would inherit all of A s public functions). To give a more concrete example, let's say we have a Person

Disabling a FactoryGirl association within a trait

倖福魔咒の 提交于 2019-12-12 09:58:30
问题 In a Rails app, I'm using FactoryGirl to define a general factory plus several more specific traits. The general case and all but one of the traits have a particular association, but I'd like to define a trait where that association is not created/built. I can use an after callback to set the association's id to nil , but this doesn't stop the association record from being created in the first place. Is there a way in a trait definition to completely disable the creation/building of an

PHP traits - change value of static property in inherited class

爱⌒轻易说出口 提交于 2019-12-12 08:37:07
问题 So, this is my trait: trait Cacheable { protected static $isCacheEnabled = false; protected static $cacheExpirationTime = null; public static function isCacheEnabled() { return static::$isCacheEnabled && Cache::isEnabled(); } public static function getCacheExpirationTime() { return static::$cacheExpirationTime; } } This is the base class: abstract class BaseClass extends SomeOtherBaseClass { use Cacheable; ... } These are my 2 final classes: class Class1 extends BaseClass { ... } class Class2

How does insteadof keyword in a Trait work

流过昼夜 提交于 2019-12-12 07:53:47
问题 I was just reading about traits and how multiple php traits can be used in the same php code separated by commas. I do not however understand the concept of the insteadof keyword that is used to resolve conflict in case two traits have the same function. Can anyone please explain how insteadof keyword works and how to use it to tell the engine that I am willing to use function hello() of trait A instead of that of trait B, given there are two traits A and B and a function hello() in both the

PHP Reflection: How to know if a method/property/constant is inherited from trait?

▼魔方 西西 提交于 2019-12-12 07:50:05
问题 I want to exclude all inherited methods from trait(s) from the list that are not overriden in a class So how to know if a class member is inherited from trait? Yes, I can check it like this: if ($trait->hasMethod($methodName) || $ref->getTraitAliases()[$methodName] !== null) { // } But what if the trait method is overriden in a class? How to know it? One way is to check if method bodies are similar, if so, i may exclude it, but is there a better way to achieve this? 回答1: Important notes This

Is there something wrong with an abstract value used in trait in scala?

蓝咒 提交于 2019-12-12 07:15:02
问题 I have trait Invoker { val method: Method } Intellij IDEA code inspection is warning me that "Abstract value used in trait". Everything compiles fine. Is there something wrong with having an abstract value in a trait? If so, how should I specify that all extenders of the trait must define a method property? 回答1: What is meant by this is the following weirdness: trait A { val i: String def j: String } class C extends A { println ("val i = " + i) println ("def j = " + j) val i = "i" def j = "j"