Questions tagged [type-bounds]
For questions about upper/lower type bounds in programming languages with static type systems that support both type arguments and subtyping.
141
questions
1
vote
1
answer
61
views
Understanding covariance and lowerbound in Scala
I struggle to understand Scala's covariance in combination with lower bounds. I will illustrate my confusion in the following code snippet with 2 compilation errors.
class Queue[+T]:
def enqueue[U &...
0
votes
0
answers
37
views
Scala complex context bound [duplicate]
In Scala 2.13 I would like pass a generic parameter of one type OR the another type:
def complex[A : Fractional | Trig]
Like Fractional OR Trig type.
Additional complexity is that the types haven't ...
0
votes
1
answer
89
views
How can I avoid duplicating a type bound in an object class in Scala?
Yes I have checked the very similarly titled question but the answer given is not as helpful to me as I am new to Scala and am having trouble understanding it.
I'm writing some functions that check a ...
0
votes
2
answers
173
views
Type parameter under self-type doesn't conform to upper bound despite evidence
I have a trait with a self-type annotation that has a type parameter. This trait is from a library and cannot be modified. I want to pass this trait to a function that will require an upper bound for ...
3
votes
1
answer
92
views
Why does an iterator of `ToString` items requires them to be `Display` also?
The following code:
enum MyEnum {
A,
B,
}
impl ToString for MyEnum {
fn to_string(&self) -> String {
match *self {
Self::A => format!("A"),
...
0
votes
1
answer
54
views
Java Generics - Upper Bounds through layers of inheritance
With a single layer of inheritance I can do something like this:
// Dog extends Animal
List<Dog> dogs = ...;
// Cat extends Animal
List<Cat> cats = ...;
List<? extends Animal> ...
0
votes
0
answers
176
views
Bound for reference of associated type
I need an associated type that is bound to be "any kind of collection" (i.e. anything that has a .iter() method). Now, .iter() in rust collections isn't part of a trait usually, but ...
4
votes
1
answer
121
views
How can a interface Stream extend Base stream interface having type bounds just as the stream interface i.e. Stream <T>?
I was going through Streams Documentation and had observed that Stream is a typed interface that extends Base Stream with types as T and again Stream <T>
public interface Stream<T> extends ...
1
vote
1
answer
265
views
the method `fold` exists for reference `&[T]`, but its trait bounds were not satisfied, I don't understand the given bounds in question
So I'm building a chip-8 CPU emulator and to make copying data into the memory easier I created the following two methods:
pub struct CPU {
// ... other unrelated fields
pub memory: [u8; ...
2
votes
1
answer
43
views
How to make a type-bound polymorphic pointer point to an extended type, using a parent type-bound procedure?
I am not sure if I wrote the question title right, but I did a MWE to explain me better what I would like to do:
module prueba
type :: basic
class(basic),pointer :: myself => null()
...
2
votes
2
answers
73
views
Scala upper bounds
In a typical Scala upperbound example
abstract class Animal {
def name: String
}
abstract class Pet extends Animal {}
class Cat extends Pet {
override def name: String = "Cat"
}
class ...
0
votes
1
answer
100
views
How to convert an Int into Ordered (Scala)
I have declared a Value class in Scala as:
sealed abstract class Value extends Expression with Ordered[Value] {
type T <: Ordered[T]
def value: T
override def compare(that: Value): Int = { .....
5
votes
2
answers
552
views
How can I generate trait bounds in a declarative macro?
I have a trait with a large number of associated types. I want a function that uses those associated types on both sides of a where clause bound:
trait Kind {
type A;
type B;
// 20+ more ...
1
vote
1
answer
55
views
Interface type parameter extends the interface?
I came by this construction when reading a codebase and I can't figure out what it does/represents:
public interface MyInterface<T extends MyInterface<T>> {}
I don't understand what the ...
0
votes
1
answer
215
views
Type parameter cannot have any other bounds if it's bounded by another type parameter: what does this mean and how to resolve it?
I was implementing custom list class MyList<T> in kotlin. In that, I wanted to add insertSorted function, which inserts a new element into the list in sorted order. For that, T must implement ...
0
votes
1
answer
36
views
Why doesn't the compiler infer T(interface) to be class that implements T when having double bounded generic type?
I have a class CBound that implements two interfaces IBound1 and Ibound2:
class CBound implements IBound1, IBound2 {}
I have a generic method that accept as type parameter a class type that ...
2
votes
1
answer
78
views
Why is javac compiling this code with bounded type generics on the return type of a method? Is it an invalid type inference? [duplicate]
We have code similar to the following:
import java.io.Closeable;
public class Test {
@FunctionalInterface
public interface Action1<E extends Exception> {
void run() throws E;
...
2
votes
1
answer
171
views
How to avoid duplication of type bound in Scala
I have a class with a constrained type parameter.
I've tried with identity but the return type is not precise.
And in the method identityTP, I need to specify again the constraint
How to avoid to ...
1
vote
1
answer
45
views
Non-unary type constructor bounded by unary type constructor
The title is attempting to describe the following subtyping
implicitly[Map[Int, String] <:< Iterable[(Int, String)]]
Type parameter A is inferred to (Int, String) here
def foo[A](cc: Iterable[...
1
vote
1
answer
288
views
Why can't I return a concrete subtype of A if a generic subtype of A is declared as return parameter?
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 {
...
0
votes
2
answers
59
views
Accept any type for which there's a suitable method
I have the class MyClass, which uses the same method name for overloading:
public class MyClass {
void add(Integer val) {
// ...
}
void add(Boolean val) {
// ...
}
}
Now I'd ...
1
vote
2
answers
119
views
Scala Upper type bounds implicitly?
I have a first trait like this :
trait FirstTrait[U] {
val myVal: U
}
And another one as follows :
trait SecondTrait[T <: firstTrait[U],U]
For the implementation I am doing :
case class ...
1
vote
1
answer
153
views
Prove that a runtimeClass satisfies a type Bound in Scala
I have a method that writes one of my classes Foo, which is defined as Thrift, in Parquet form.
import Foo
import org.apache.spark.rdd.RDD
import org.apache.thrift.TBase
import org.apache....
0
votes
2
answers
82
views
The correct signature of a utility function to Array class: Comparable<E> or Comparable<? super E>?
I'm trying to write a utility function to the Array class.
The function should return the minimum element for all kinds of comparable types.
My question is which signature should the function has:
...
0
votes
1
answer
179
views
In Java or Scala, how to define conditional control flow based on whether type bound is satisfied?
In scala, I have a class definition:
class A[T >: Null]{}
And a function that optionally creates an instance:
def use[T](): Option[A[T]]
now I want to define it such that:
it returns None if ...
2
votes
2
answers
314
views
Scala lower bound type parameter does not work when type argument is not specified explicitly
I have a below class which uses covariance annotation and another type parameter for its method to which class type parameter is lower bound
class MyQueue[+T]{
def add[U >: T](arg:U):Unit= {
...
21
votes
2
answers
2k
views
Workaround for TypeVar bound on a TypeVar?
Is there some way of expressing this Scala code with Python's type hints?
trait List[A] {
def ::[B >: A](x: B): List[B]
}
I'm trying to achieve this sort of thing
class X: pass
class Y(X): pass
...
0
votes
1
answer
53
views
How to write fuctions that takes IntoIter more generally
I was reading an answer to stackoverflow question and tried to modify the function history to take IntoIter where item can be anything that can be transformed into reference and has some traits Debug ...
2
votes
1
answer
73
views
Compile-time Type Parameters with Multiple Bounds
Can I use Type Parameters with multiple bounds to guarantee at compile-time that a container's contents conform to certain traits?
This is probably best expressed in code:
public class TestCase {
...
0
votes
1
answer
145
views
Upper Type bound Polymorphic method Scala
I'm trying out some generic functions in Scala but I seem to be stuck trying to get the following example to work:
I've got the following (simplified) problem. I've got a parent config (which is a ...
5
votes
2
answers
1k
views
How to require at compile time that a type parameter be a trait (and not a class or other type value)?
I'm looking for some kind of upper bound on a generic parameter T that ensures that T is a trait.
class Foo
trait Bar
def f[A ??? IsATrait] = ???
// f[Foo] Won't compile
f[Bar] // this is fine
4
votes
0
answers
128
views
How can I add a trait bound to a public type that refers to a private type? [duplicate]
I have a public generic type that wraps a private generic type. I'd like to state that my public type implements a trait whenever the private type does as well:
pub trait Example {}
pub struct ...
0
votes
1
answer
106
views
How to express type bounds on a type erased generic interface in Scala?
Here is a interface defined in Java:
public interface TBase<T extends TBase, F extends TFieldIdEnum>
When I tried to add type bounds for methods using this interface as below:
def test[TB[A &...
2
votes
2
answers
231
views
Can I force a constructor to put a stricter bound on its generic type?
In java, generic classes have constructors to construct instances of some generic type. This is simple, and callers of the constructor can specify any type that is within bounds.
Is it possible to ...
2
votes
1
answer
514
views
Java 8 compilation error "inference variable D has incompatible bounds"
Java 8 won't compile the code below (Java 7 used to do it fine). I understand the error somewhat but have no idea how to fix my code, can you help me?
The error is on this line:
spec = spec.and(...
5
votes
1
answer
381
views
Scala - overriding type-member with bounds
I have the following problem with hierarchy of traits in Scala code:
First of all, I have a basic trait MyTrait[A] with such definition:
trait MyTrait[A] {
def v1: A
}
It is then followed by a ...
2
votes
0
answers
59
views
Why can't a List<Set<? extends Integer>> be assigned to a List<Set<? extends Number>>? [duplicate]
Trying to get some generics to work, I ended up with the following simplified example:
Set<? extends Number> setOfNumbers;
Set<? extends Integer> setOfIntegers = new HashSet<&...
2
votes
1
answer
1k
views
syntax: multiple generic constraints and inheritance, interfaces
what is the correct syntax for specifying multiple generic bounds/constraints in Kotlin?
class CustomClass<T> where T: Constraint1, T: Constraint2,
ParentClass<T>(), Interface1 { /* .....
10
votes
1
answer
349
views
Does Delphi Generics support Lower / Upper Type Bounds?
Does Delphi support lower / upper type bounds for its generics, e.g. such as Scala does?
I did not find anything about it in the Embarcadero docs:
Overview of Generics
Declaring Generics
Constraints ...
1
vote
1
answer
1k
views
Wrapping RefCell and Rc in a struct type
I would like to have a struct which has a writable field, but explicitly borrowable:
struct App<W: Clone<BorrowMut<Write>>> {
stdout: W,
}
... so it can internally use it:
...
0
votes
1
answer
79
views
scala generic conversion A[T] => B[T]
I am trying to use generics to not lose the subtype of the implementation.
It's not totally clear to me how to search for this question although I believe it has as a name.
I've a trait named conf and ...
0
votes
0
answers
33
views
Scala generic multiple type variant [duplicate]
i do some experiments with Scala types
and tried to do something like that:
case class Foo(name: String)
defined class Foo
scala> case class Bar(id: Int)
defined class Bar
scala> def ...
0
votes
1
answer
58
views
Scala type bounds in builder
I think I narrowed my problem down to the following:
trait Thing
case class SpecificThing(i: Int) extends Thing
trait ThingSource[T <: Thing] {
def next: T
}
class SpecificThingSource() ...
6
votes
1
answer
880
views
Strange Java cast syntax using & [duplicate]
(Actually, this question is not directly related to lambdas, but to casts using bounds, so the question marked as duplicate does not provide an answer to this question. You'll find the answer to my ...
0
votes
0
answers
119
views
scala reflection type bounds
I am trying to build a full set of paths from a case class using the Scala reflection api. One of the parameters passed to the constructor has a trait as an upper type bound (all of the instances of ...
4
votes
1
answer
438
views
Why does a trait requiring AddAssign as a supertrait also require it to be Sized?
I have a trait, and to implement that trait, I would like to require the implementor to implement AddAssign; however, doing that results in my trait seemingly needing to require Sized:
trait Foo: ::...
3
votes
1
answer
593
views
Upper type bound allowing subtypes but not the parent type
Is it possible to have a generic method with an type bound that amounts to "every possible concrete subclass of this trait, but not the trait itself?"
As an example, suppose I have the following ...
2
votes
1
answer
268
views
How to understand the use of supertypes and variance in Scala's Option?
Specifically, looking at getOrElse.
Scala's Option is defined to be covariant in A like this:
sealed abstract class Option[+A] extends Product with Serializable {
self =>
@inline final def ...
5
votes
1
answer
532
views
Higher kinded type constructor with upper type bounds doesn't work if bound is abstract type member
I want to define a trait that is parameterized by an upper bound R and a higher kinded type constructor F[_] that accepts only arguments that are subtypes of R. I want that this trait implements a ...
0
votes
2
answers
111
views
Scala Generics Type Bounds - Pointing to the Actual Type
I'm playing with scala generics and type bounds to understand its possible use cases. I'm puzzled with a scenario.
Let's say I have a trait Combinable
trait Combinable[T] {
def combine(other: T):...