generics

Sort a List so a specific value ends up on top

落花浮王杯 提交于 2020-05-25 06:21:45
问题 I have a class Offer which contains a filed Category. I want all Offers of a specific category to appear on top, followed by all else. I tried this, but to no avail, what would you recommend? Offers = Offers.OrderBy(x => x.Category == "Corporate").ToList(); 回答1: When you order by a boolean value false (0) comes before true (1). To get the elements that match the predicate first you should reverse the sort order by using OrderByDescending : Offers = Offers.OrderByDescending(x => x.Category ==

Why can't I return a concrete subtype of A if a generic subtype of A is declared as return parameter?

和自甴很熟 提交于 2020-05-24 06:08:11
问题 abstract class IntTree object Empty extends IntTree case class NonEmpty(elem: Int, left: IntTree, right: IntTree) extends IntTree def assertNonNegative[S <: IntTree](t: S): S = { t match { case Empty => Empty // type mismatch, required: S, found: Empty.type case NonEmpty(elem, left, right) => if (elem < 0) throw new Exception else NonEmpty(elem, assertNonNegatve(left), assertNonNegative(right)) // req: S, fd: NonEmpty.type } } This is my failed attempt of implementing the function with

Type mismatch on abstract type used in pattern matching

断了今生、忘了曾经 提交于 2020-05-21 04:53:13
问题 This code compiles with an error: def f1[T](e: T): T = e match { case i:Int => i case b:Boolean => b } // type mismatch; // found : i.type (with underlying type Int) // required: T // case i:Int => i ... And this code implementing GADT looks pretty identical from type checking perspective, but compiles without error: sealed trait Expr[T] case class IntExpr(i: Int) extends Expr[Int] case class BoolExpr(b: Boolean) extends Expr[Boolean] def eval[T](e: Expr[T]): T = e match { case IntExpr(i) =>

Type mismatch on abstract type used in pattern matching

徘徊边缘 提交于 2020-05-21 04:50:32
问题 This code compiles with an error: def f1[T](e: T): T = e match { case i:Int => i case b:Boolean => b } // type mismatch; // found : i.type (with underlying type Int) // required: T // case i:Int => i ... And this code implementing GADT looks pretty identical from type checking perspective, but compiles without error: sealed trait Expr[T] case class IntExpr(i: Int) extends Expr[Int] case class BoolExpr(b: Boolean) extends Expr[Boolean] def eval[T](e: Expr[T]): T = e match { case IntExpr(i) =>

How to pass extential type of tuple to invariant generic function?

青春壹個敷衍的年華 提交于 2020-05-17 06:13:07
问题 I have an extential type of tuple, and I want to pass it to a generic function (I use ClassTag in this example but it is a custom type class with invariant type parameter): type TC = (ClassTag[T], Option[T]) forSome {type T} def foo[T](ct: ClassTag[T], o: Option[T]) = {} val tc: TC = (classTag[String], Option[String](null)) foo(tc._1, tc._2) This gives me error: error: type mismatch; found : scala.reflect.ClassTag[T] required: scala.reflect.ClassTag[Any] Note: T <: Any, but trait ClassTag is

Arithmetic with Generics and Protocols

无人久伴 提交于 2020-05-16 08:44:14
问题 Ahoy Everyone, I have been working on a Node system for the last few nights, and have hit a bit of a road block (again XD) Below is a large chunk of code that handles Plugs ( which can be made up of different data types), and Nodes which hold the plugs, and can do something with the data from the plugs. It was copied out of a playground, so should "work" if pasted into one. I am currently stuck at the execution function in the AddNode , which would access the data from two plugs, add them

Arithmetic with Generics and Protocols

*爱你&永不变心* 提交于 2020-05-16 08:39:13
问题 Ahoy Everyone, I have been working on a Node system for the last few nights, and have hit a bit of a road block (again XD) Below is a large chunk of code that handles Plugs ( which can be made up of different data types), and Nodes which hold the plugs, and can do something with the data from the plugs. It was copied out of a playground, so should "work" if pasted into one. I am currently stuck at the execution function in the AddNode , which would access the data from two plugs, add them

Arithmetic with Generics and Protocols

[亡魂溺海] 提交于 2020-05-16 08:38:59
问题 Ahoy Everyone, I have been working on a Node system for the last few nights, and have hit a bit of a road block (again XD) Below is a large chunk of code that handles Plugs ( which can be made up of different data types), and Nodes which hold the plugs, and can do something with the data from the plugs. It was copied out of a playground, so should "work" if pasted into one. I am currently stuck at the execution function in the AddNode , which would access the data from two plugs, add them

AssertJ `containsExactly` assertion on list with wildcard

给你一囗甜甜゛ 提交于 2020-05-15 10:17:05
问题 I have a getter returning a List with a wildcard: import java.util.List; public interface Foo { List<? extends Bar> getList(); } Where Bar is an other interface. When I write an assertion with AssertJ like this: assertThat(foo.getList()).containsExactly(bar1, bar3); EDIT: my complete usage is to chain a usingElementComparator and to provide a Comparator<Bar> to compare the expected Bar instances. Comparator<Bar> comparator = createBarComparator() assertThat(foo.getList())

Is there any equivalent of C#'s “default” keyword for Kotlin?

我的梦境 提交于 2020-05-15 10:04:07
问题 Code Example: import java.util.UUID interface InterfaceOne<AType> { var abcOne:AType } interface InterfaceTwo<AType> { var abcTwo:AType } class Example<AType>: InterfaceOne<AType>, InterfaceTwo<AType> { override var abcOne: AType // Looking for default value to not from constructor set(value) { field = value //... } override var abcTwo: AType // Looking for default value to not from constructor set(value) { field = value //... } fun test(uuid: AType) { abcTwo = uuid abcOne = default // I'm