traits

Is there any way to work around an unused type parameter?

一笑奈何 提交于 2019-12-23 08:05:12
问题 Code: trait Trait<T> {} struct Struct<U>; impl<T, U: Trait<T>> Struct<U> {} Error: error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates --> src/main.rs:5:6 | 5 | impl<T, U: Trait<T>> Struct<U> {} | ^ unconstrained type parameter It seems that RFC 447 prohibits this pattern; is there any way to work around this? I think it could be solved by changing T to an associated type, but that would prevent me from doing multidispatch. 回答1: A type parameter

How can I create an iterator of &T from either a &Vec<T> or Vec<&T>?

徘徊边缘 提交于 2019-12-23 07:58:04
问题 I have an enum with two variants. Either it contains a reference to a Vec of String s or it contains a Vec of references to String s: enum Foo<'a> { Owned(&'a Vec<String>), Refs(Vec<&'a String>), } I want to iterate over references to the String s in this enum. I tried to implement a method on Foo , but don't know how to make it return the right iterator: impl<'a> Foo<'a> { fn get_items(&self) -> Iter<'a, String> { match self { Foo::Owned(v) => v.into_iter(), Foo::Refs(v) => /* what to put

Can traits have properties & methods with private & protected visibility? Can traits have constructor, destructor & class-constants?

倾然丶 夕夏残阳落幕 提交于 2019-12-23 07:57:56
问题 I've never seen a single trait where properties and methods are private or protected. Every time I worked with traits I observed that all the properties and methods declared into any trait are always public only. Can traits have properties and methods with private and protected visibility too? If yes, how to access them inside a class/inside some other trait? If no, why? Can traits have constructor and destructor defined/declared within them? If yes, how to access them inside a class? If no,

How can I dispatch on traits relating two types, where the second type that co-satisfies the trait is uniquely determined by the first?

北城余情 提交于 2019-12-23 06:49:07
问题 Say I have a Julia trait that relates to two types: one type is a sort of "base" type that may satisfy a sort of partial trait, the other is an associated type that is uniquely determined by the base type. (That is, the relation from BaseType -> AssociatedType is a function.) Together, these types satisfy a composite trait that is the one of interest to me. For example: using Traits @traitdef IsProduct{X} begin isnew(X) -> Bool coolness(X) -> Float64 end @traitdef IsProductWithMeasurement{X,M

Understanding Scala Ordered[ ] trait to compare reference

北城以北 提交于 2019-12-23 05:15:52
问题 Currently, I am learning Scala and now, I am having some confusions understanding Ordered traits to compare object. Consider following example, which are my current understanding of comparing, Case I, class Example(var n: Int) extends Ordered[Example] { // ... def compare(that: Example) = (this.n) - (that.n) } var obj1 = new Example(12) var obj2 = new Example(12) obj1 compare obj2 //return 0 i.e. obj1 and obj2 are equal. Case II, class Example(var m: Int, var n: Int) extends Ordered[Example]

Are traits and interfaces binary compatible?

你离开我真会死。 提交于 2019-12-23 03:36:15
问题 I've just kind of surprised by the fact that Scala is binary incompatible across different releases. Now, since in Java 8 we have default method implementations which is pretty much the same as trait s provide us with is it safe to use traits in Java code? I tried to use it myself as this: trait TestTrait { def method(v : Int) def concrete(v : Int) = println(v) } public class Test implements TestTrait{ // Compile-error. Implement concrete(Int) @Override public void method(int v) { System.out

“Lambdifying” scala Function in Java

安稳与你 提交于 2019-12-23 03:32:23
问题 Using Java and Apache Spark (that has been rewritten in Scala), faced with old API method ( org.apache.spark.rdd.JdbcRDD constructor), that has AbstractFunction1 as it's argument: abstract class AbstractFunction1[@scala.specialized -T1, @scala.specialized +R]() extends scala.AnyRef with scala.Function1[T1, R] {} Because AbstractFunction1 is an abstract class, I cant use Java8 lambdas, so I decided to wrap scala.Function1 trait is the same with java.util.functions.Function but does't

How does implicitly work in this example from Scala in Depth

假装没事ソ 提交于 2019-12-23 02:48:28
问题 In the book Scala in Depth . There's this example of implicit scoping as follows: scala> object Foo { | trait Bar | implicit def newBar = new Bar { | override def toString = "Implicit Bar" | } | } defined module Foo scala> implicitly[Foo.Bar] res0: Foo.Bar = Implicit Bar My question here is how did implicitly find the implementation of the trait Bar in the above given example? I think I am a little confused by how implicitly works 回答1: Apparently, for Foo.Bar, it works like Foo#Bar, i.e., if

Why does using nested traits change PHP behavior?

放肆的年华 提交于 2019-12-22 18:17:43
问题 Using PHP 7.2, I have a class MyClass that uses a trait MyFirstTrait . It is defined like this: class MyClass { use MyFirstTrait; } This MyFirstTrait uses another trait MySecondTrait . It is defined like this: trait MyFirstTrait { use MySecondTrait; } MySecondTrait does not use any other traits. It is defined like this: trait MySecondTrait { } If I define MyClass first and then the traits (it does not matter in which order the traits are defined), an error is raised. The file would be like: /

How and where should I use the keyword “use” in php

限于喜欢 提交于 2019-12-22 10:38:21
问题 I used use the keyword "use" generally above the class definition. Like this: <?php namespace suites\plugins\content\agpaypal; use \Codeception\Util\Fixtures; use \Codeception\Verify; use \Codeception\Specify; class agpaypalTest extends \Codeception\Test\Unit { protected $tester; ... But now I realised, that I have to put the line for the trait Specify into the class definition. Like this: <?php namespace suites\plugins\content\agpaypal; use \Codeception\Util\Fixtures; use \Codeception\Verify