generic

Convert java legacy code to generic - how to replace Object with type?

// legacy code void setCacheValue(String name, Object value){ getServletContext().setAttribute(name, value); } Object getCacheValue(String name){ return getServletContext().getAttribute(name); } // so I want to use generic for "type safety" // first, set method seems working perfectly <T> void setCacheObject(String name, T va...

Why does instanceof seem to work in a static generic function sometimes?

Greetings. This is my first post in this site. I thought that because of type erasure, one could not expect the following code to compile, and indeed, it did not compile on an earlier version of Eclipse. My understanding was that instanceof was a run-time operator and could not know about the generic type which would be, by run-time, ...

C#. Saving information about events and event handlers and removing handlers using this information

I have an object which is creating several event handlers using lambda expressions, and I need to remove all handlers created in that object in one time. I'm trying to create some unified static object which will 'know' which handler relates to which object and event if event handler was created through this static object. I tried some...

Go - Generic function using an interface

Since I've a similar function for 2 different data types: func GetStatus(value uint8) (string) {...} func GetStatus(name string) (string) {...} I would want to use a way more simple like: func GetStatus(value interface{}) (string) {...} Is possible to create a generic function using an interface? The data type could be checked usin...

C# - generic List and ConvertAll() Method, how does it internally work?

Hello, from some code I found in Sacha Barbers free mvvm framework chinch I saw this: return new DispatcherNotifiedObservableCollection<OrderModel>( DataAccess.DataService.FetchAllOrders( CurrentCustomer.CustomerId.DataValue).ConvertAll( new Converter<Order, OrderM...

Create a ASP.NET Generic gridview

I wont to create a User Control based in gridview that have the edit add delete incorporate, the problem is these: In the admin part of my web site i have to repeat the same action for view add delete update the data for different datasource. I wont to create a generic gridview that have incorporate these action. The gridview can take...

Can i get the generic type of an object?

this is my method: GetListItemsPainted<T>(List<T> list) and i don't know what type is that list of, how can i create new list that would have the passed list type? something like this: List<list.GetType()> newList = new List<list.GetType()>(); how can i cast my list to the real type so i would have all his properties etc.? thanks ...

Generic Text Only printer driver mangles control codes

If an escape character (or most other characters < 0x20) is sent to the generic / text only printer it gets printed as a period. Using the code in the WinDDK is it possible to 'correct' this behaviour so that it passes it through unmodified? The general scenario for this is that some application ('user app') outputs a document to a win...

How to handle JPA annotations for a pointer to a generic interface

I have a generic class that is also a mapped super class that has a private field that holds a pointer to another object of the same type: @MappedSuperclass public abstract class MyClass<T extends MyIfc<T>> implements MyIfc<T> { @OneToOne() @JoinColumn(name = "previous", nullable = true) private T previo...

What trick does Java use to avoid spaces in >> ?

In the Java Generic Book, while contrasting the difference between C++ Templates and Java Generic says: In C++, a problem arises because >> without the space denotes the right-shift operator. Java fixes the problem by a trick in the grammar.) What is this trick? ...

How do I restrict accepting only one type in my generic method?

I have a generic function foo, which accepts any type and prints them out. public static <T> T foo(T... arg) { List<T> foo = Arrays.asList(arg); for (T t : foo) { System.out.println(t); } return null; } How do I make sure that the arguments received are of only 1 type. For example, {1,'a',3} should be invalid. ...

Constrain generic extension method to base types and string

I want to have an extension method for XElement/XAttribute that allows me to apply a "ValueOrDefault" logic - perhaps with various slightly different implementations: ValueOrNull, ValueOrDefault, NumericValueOrDefault (which validates if the value is numeric), but I want to constrain these methods so that they can only work with ValueTyp...

Different behavior of reflected generic delegates with and without debugger

Hello. We have encountered some strange things while calling reflected generic delegates. In some cases with attatched debuger we can make impossible call, while without debugger we cannot catch any exception and application fastfails. Here is the code: using System; using System.Windows.Forms; using System.Reflection; namespace Gener...

How online-game clients are able to exchange data through internet so fast?

Hello, Let's imagine really simple game... We have a labirinth and two players trying to find out exit in real time through internet. On every move game client should send player's coordinates to server and accept current coordinates of another client. How is it possible to make this exchange so fast (as all modern games do). Ok, we ...

c# Generic overloaded method dispatching ambiguous

Hello, I just hit a situation where a method dispatch was ambiguous and wondered if anyone could explain on what basis the compiler (.NET 4.0.30319) chooses what overload to call interface IfaceA { } interface IfaceB<T> { void Add(IfaceA a); T Add(T t); } class ConcreteA : IfaceA { } class abstract BaseClassB<T> : IfaceB<T>...

C# method generic params parameter bug?

Hey, I appears to me as though there is a bug/inconsistency in the C# compiler. This works fine (first method gets called): public void SomeMethod(string message, object data); public void SomeMethod(string message, params object[] data); // .... SomeMethod("woohoo", item); Yet this causes "The call is ambiguous be...

How can I get the directory (file path) separator in Perl?

In case of Java, we can get the path separator using System.getProperty("path.separator"); Is there a similar way in Perl? All I want to do is to find a dir, immediate sub directory. Say I am being given two arguments $a and $b; I am splitting the first one based on the path separator and joining it again except the last fragment and...

Creating a Generic List of a specified type

Hi I want to create a generic list - but I want to specify the type at runtime - is there a way I can do this? using reflection perhaps? Something like this... Public Shared Sub create(ByVal t As Type) Dim myList As New Generic.List(Of t) End Sub Thanks in advance James ...

Problem with casting x<char> to x<t>

Hey guys I cant manage with this code. The idea is to return default English alphabet in case of erroneous create method execution. Thanks. An idea to override explicit operator is good, but i cant imagine an implementation of casting. namespace trie { class AlphabetFactory<T> where T: IConvertible { pub...

Type contraint problem of C#

I meet a problem about type contraint of c# now. I wrote a pair of methods that can convert object to string and convert string to object. ex. static string ConvertToString(Type type, object val) { if (type == typeof(string)) return (string)val; if (type == typeof(int)) return val.ToString(); if (type.IsSubclassOf(typeof(CodeObject)...