type-safety

closures mean fully type-safe criteria?

combining closures (FCM) and generics, would it be possible to have fully type-safe criteria. // The following works without a cast as Foo.id is a 'long' field. List<Long> ids = session.createCriteria(Foo.class) .setProjection(Foo#id) .list(); // The following is a compilation error, as F...

Why I cannot derive from long?

Hi My function returns some long value which contains two values in lower and higher 32 bits. I thought the best ways to handle return value is to derive my custom type from long and provide type extenders like GetLowerValue(), GetHigherValue(). The problem is that .NET does not allow to derive from long If you compile that public c...

Type-safe alternative to HttpContext.Items

I am implementing an HTTP Module in ASP.NET to identify geographical information based on the request's IP (a GeoIP module) and I will need to place things somewhere so the handler or later modules can inspect. Except HttpContext.Items (which is not type-safe) is there some other decent alternative? ...

Working with Byte literals

I'm using the following function to brighten up color values (it's a lambda in my code, but that shouldn't make a differende): Function ReduceDistanceTo255(ByVal i As Byte) As Byte Return i + (255 - i) \ 2 End Function It won't compile, since the compiler interprets 255 and 2 as integers rather than bytes, making the result of typ...

Generics and sorting in Java

Suppose you write a static function in Java to sort an array, much like Arrays.sort(). The problem with Arrays.sort() is that it receives an array of Object, and throws a ClassCastException if its elements don't implement Comparable. So you want your function to receive as an argument an array of a subtype of Comparable. Something like ...

What is type safety and what are the "type safe" alternatives?

Possible Duplicates: What is Type-safe? What is type-safety? I was reading about c++ vectors and it was mentioned that memcpy and printf functions from C are not type safe. Article here: http://en.wikipedia.org/wiki/Vector_(C%2B%2B). Question: In simple English, what is type safety and what are the "type safe" alternatives? ...

Unsafe conversion

Is the following conversion safe? int b[10][10]; char *x; int a[]={0,1,2,3,4,5,6,7,8,9}; for(int i=0;i<10;i++) for(int j=0;j<10;j++) b[i][j]=a[i]; for(x=(char *)&b[0];x<=(char *)&b[9][9];x+=sizeof(a+1)) // Problem lies here! printf("%d\n",*x); I don't think the above conversion in the for loop is safe (I think it is platfo...

Generics in VB.NET

Now, as a C# programmer, I know that generics are awesome. However, when dabbling in some VB.NET, I discovered that the following does not cause a compiler error: Dim instance As List(Of Integer) instance.Add(True) Why is this? I know that you are not required to cast in VB.NET, but I'd have thought that this kills the main reason to...

Selector that can only take child elements of a certain type?

I need to write a control that is supposed to take only a certain type of child controls. Functionally it works like a Selector in that I can select/activate on of its childs but it seems that I can not derive from Selector because an ItemsControl can take any type of child (object). I really would like to have compiletime typesafety her...

Java Generics Type Safety warning with recursive Hashmap

Hi, I'm using a recursive tree of hashmaps, specifically Hashmap map where Object is a reference to another Hashmap and so on. This will be passed around a recursive algorithm: foo(String filename, Hashmap<String, Object> map) { //some stuff here for (Entry<String, Object> entry : map.entrySet()) { //type warning th...

Lucene's nested query evaluation regarding negation

Hi, I am adding Apache Lucene support to Querydsl (which offers type-safe queries for Java) and I am having problems understanding how Lucene evaluates queries especially regarding negation in nested queries. For instance the following two queries in my opinion are semantically the same, but only the first one returns results. +year:1...

Better type safety in Java collections

In my java coding, I often end up with several Map<String,Map<String,foo>> or Map<String,List<String>> and then I have trouble remembering which String is which key. I comment the declaration with //Map<capabiltyId,Map<groupId,foo>> or //Map<groupId,List<capabilityId>, but it's not the greatest solution. If String wasn't final, I would...

Converting a pointer for a base class into an inherited class

Hey, I'm working on a small roguelike game, and for any object/"thing" that is not a part of the map is based off an XEntity class. There are several classes that depend on it, such as XPlayer, XItem, and XMonster. My problem is, that I want to convert a pointer from XEntity to XItem when I know that an object is in item. The sample ...

[Java] Type safety: Unchecked cast from Object

Hi, I try to cast an object to my Action class, but it results in a warning: Type safety: Unchecked cast from Object to Action<ClientInterface> Action<ClientInterface> action = null; try { Object o = c.newInstance(); if (o instanceof Action<?>) { action = (Action<ClientInterface>) o; } else { // TODO 2 Auto-generated catch bloc...

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...

Java Builder pattern with Generic type bounds

Hi all, I'm attempting to create a class with many parameters, using a Builder pattern rather than telescoping constructors. I'm doing this in the way described by Joshua Bloch's Effective Java, having private constructor on the enclosing class, and a public static Builder class. The Builder class ensures the object is in a consistent...

Looking for a type-safe web development platform

What programming language + web framework combinations support static type-checking? I'm not scared of functional programming (I'd prefer it), however I'm looking for a mature framework with the bells and whistles we've come to expect. Obviously this includes security and efficiency concerns. EDIT: Just for informational purposes.... In...

"Overriding" instance variables in subtype: Possible risks?

Say I had a class SuperClass and two subclasses SubClassA and SubClassB that inherit from SuperClass. abstract class SuperClass{ ... List someList; ... } class SubClassA extends SuperClass{ ... List<String> someList; ... } class SubClassB extends SuperClass{ ... List<Integer> someList; ... } That way...

Return an opaque object to the caller without violating type-safety

I have a method which should return a snapshot of the current state, and another method which restores that state. public class MachineModel { public Snapshot CurrentSnapshot { get; } public void RestoreSnapshot (Snapshot saved) { /* etc */ }; } The state Snapshot class should be completely opaque to the caller--no visible met...

C++ STL: Trouble with iterators

I'm having a beginner problem: bool _isPalindrome(const string& str) { return _isPalindrome(str.begin(), str.end()); // won't compile } bool _isPalindrome(string::iterator begin, string::iterator end) { return begin == end || *begin == *end && _isPalindrome(++begin, --end); } What am I doing wrong here? Why doesn't str.begin(...