generic-programming

Avoiding Java Type Erasure

∥☆過路亽.° 提交于 2019-11-29 04:09:09
Is there a way one could avoid type erasure and get access to a type parameter? public class Foo<T extends Enum<?> & Bar> { public Foo() { // access the template class here? // i.e. : baz(T.class); // obviously doesn't work } private void baz(Class<T> qux) { // do stuff like T[] constants = qux.getEnumConstants(); ... } } I need to know about T , and do things with it. Is it possible, and if so, how can it be done without passing in the class in the constructor or anywhere besides the parameter? EDIT: The main purpose of this question is to find out if there is any practical way around type

templates problem ('typename' as not template function parameter)

好久不见. 提交于 2019-11-29 02:36:28
Actually I've a problem with compiling some library with intel compiler. This same library has been compiled properly with g++. Problem is caused by templates. What I'd like to understand is the declaration of **typename** as not template function parameter and variable declaration inside function body example: void func(typename sometype){.. ... typename some_other_type; .. } Compilation this kind of code produce following errors (intel),(gcc doesn't claim): I've got following errors ../../../libs/log/src/attribute_set.cpp(415): error: no operator "!=" matches these operands operand types are

Can someone explain what does <? super T> mean and when should it be used and how this construction should cooperate with <T> and <? extends T>?

我怕爱的太早我们不能终老 提交于 2019-11-28 23:16:16
I'm using generics rather long time but I've never used construction like List<? super T> . What does it mean? How to use it? How does it look after erasure? I also wonder: is it something standard in generic programming (template programming?) or it's just a java 'invention'? Does c#, for example, allow similar constructions? This construct is used when you want to consume items from a collection into another collection. E.g. you have a generic Stack and you want to add a popAll method which takes a Collection as parameter, and pops all items from the stack into it. By common sense, this code

How can I check for generic type in Kotlin

与世无争的帅哥 提交于 2019-11-28 21:54:55
问题 I'm trying to test for a generic type in Kotlin. if (value is Map<String, Any>) { ... } But the compiler complains with Cannot check for instance of erased type: jet.Map The check with a normal type works well. if (value is String) { ... } Kotlin 0.4.68 is used. What am I missing here? 回答1: The problem is that type arguments are erased, so you can't check against the full type Map, because at runtime there's no information about those String and Any. To work around this, use wildcards: if

Scrap Your Boilerplate in f#

邮差的信 提交于 2019-11-28 20:49:29
I've used the Scrap Your Boilerplate and Uniplate libraries in the Haskell programming language, and I would find that form of generic programming over discriminated unions to be really useful. Is there an equivalent library in the f# programming language? Not that I know of; without support built-in to the language/compiler, I expect the only alternative is a reflection-based version. (I don't know how Uniplate is implemented - do you?) Here's the code for a reflection-based version based on the example from the original presentation. I have not thought deeply about its limitations, but this

Boilerplate-free annotation of ASTs in Haskell?

核能气质少年 提交于 2019-11-28 19:56:30
问题 I've been fiddling around with the Elm compiler, which is written in Haskell. I'd like to start implementing some optimizations for it, and part of this involves traversing the AST and adding "annotation" to certain nodes, such as tail-calls, etc. I know I can use SYB or uniplate to do the traversal, but I'm wondering if there's a boilerplate-free way to deal with the types. So, suppose we have a bunch of algebraic types for our AST: data Expr = PlusExpr Expr Expr ... data Def = TypeAlias

'Multipurpose' linked list implementation in pure C

早过忘川 提交于 2019-11-28 16:06:38
This is not exactly a technical question, since I know C kind of enough to do the things I need to (I mean, in terms of not 'letting the language get in your way'), so this question is basically a 'what direction to take' question. Situation is: I am currently taking an advanced algorithms course, and for the sake of 'growing up as programmers', I am required to use pure C to implement the practical assignments (it works well: pretty much any small mistake you make actually forces you to understand completely what you're doing in order to fix it). In the course of implementing, I obviously run

Safely copying fields between case classes of different types

我是研究僧i 提交于 2019-11-28 07:02:37
Assuming you have case classes like the following case class Test1(a:String,b:Int,c:Char) case class Test2(a:String,b:Int) And you instantiate the classes with the following variables val test1 = Test1("first",2,'3') val test2 = Test2("1st",20) Is there a way to use the .copy method (or otherwise), to apply the variables inside Test2 to Test1, something like val test3 = test1.copy(test2) //Note this isn't valid scala code // Result should be ("1st",20,'3') If this isn't possible in pure scala, how would it be done in Shapeless 1/2 (current code is in Shapeless 1, however we are planning to

How can I produce a Tag type for any datatype for use with DSum, without Template Haskell?

╄→гoц情女王★ 提交于 2019-11-28 06:24:54
问题 background I want to write some library code, which internally uses DSum to manipulate a user's datatype. DSum requires a 'tag' type that has a single type argument. However I want my code to work with just any old concrete type. So, I'd like to just take the user's type and automatically produce the tag type. I've asked a very similar question here How can I programatically produce this datatype from the other?, and gotten a great answer. That answer relies on TH, mainly so that it can

What is the purpose of generics before return type

可紊 提交于 2019-11-28 05:14:08
问题 For example, in the following code public <U extends Number> void inspect(U u){ // Some method } what is the purpose of (what is this line doing or how is it read) that comes just before the return type 回答1: This is the syntax that makes your method (as opposed to your class ) generic. Methods of regular and generic classes can be made generic on their own type parameters. In this case, your inspect method is generic on U , which must be a type extending from Number . 来源: https:/