generics

Enumerating generic structs

这一生的挚爱 提交于 2020-01-06 15:16:22
问题 I wanted to try to build a proper implementation of Peano numbers using struct s, but it seems my generics game is not good enough yet and I could use some help. I read the docs on generics and some StackOverflow questions, but they don't fit my case. I introduced a Peano trait and Zero and Succ types: trait Peano {} struct Zero; struct Succ<T: Peano>(T); And implemented a Peano trait for both types to be able to abstract over both: impl Peano for Zero {} impl<T> Peano for Succ<T> where T:

Explanation of the get-put principle

自古美人都是妖i 提交于 2020-01-06 15:13:12
问题 I have read O'Reilly book, in that I came to know this get-put principle . Use an extends wildcard when you only get values out of a structure. Use a super wildcard when you only put values into a structure. And don't use a wildcard when you both want to get and put from/to a structure. Exceptions are: You cannot put anything into a type declared with an extends wildcard except for the value null , which belongs to every reference type. You cannot get anything out from a type declared with an

IComparer<T> child as a generic param could not be used in Sort

浪子不回头ぞ 提交于 2020-01-06 13:56:28
问题 Got stuck trying to sort my List<> in C#. I have an interface type with its implementation class: public interface IRoom { // ... } public class Room : IRoom { // ... } And a base class for my comparers: public abstract class RoomComparer : IComparer<IRoom> { public RoomComparer() { } public abstract int Compare(IRoom x, IRoom y); } And two its children: public class StandardMultipliedSquareMetersComparer : RoomComparer { public StandardMultipliedSquareMetersComparer() { } public override int

Django get class from string

心已入冬 提交于 2020-01-06 11:34:13
问题 I'm looking for a generic way in Python to instantiate class by its name in similar way how it is done in Java without having to explicitly specify the class name in IF..ELIF condition. This is because I have several different models and serializers and want to make them addressable by parameters in the HTTP request. It is to enhance loose coupling and modularity. For example https://www.domain.com/myapp/sampledata.json?model=<modelname> should get the classes <modelname> and <modelname

Generic member function pointer help

人走茶凉 提交于 2020-01-06 08:46:09
问题 Hey, I've got a question about general member function pointers. I'm trying to achieve something similar to the following question How to define a general member function pointer Essentially what I want to be able to do is to register a member function pointer that accepts a generic Event object as its argument with a specific event type (for example, a KeyboardEvent). Then, in my input management class, what I want to be able to do is whenever the user hits a key, I can create a

Code reuse through generics vs polymorphism

房东的猫 提交于 2020-01-06 08:25:12
问题 What is the better way to reuse implementation: inheritance or generics? The model is following: Script has Steps, Steps have Elements. Tree structure is double linked, i.e. Steps know their Script and Elements now their Step. Now, there are 2 types of Scripts: Templates and Runs, where a Run is created at first as a copy of the Template. This results in 2 similar hierarchies ScriptTemplate->ScriptTemplateStep->ScriptTemplateElement and ScriptRun->ScriptRunStep->ScriptRunElement. Most of the

Is there a way to alias a longwinded generic type in the scope of a class?

无人久伴 提交于 2020-01-06 08:18:34
问题 I have a shared repository class using a shared entity/document type, that looks kind of like this: type IEntity<TData extends {}, TType extends string = string> = TData & { id: string; type: TType; createdAtIso: string; } class Repository<TData extends {}, TType extends string = string> { constructor(private _type: TType) { } save(data: TData): IEntity<TData, TType> { return this._save({ id: 'asdf', type: this._type, createdAtIso: new Date().toISOString(), ...data }); } _save(entity: IEntity

Swapping elements of two Arrays in Scala with wildcard type

懵懂的女人 提交于 2020-01-06 07:44:12
问题 I want to do something like the following: class A[T] (_arr: Array[T]) { var arr = _arr } class C { def foo[T <: A[_]](a: Array[T]){ var ele1 = a(0) var ele2 = a(1) for(i <- 0 until ele1.arr.length) { val temp = ele1.arr(i) ele1.arr(i) = ele2.arr(i) ele2.arr(i) = temp } } } But I get the following compiler error: <console>:15: error: type mismatch; found : temp.type (with underlying type Any) required: _$1 ele2.arr(i) = temp ^ I'm not sure why ele1.arr(i) = ele2.arr(i) would work and ele2.arr

Swapping elements of two Arrays in Scala with wildcard type

被刻印的时光 ゝ 提交于 2020-01-06 07:44:05
问题 I want to do something like the following: class A[T] (_arr: Array[T]) { var arr = _arr } class C { def foo[T <: A[_]](a: Array[T]){ var ele1 = a(0) var ele2 = a(1) for(i <- 0 until ele1.arr.length) { val temp = ele1.arr(i) ele1.arr(i) = ele2.arr(i) ele2.arr(i) = temp } } } But I get the following compiler error: <console>:15: error: type mismatch; found : temp.type (with underlying type Any) required: _$1 ele2.arr(i) = temp ^ I'm not sure why ele1.arr(i) = ele2.arr(i) would work and ele2.arr

vhdl :: creating a type with a size parameter

时间秒杀一切 提交于 2020-01-06 06:13:08
问题 I was wondering is there was a way to defined a type with a size parameter in VHDL. e.g. type count_vector(size: Natural) is unsigned (size-1 downto 0); and then later on do something like variable int : count_vector(32) := (others => '0'); variable nibble : count_vector(4) := (others => '0'); Essentially, is there a way to defined an "array-like" type, or is that not allowed by syntax? I am currently trying to use generics for re-usability, but I would like to be able to take maximal