Questions tagged [subtyping]
In programming language theory, subtyping or subtype polymorphism is a form of type polymorphism in which a subtype is a datatype that is related to another datatype (the supertype) by some notion of substitutability. Substitutability means that program elements, typically subroutines or functions, written to operate on elements of the supertype can also operate on elements of the subtype.
Am trying to understand how to extend an interface and use it in another class but each time the compiler throws a casting error. I have tried using wildcard in the method printResult but it does not ...
I have this statement in my university classes' notes on sub-typing relations:
T is a subtype of S if code written for variables of type S can also be safely used on variables of type T
Section 4.10....
asked Jan 14 at 8:34
TF.Ryan
4311 silver badge33 bronze badges
I've been looking for references discussing subtyping in Coq with respect to records, but have come up dry.
I'm specifically wondering what the Record _ : _ := { _ :> * } syntax means, and how it ...
asked Dec 15 '20 at 20:44
I'm writing a Prolog system and I am using polymorphic variants to represent Prolog terms.
In particular, I use polymorphic variants (instead of regular ones) so I can do subtyping while ensuring ...
asked Nov 11 '20 at 13:42
repeat
19.3k44 gold badges5151 silver badges151151 bronze badges
I have a subtyped list:
public class LDBList<T> : List<T>
where T : LDBRootClass { ...
I need to concat them
LDBList<TSHeaderItem> catted = tsh1.Concat2(tsh2);
(and ...
I have the following definitions:
trait A {
fn f(&self);
}
trait B: A {
// ...
}
I'd like implement this kind of function:
fn convert(v: Rc<RefCell<dyn B>>) -> Rc<RefCell&...
asked Oct 16 '20 at 21:30
In java generic subtyping is not allowed which means this will not work -
List<String> ls = new ArrayList<String>();
List<Object> lo = ls;
That is so that things like this can't be ...
I know from this question that Scala generates for a trait such as
trait A {
def a = { ... }
}
a structure that would look similar to the following Java code
public interface A {
public void a();
...
asked Aug 27 '20 at 10:15
I have a situation where I'd like to implement a given trait (CanBeString in the example below). I would like to have the option either to implement that trait using a newly created case class (...
I'm coming from a Java background, and I'm trying to wrap my head around Haskell's type system.
In the Java world, the Liskov Substitution Principle is one of the fundamental rules, and I'm trying to ...
asked Aug 1 '20 at 21:34
raner
71444 silver badges1414 bronze badges
Is there a way to have a subtype of a datatype in SML, such that the subtype would cover only some of the datatype's constructors? Something like this:
(* This works as expected *)
datatype Animal = ...
asked Jul 19 '20 at 9:21
Lassi
2,36222 gold badges1313 silver badges2828 bronze badges
I wonder what is the reasoning behind the following behaviour?
@ trait Bar
defined trait Bar
@ trait Foo { self: Bar => }
defined trait Foo
@ def x: Foo = ???
defined function x
@ val y: Bar = x
...
asked Jul 18 '20 at 12:07
Krever
1,30111 gold badge1313 silver badges2626 bronze badges
The following simple code:
implicit val a: String = "abc"
implicitly[a.type]
fails to compile despite that a is totally in the scope and consistent in type:
Error:(9, 13) could not ...
asked Jul 6 '20 at 0:34
tribbloid
5,0321010 gold badges4545 silver badges7878 bronze badges
List of shapes infers to List[Shape] but list of boxed shapes infers to List[Box[Square | Circle]]
scala> sealed trait Shape
| case class Square() extends Shape
| case class Circle() ...
asked May 31 '20 at 18:40
Mario Galic
39.2k66 gold badges3737 silver badges7171 bronze badges
I have been reviewing Scala knowledge and have been going circles about the variance/lower bound.
In the 'functional programming in scala' book, the Either type, it has below signature/exercise (...
asked May 22 '20 at 14:57
I have a class A and a class A1 extends A. Is it possible to overload a method with subtyping?
I had something like this in mind. I found that this does not work though.
public class Test {
...
asked Apr 28 '20 at 8:23
Herr Derb
4,25744 gold badges2222 silver badges4545 bronze badges
i am trying to understand subtyping. so for example, the transitive property holds where if A is a subtype of B, and B is a subtype of C, then A would also be a subtype of C.
I get confused then if ...
I am just experimenting with the use of Scala type classes within Flink. I have defined the following type class interface:
trait LikeEvent[T] {
def timestamp(payload: T): Int
}
Now, I want to ...
asked Apr 13 '20 at 17:52
I think this is more of a conceptual object oriented programming problem than a technical one, I have a few ideas the I could use, but each feels more like a hacky workaround rather than easily ...
asked Apr 11 '20 at 19:19
I am referencing section 5.1.6.1 of the JLS (https://docs.oracle.com/javase/specs/jls/se13/jls13.pdf).
At point 2, it says that "If there exists a parameterized type X that is a supertype of T, and a ...
asked Mar 17 '20 at 17:39
Consider the following wishful program.
{-# LANGUAGE ExtensibleGADTs #-}
data Free a where
Lift :: a -> Free a
data Free (f a) => FreeFunctor f a where
Map :: (a -> b) -> ...
asked Mar 16 '20 at 6:07
Aadit M Shah
67k2626 gold badges145145 silver badges269269 bronze badges
I'm trying to understand how to use implicits in Scala:
In my code I have:
trait Cell
class EnemyCell extends Cell
class RectangleCell extends Serializable with Cell
I defined a trait Placeable and ...
I'm trying to understand how Dart type system works.
Here is a snippet:
class Base {}
class Derived extends Base {}
void f(Derived x) {}
void main() {
Base x = Base();
f(x);
}
At least on ...
asked Oct 16 '19 at 10:10
I was playing with the idea of a simple wrapper function using TypeScript. I ended up getting something working with the following:
export function logFn<T extends (...args: any[]) => any>(
...
asked Oct 2 '19 at 22:08
Kevin
16311 gold badge11 silver badge66 bronze badges
The following is from a ScalaZ document when I am learning about subtype polymorph in Scala.
scala> trait Plus[A] {
def plus(a2: A): A
}
defined trait Plus
scala> def plus[A &...
asked Sep 16 '19 at 3:57
zell
8,08677 gold badges4141 silver badges8888 bronze badges
In Scala 2.13 and Cats, the following works fine:
import cats.implicits._
Traverse[Option]
However the following fails:
import cats.implicits._
Traverse[Some]
I would like for the latter to work, ...
asked Sep 1 '19 at 21:12
eirirlar
79555 silver badges2323 bronze badges
https://wiki.haskell.org/Polymorphism says
Ad-hoc polymorphism refers to when a value is able to adopt any one of several types because it, or a value it uses, has been given a separate definition ...
asked Jul 18 '19 at 20:37
Is there a way to tell makeExecutableSchema from graphql-tools to ignore certain directives?
I want to query my neo4j database with graphql. I also want to be able to specify subtypes in graphql. ...
asked Jun 23 '19 at 23:54
I am learning OOP concepts, which do not really have well-established definitions.
I heard different things about polymorphism and can not decide what is right.
Most people will say that it is a ...
I was trying to create sub-types of errors in GO. I've asked a question previously regarding the matter.
Now I'am facing an issue with the multiple types. Following code shows the error type ...
asked Jun 13 '19 at 13:17
ThisaruG
2,38255 gold badges3232 silver badges5050 bronze badges
I am trying to define a class with type boundaries.
trait External[T]{
def deserialize( bytes: Array[Byte] ) : T
}
trait A
case class Hi(i: Int) extends A
class Foo[X >: A] extends External[...
asked May 13 '19 at 13:24
igx
3,32155 gold badges3232 silver badges6767 bronze badges
Why are interfaces with no super interface subtypes of Object in Java?
What I am asking about here is why was this design choice taken by the language creators i.e. what is the practical purpose of ...
I'm learning about subtypes and am wondering why an example given here https://www.typescriptlang.org/docs/handbook/type-compatibility.html compiles, but when I pass the subtype directly as an ...
I'm trying to implement some function in a super class so I don't have to always repeat it in its children. Sample:
trait Animal {
def applyF(transition: Animal => Animal): Animal = transition(...
asked Apr 25 '19 at 17:32
Piu130
94622 gold badges1111 silver badges2323 bronze badges
I am having hard time to understand the solution of the given question. I can't understand at each step which of the class' methods are invoked.
I tried to make a list for what are a,b,c declared ...
Assume that A is a subtype of B,
Is(A→B)→A a subtype of(B→B)→A?
Is A→(B→A)a subtype of B→(B→A)?
asked Apr 23 '19 at 18:18
There are 4 classes:
class Lower extends Middle {
private int i;
private String name;
public Lower(int i){
super(i+1);
name = "Lower";
this.i = i;
}
...
Say we have List<List<String>> and List<List<?>>.
Obviously, the type argument ? contains the type argument String, and hence, List<?> is a direct supertype of List<...
asked Apr 11 '19 at 12:07
Here is an example that works fine:
import cats.data.ReaderT
import cats.instances.option._
...
def f1:ReaderT[Option, Service, Int] =
ReaderT(service => for {
res <- Some(10)
...
asked Mar 25 '19 at 11:28
Alexandr
8,3391111 gold badges5353 silver badges9292 bronze badges
According to docs, "Type compatibility in TypeScript is based on structural subtyping". So this is possible:
type Person: {
name: string;
}
const developer = {
name: 'Joe',
language: '...
asked Mar 15 '19 at 10:17
apieceofbart
1,60911 gold badge1919 silver badges2525 bronze badges
Considering the following lines
const a: Array<number> = [1,2,3];
const b = [1,2,3];
const q: Array<boolean | number> = a;
const r: Array<boolean | number> = b;
The above code ...
asked Feb 14 '19 at 21:36
paul23
7,16499 gold badges4444 silver badges105105 bronze badges
From a programming language theory standpoint, in C++, qualifiers like const and volatile allow to express a form of subtyping, with for example int being a subtype of const int.
I was wondering if ...
asked Dec 14 '18 at 20:22
Vincent
49.7k4949 gold badges168168 silver badges338338 bronze badges
I have the following simple functions:
type t = unit -> unit
let f () = ()
let g ?opt:_ () = ()
With these functions defined, (f :> t) succeeds, but (g :> t) fails with the following ...
asked Dec 11 '18 at 19:11
kevinji
9,91344 gold badges3535 silver badges5454 bronze badges
I am quite confused. I stumbled upon the signature rules (referred also by B.Liskov in her work), which state:
Contravariance of arguments. m1 and m2 have the same number of
arguments. If the ...
asked Oct 7 '18 at 21:15
John V
4,30788 gold badges3333 silver badges5656 bronze badges
In TypeScript 3.0.3, The following code will give a compile-time error:
function f() {
if (true) {
return (x: {left:String}) => x.left;
} else {
return (x: {right:String}) =...
asked Sep 3 '18 at 0:06
Keigo Imai
14511 gold badge11 silver badge99 bronze badges
I have class:
class X<T extends B> [...]
// this.p.a :: B | null
methodA(a: T):void {[...]}
methodB(): void {
if(this.p.a){ // :: B
this.methodA(this.p.a) // Error
As you can ...
asked Aug 29 '18 at 16:14
przemo_li
3,68333 gold badges2828 silver badges5252 bronze badges
I have a functional object like:
object Doubler {
def apply(v: Int) = v*2
}
It works when I do Doubler(1)
I can also extend it with lambda type:
object Doubler extends (Int=>Int) {...}
Does ...
asked Jul 27 '18 at 16:06
texasbruce
13.8k1111 gold badges5757 silver badges116116 bronze badges
Consider type B and its subtype A, determined by a predicate P. An instance would be natural numbers for B and prime numbers for A, with some primality test as P. It is straightforward to implement a ...
Does the == operator really compare List by content? Especially with regards to empty List?
The following comparisons work as expected
List("A", "B", "C") == "ABC".split("").toList // true
List() == ...
asked Jul 21 '18 at 17:58
Polymerase
5,02766 gold badges3333 silver badges5353 bronze badges
I'm reading java specs
https://docs.oracle.com/javase/specs/jls/se10/html/jls-4.html#jls-4.10.2
and this line confuses me:
D<U1 θ,...,Uk θ>, where D<U1,...,Uk> is a generic type which ...
asked Jul 20 '18 at 2:35
mzoz
1,03611 gold badge1010 silver badges2121 bronze badges