218 questions
-1
votes
1
answer
61
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
430
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 {
...
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
64
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 ...
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
405
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
4
answers
837
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'...
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 ...
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.
...
3
votes
1
answer
169
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 '...
0
votes
2
answers
179
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
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 : ...
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 ...
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 ...
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 ...
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 ...
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 ...
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
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: ...
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<...
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
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
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. ...
0
votes
1
answer
77
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:...
2
votes
1
answer
50
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 ...