type-constraints Questions
2
I have a typeclass A with a non injective associated type Context
class A a where
type Context a
so many instances of A can have the same Context.
Some instances of A are also instances of the cl...
Intoxicated asked 20/12, 2023 at 12:19
3
Solved
I was really excited when I first heard about C++20 constraints and concepts, and so far I've been having a lot of fun testing them out. Recently, I wanted to see if it's possible to use C++20 conc...
Ansermet asked 15/10, 2021 at 19:11
7
I have the following method with generic type:
T GetValue<T>();
I would like to limit T to primitive types such as int, string, float but not class type. I know I can define generic for cl...
Rebarebah asked 30/4, 2009 at 3:42
1
Solved
In the code below, I define a generic linked list. Go1.18 is happy to use an instance of the list as a key to a map. However, the last line, when uncommented, doesn't compile; I get the error:
Con...
Paraldehyde asked 16/3, 2022 at 23:55
2
Solved
I've been working in quite a number of statically-typed programming languages (C++, Haskell, ...), but am relatively new to Rust.
I often end up writing code like this:
struct LeafNode<K: Ord + ...
Dandruff asked 16/12, 2021 at 11:22
2
I am trying to create forms that inherit from a "generic" base class where the generic argument of that base class has a constraint that it must implement one of my interfaces.
It compiles and run...
Ingeingeberg asked 30/1, 2017 at 8:49
2
Given the type
type EnumerableComponentFactory = <C, I>(config: {
Container: React.ComponentType<C>;
Item: React.ComponentType<I>;
}) => React.FC<{ items: I[] }>;
with...
Pyro asked 15/7, 2021 at 16:54
3
Solved
(As a result of doing the research to answer this question, I (think I have!) determined that the answer is "no." However, I had to look in several different places to figure this out, so I think t...
Slideaction asked 26/8, 2014 at 14:39
11
Solved
Can you please explain to me what where T : class, new() means in the following line of code?
void Add<T>(T item) where T : class, new();
Foreclosure asked 19/1, 2011 at 16:37
1
Solved
I'm trying to wrap my head around generic type constraints in Swift. Here is my starting point:
class Promise<T> {
func resolve(_ block:@escaping (T) ->Void) {}
func fulfill(_ result:T)...
Helmsman asked 21/11, 2019 at 14:43
2
Solved
I'm trying to create a type similar to Rust's Result or Haskell's Either and I've got this far:
public struct Result<TResult, TError>
where TResult : notnull
where TError : notnull
{
priv...
Racism asked 14/11, 2019 at 8:24
3
Solved
I'm trying to customise ASP.NET Identity 3 so that it uses integer keys:
public class ApplicationUserLogin : IdentityUserLogin<int> { }
public class ApplicationUserRole : IdentityUserRole<...
Elspet asked 3/5, 2015 at 19:1
1
Solved
In below example, I want to define a contains method that doesn't compile if a and b are not of the same base type.
In contains1 impl, if a is Seq[Int] and b is String, T is derived to be Any, a...
Grape asked 17/6, 2019 at 18:32
2
Motivation
I have an Either<L, R> class, which represents a value of one of two types, or semantically different states. In some cases, it is valuable to operate on it no matter which alterna...
Arrester asked 16/6, 2019 at 11:32
5
Solved
I have created a couple of interfaces and generic classes for working with agenda appointments:
interface IAppointment<T> where T : IAppointmentProperties
{
T Properties { get; set; }
}
in...
Attainder asked 2/7, 2013 at 20:4
1
Solved
While learning about generics in TypeScript, I wanted to try to recreate the following JavaScript:
function add(x, y){
return x + y;
}
I tried like:
type StringOrNumber = string | number;
fun...
Cotsen asked 31/1, 2019 at 22:44
1
Solved
I'm trying to define a trait with an associated type. I also want the associated type to implement Iterator with its Item associated type implementing AsRef<str>.
While I know how to do it f...
Rialto asked 1/1, 2019 at 19:6
2
Solved
In Haskell we are given the ability to combine constraints on types with a logical and.
Consider the following
type And (a :: Constraint) b = (a, b)
or more complicatedly
class (a, b) => An...
Nelda asked 21/4, 2012 at 0:36
2
Solved
How do I override the method Zero in the following code in such a way that I can return Euro(0) for the definiton in the type Euro
[<AbstractClass>]
type Currency () =
abstract member Zero...
Niggard asked 15/2, 2017 at 15:50
1
Consider the following generic method:
public T2 Frob<T1, T2>(T1 item)
where T1 : class, T2
=> item as T2;
The compiler will refuse to compile this code; The type parameter 'T2' canno...
Bonin asked 13/2, 2017 at 16:4
2
Solved
I have this interface:
type IMovingFunc< 'T > =
abstract member Add : 'T -> unit
Now I want to create a generic type that implements Add function and uses (+) operator:
type MovingSum...
Boaten asked 27/11, 2016 at 14:41
1
Solved
All the experiments described below were done with GHC 8.0.1.
This question is a follow-up to RankNTypes with type aliases confusion. The issue there boiled down to the types of functions like thi...
Stanger asked 26/10, 2016 at 22:39
3
Solved
In the sample code shown below, the "CompileError" method won't compile, because it requires the where T : new() constraint as shown in the CreateWithNew() method. However, the CreateWithActivator&...
Becca asked 12/9, 2016 at 5:58
3
I'm trying to create a generic function which requires of its type argument that it is a record type, and that it has a specific property. Here's a sample that generates the relevant compiler error...
Cite asked 1/9, 2016 at 14:56
4
Solved
I'm attempting to create various extension method for a generic type bound to specific generic type parameters in F#, but the language does not seem to be allowing me:
What I want to do is somethi...
Milan asked 7/10, 2009 at 13:15
1 Next >
© 2022 - 2024 — McMap. All rights reserved.