19

Unless you have at least 3 use-cases where the algorithms will be used with different types and sizes, you are not going to do good job of the genericity anyway. So don't bother too much. I'd recommend writing it in plain C using custom typedefs for all types (except things that are obviously size_t, int or such) and #defines for any relevant sizes. Than ...


14

Your specific problems are relatively easily solved. Problem 1 is just a syntax issue for how to write methods with generic type parameters. In your case this would be: private List<T> SortList<T>(List<T> aList) It's the <T> after the method name which allows you to use T as a generic type parameter in the rest of the signature ...


8

Generic programming encompasses more than just “Generics”, which in turn is another name for parametric polymorphism. This concept is rather widespread and not constrained to object-oriented languages. It is a distinguishing feature of the ML language family which includes Standard ML, Ocaml (which also supports OOP, but is primarily functional) and Haskell. ...


7

Nope. In fact, many of the best systems use both in combination. Containers are worthless if they are not generic- case in point, Java or C#'s containers when those languages were launched. Indeed, generic programming is virtually identical to OOP, except that it occurs at compilation/interpretation time rather than execution time, which has a large number ...


5

What you are describing sounds like you want something similar to the relationship between a DateTime and a TimeSpan. Perhaps you could describe it as a Note and a NoteShift. I think that calling it a Note instead of an OctaveNote is much clearer when you consider that C7 is a different note than C4 (C in the 7th octave vs C in the 4th). Further learning ...


4

To answer your questions directly: 1) No, it will not affect performance much. Generics are mostly erased by the time the program gets executed anyway, so the additional cost is really only multiple (up to 3) map lookups instead of 1. I would not expect this to ever be a bottleneck in a scenario where usage of a standard HashMap is acceptable. 2) Not ...


4

I hope it's not poor form to answer my own question, but upon trying to implement a LogicProgramAnswersetIterator, I ran into some problems that lead me to think either I would have to go to "heroic" measures to pretend that my objects are iterable, or else admit that iterator is not a good match for this use case. I think that the reasons why that is the ...


3

Take a look at the documentation - specifically the category table and the stream iterators section. The stream iterators all belong to the InputIterator or OutputIterator categories (only the first is relevant for your case). We can approximately say that input streams and InputIterators are equivalently powerful/expressive, but IMO streams have a lot of ...


3

I don't like this bit: void Equip(Equipment equipment) { ...(check if equipment inherits from or is of type T) } And that's the same thing @MikeSW apparently meant, but didn't elaborate and got downvoted. If you have generics (reified at that, in C#), why do you leave type-checking until runtime? The equip function has code to determine if the ...


2

My question is: Would you recommend using this in a serious project? Yes! Here is something that is mostly true about library functions: they necessarily have to be generally applicable in order to accommodate many users with different requirements. If you know the underlying algorithm, the library function is going to be larger and slower than your own ...


2

It seems like it would be easier to use the Activator class' CreateInstance method instead: T worker = (T)Activator.CreateInstance(typeof(T)); It essentially does the same thing. CreatInstance uses the default/parameterless constructor for the type and returns an object (thus the casting). If one doesn't exist, it will throw an exception. Quick Edit: ...


2

These are known as Kinds or Type Classes (in some areas, not others) or a few other things. They are defined formally as any other function, except the Type Constructors (another searchable term for parameterized types) use kinds as their signature rather than first level types. These have been formally defined in the past and proper academic research ...


2

Generic functions take the type of at least one function argument generically at compile time. That is, the compiler finds out which type is used at a certain place and applies exactly this type where it is used in the function. E.g. if you have a generic argument in your function that is used with a + operator, the type must have appropriate methods. For ...


1

The most elegant way to do this is to represent an OctiveNote as an integer. This is in swift but it should be easy to translate: enum Note: Int { case c, d, e, f, g, a, b } struct OctiveNote { let rawValue: Int init?(rawValue: Int) { precondition(rawValue >= 0) self.rawValue = rawValue } init(n: Note, o: Int) { ...


1

There are several meanings to "generic". Informal definition "generic" in everyday language something that shares common properties but is less specific in some ways. Under this perspective, you could consider qsort() as generic : the code of this function is able to sort any fixed size data structure for which you can define a comparison function by ...


1

Its fairly common to use a custom BaseController from which your other controllers inherit rather than Controller. I dont think there is any particular danger here. However! I have to agree that its not good practice. Your approach suggests that you will go all the way down to generic repositories. But at some point you are going to have to deal with ...


1

You are doing it wrong. Your approach simply violates the Interface Segregation Principle which says No client should be forced to depend on methods it does not use Imagine if I have a controller UsersController which does not support IGenericController<T, Y>.Delete(Y code) method (for whatever reason) but wishes to reuse most of the logic in ...


1

On https://gitter.im/akka/dev, Konrad Malawski writes: It's about the horrible error messages that one gets if things go wrong with HLists not depending on shapeless, so people can use whatever version of shapeless that they want to it's totally possible to build a HList based version of those methods and give it to people as a library, ...


1

If it is a little confusing to you, it's a problem. Someone unfamiliar to the code (which will probably be you in 2 months) will be utterly baffled. From the look of your code, you are letting yourself in for a lot of boilerplate. You might be best served looking at a single problem and saying "What's the simplest thing that could possibly work" and ...


Only top voted, non community-wiki answers of a minimum length are eligible