218 questions
-1
votes
1
answer
65
views
WTF there are many definitions for polymorphism
I’m really confused about what polymorphism in OOP actually means. Everyone explains it differently, and I’m not sure which definition is correct:
In some places, polymorphism is explained as dynamic ...
0
votes
1
answer
54
views
How can I define linked types (e.g., abstract matrix and corresponding column type)?
I am writing functions manipulating binary matrices and I would like to make them parametric where possible.
How can I create structs that have, for example, a matrix and an element of the column ...
0
votes
1
answer
433
views
can I pass a generic type into a SwiftUI view as @Bindable? [duplicate]
Swift newbie here, please be kind!
How can I convert this SwiftUI view into a version that accepts different types of items as input?
I have this view in my iOS app:
struct EditMeshType: View {
...
1
vote
0
answers
107
views
Is there any signature that can make "both" defined as "both f = bimap f f" work with heterogeneous pairs? [duplicate]
This works
> bimap succ succ (1, 'a')
(2, 'b')
but this doesn't
> both f = bimap f f
> both succ (1, 'a')
<interactive>:11:12: error:
• No instance for (Num Char) arising from the ...
8
votes
1
answer
274
views
How does 'forall' affect function signature?
I declared two functions using the 'forall' quantifier. The first of them has a quantifier with all generic type parameters before the signature. The second has quantifiers in place of the first usage ...
0
votes
1
answer
59
views
Foreign key parametred to tackle different tables
My context
Any node has a support....but this support can be a wall or a pylon (actually there are about 7 different types of support)
Important: the column type_support is saying which table to ...
0
votes
1
answer
66
views
resolving actual runtime type of generic parameter rather than inferred type
First, setup
interface IRequirement { }
interface ITarget { ICollection<IRequirement> Requirements { get; set; } }
interface IRequirementHandler<TRequirement, TTarget>
where ...
2
votes
0
answers
45
views
What's the different between these two `type` define which have different generic place, and how to use them? [duplicate]
I found 2 different type like:
type Pair = <Head, Tail> (head: Head) => (tail: Tail) => {head: Head; tail: Tail;}
type Pair <Head, Tail> = (head: Head) => (tail: Tail) => {...
1
vote
0
answers
448
views
How to use parametric typing with structs that have many fields different types
I have a struct that has quite a few different fields. Initially, I can use parametric typing for all of the fields. For example:
struct MyStruct{TF, TI, TB}
a::TF
b::TF
c::Array{TF, 2}
...
-1
votes
2
answers
110
views
How to bypass upcasting and achieve polymorphism without virtual and override in C#
I have three classes called Animal, Cat and Dog where Cat and Dog inherit from Animal:
public class Animal
{
public void Talk()
{
Console.WriteLine("Parent");
}
}
public ...
-1
votes
2
answers
407
views
Error C# (CS0246) while implementing an interface in a class in Unity
I'm getting acquainted with the Polymorphism of OOP while making a game in Unity.
I try to use an interface that will take part in a damage system.
Here is the interface code:
using System.Collections;...
0
votes
2
answers
334
views
protocol annotation in var declaration breaks code
I am trying to call the method .enumerate() on an instance of a type which conforms to the protocol Sequence. According to Apple's documentation, this should be correct, since .enumerate is part of ...
0
votes
4
answers
839
views
Julia parametric subtyping "<:" operator equivalent for declaring the parametric type
This is me probably going off what is considered idiomatic in Julia, as I am clearly abusing parametric types (generic types instead of hardcoded ones) but I am struggling to understand why this doesn'...
2
votes
1
answer
112
views
Parametric constructors
I am having some issues understanding the concept of parametric constructors in Julia. I am looking at the standard example in the Julia docs:
struct Point{T<:Real}
x::T
y::T
end
To my ...
0
votes
1
answer
70
views
How to declare a type that matches any function in Standard ML?
I'm trying to declare a record type that has two entries, one named id that is a string, and another named algorithm that can be any function.
According to what I've investigated, I need to use ...
0
votes
0
answers
55
views
swift, troubles with generic parameters and constraints
I'm writing some tests and mocks for my product but I'm having troubles with generic parameters and constraints...
enum ApiResult<Success, Failure> where Failure: Error {
case success(...
1
vote
1
answer
60
views
Extending and/or parametric object as attribute
As I understand now from Array as object I have to use parametric object, because using non-parametric Logtalk objects implies that I have to use assert i.e. any change/set rewrites the whole array.
...
2
votes
0
answers
399
views
How to restrict a generic type parameter to child classes?
I'm trying to do something like that:
internal class ConcreteLinkedItem : GenericLinkedItem<ConcreteLinkedItem>
{ //Specific methods which use GenericLinkedItem
}
internal class ...
3
votes
1
answer
170
views
Why is this OCaml definition accepted with a wrong type?
I have defined a buggy function:
let first : 'a -> 'b -> 'a = fun x y -> y ;;
(* Result: val first : 'a -> 'a -> 'a = <fun> *)
The compiler accepts it and changes the type from '...
2
votes
0
answers
381
views
Is there a way in SQLAlchemy to proxy a JSON field of key/value pairs to act as individual columns?
My situation is similar to that of the Vertical Attribute Mapping examples covered in the SQLAlchemy documentation. However, the column names and values are stored in a JSON field as a dictionary. The ...
0
votes
2
answers
183
views
What is the type of the function g = (.).(.)?
The answer is: (a -> b) -> (c -> d -> a) -> c -> d -> b
But I don't know how to get there.
3
votes
1
answer
282
views
Julia restrict parametric return type
I want to restrict the parametric return type (a Vector of something) of a function.
Say I have a function f defined as follows:
julia> function f()::Vector{Real}
return [5]
end
f ...
3
votes
1
answer
100
views
Parametric theorem implied by goal
During some development using cubical-agda, I noticed (and later checked) that my current goal, if proven would also imply such theorem:
parametric? : ∀ ℓ → Type (ℓ-suc ℓ)
parametric? ℓ = (f : {A : ...
1
vote
3
answers
248
views
Signature of `join bimap` in Haskell
In one of the solutions on codewars I've met the following expression:
join bimap
where join :: Monad m => m (m a) -> m a,
and bimap :: Bifunctor p => (a -> b) -> (c -> d) -> p ...
6
votes
3
answers
746
views
A way to generalize Haskell's Either type for arbitrarily many types?
I am creating a turn based game. I want to define a datatype that encodes one type out of many possible types. Here is the motivating example:
I have defined a Turn type using GADTs, so the type of ...
5
votes
4
answers
590
views
Polymorphism in Haskell
I'm new to Haskell and reading Haskell from first principles.
BTW this question is general not related to this book, I have taken following question as example
In Chapter 10, Section 10.5, Q 5, part f
...
2
votes
2
answers
268
views
Casting to arbitrary type in Typed Racket folding a Tree
I'm trying to produce a typed Racket procedure that for some type A, takes a Tree, and a function from two As to an A, another parameter of type A, and returns a value of type A. I'm not very familiar ...
0
votes
1
answer
38
views
Static member declaration in Parametrized polymorphism
I have gender class with Male and Female as my parametric types of class
I am using following hierarchy:
#ifndef __GENDER_H
#define __GENDER_H
#include <string>
using namespace std;
// Forward ...
4
votes
2
answers
330
views
How to make constructors that create default values for the parametrically typed fields
For a type with parametrically typed fields like:
struct Point{T <: AbstractFloat}
x::T
y::T
end
How to make an outer constructor that creates default values with the desired types? For ...
2
votes
3
answers
542
views
Why are implementations of Iterator<Item = T> and Iterator<Item = &T> conflicting?
This code fails to compile:
pub trait ToVec<T> {
fn to_vec(self) -> Vec<T>;
}
impl<I, T> ToVec<T> for I
where
I: Iterator<Item = T>,
{
fn to_vec(self) -&...
0
votes
1
answer
298
views
How do implement a struct that takes a trait MyTrait<A>? [duplicate]
I've defined a trait as follows:
trait Readable<E> {
fn read_u8(&mut self) -> Result<u8, E>;
fn read_u16be(&mut self) -> Result<u16, E>;
}
The idea is to ...
2
votes
1
answer
2k
views
How to initialize a generic variable in Rust
In a function generic on T, how can I properly create and initialize a variable of type T in safe (or unsafe) Rust? T can be anything. What is the idiomatic way of doing such thing?
fn f<T>() {
...
76
votes
2
answers
30k
views
When and why to use AsRef<T> instead of &T
AsRef documentation writes
Used to do a cheap reference-to-reference conversion.
I understand reference-to-reference part what does it mean by cheap? I hope it has nothing to do with complexity ...
1
vote
1
answer
347
views
How to send generic T to another thread?
How to send generic T?
I try to send a generic T to another thread but I'm getting:
error[E0308]: mismatched types
--> src/main.rs:23:22
|
23 | t1.merge(Element(vec![3]));
| ...
0
votes
1
answer
35
views
Is there a good design pattern: common method has a superclass param but contains calls to methods available in child classes (thus requiring casting)
So if I wanted a method to be common for 3 child classes, like this:
public void commonForAllAnimals(Animal animal) {
// this method exists in subclasses that I need
animal.foo();
// only in Dog
...
2
votes
3
answers
128
views
Making a generic From/Into shortcut for two-steps From
StructA implements From<StructB>, and StructB implements From<S>.
How can I generically implement a 'shortcut' Into<StructA> for S or From<S> for StructA?
If this is a bad idea,...
2
votes
1
answer
1k
views
Implement trait for base type (float)
I want to work with rings, so I have a trait RingOps and I want float to be a part of it. I think float implements each supertype so deriving would be great, but if not, how to do this?
trait RingOps: ...
3
votes
1
answer
345
views
Is there a way to pass an operator with unknown type in haskell?
I have a function f op = (op 1 2, op 1.0 2.0), which is required to work like this:
f (+)
(3, 3.0)
But without declaring the type of f it works like this:
f (+)
(3.0, 3.0)
And I'm struggling with ...
8
votes
3
answers
4k
views
Does Rust implement From<Vec<T>> for Vec<U> if I have already implemented From<T> for U?
I have a struct NotificationOption and another struct NotificationOption2 as well as an implementation for From<NotificationOption> for NotificationOption2.
I'm would like to convert Vec<...
3
votes
1
answer
123
views
Haskell generic type parameters resolver tool or method [duplicate]
Let's look at these functions' types for example:
:t traverse
traverse
:: (Applicative f, Traversable t) => (a -> f b) -> t a -> f (t b)
:t id
id :: a -> a
They have no concrete ...
2
votes
2
answers
1k
views
How to return an Iterator with associated type being generic from a function?
For a function like this:
fn generate_even(a: i32, b: i32) -> impl Iterator<Item = i32> {
(a..b).filter(|x| x % 2 == 0)
}
I want to make it generic, instead of the concrete type i32 I ...
0
votes
2
answers
100
views
Java Parametrized method explanation
I would like to implement a Generic method inside a CommonInterface to implement it with different parameters and return Type.
Therefore I have:
public interface Common {
public <T, N> T call(...
0
votes
2
answers
95
views
Parametric Polymorphism Problem: Using function with single float parameter with an array of float parameters
To clarify what I mean, my issue is with a simulated annealing problem where I want to find the theta that gives me the max area of a shape:
def Area(theta):
#returns area
def SimAnneal(space,func,...
0
votes
2
answers
157
views
Is it possible to do 'recursive parametric polymorphism' in C#
So I am working with .NET Core, and I am working out some data contracts that need to be expandable, to this end, I intend to use parametric polymorphism aka generics.
Now, My sample interfaces and ...
0
votes
1
answer
113
views
"Inference for polymorphic keyword functions not supported"
The following code produces the titular error:
(: f (∀ (a) (-> [#:x a] (U Integer a))))
(define (f #:x [x #f]) (or x 0))
(f #:x 3)
OK, but (f #:x (cast 3 Integer)) still produces the same error. ...
1
vote
1
answer
108
views
What is the full space of parametrically polymorphic functions (not ad hoc polymorphic) operations in programming languages?
On page 349 paragraph 5 of A Theory of Type Polymorphism in Programming, Milner says,
For us, the polymorphism present in a program is a natural outgrowth
of the primitive polymorphic operators which ...
0
votes
1
answer
78
views
How to constrain type of right hand side parameter of operator implementation?
Trying to implement a generic Vec implementing the std::ops::Add trait. I want the implementation to automatically convert the underlying type of the vector on addition so I can do something like this:...
0
votes
1
answer
129
views
Enforcing parametricity on arguments
Using RankNTypes one can enforce various kinds of parametricity. For example, A id :: A:
newtype A = A { unA :: forall a. a -> a }
But how about cases where we only care about the parametricity of ...
2
votes
1
answer
52
views
Why can't you generalize parametric classes?
When you define parametric classes you can only use a fixed number of parameters.
class Container<T> {
...
}
However, if you want to create, say, a Map with multiple values. You must use a ...
3
votes
2
answers
104
views
Functoriality of List in Pure Haskell Lambda Calculus
I'm trying to implement various things in pure lambda calculus with Haskell.
Everything works fine
{-# LANGUAGE RankNTypes #-}
type List a = forall b. (a -> b -> b) -> b -> b
empty :: ...