Search Results

Search found 8 results on 1 pages for 'typesafety'.

Page 1/1 | 1 

  • "Overriding" instance variables in subtype: Possible risks?

    - by sebastiangeiger
    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 it is convenient because I can get someList.size() in Superclass and have Typesafety in the Subclasses. The problem is that it does not "feel" right, can you think of potential hazards this apporach has that I am not aware of?

    Read the article

  • Java Generics Type Safety warning with recursive Hashmap

    - by GC
    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 that must be suppressed foo(entry.getKey(), (HashMap<String, Object>)entry.getValue()); } } I know for sure Object is of type Hashmap<String, Object> but am irritated that I have to suppress the warning using @SuppressWarnings("unchecked"). I'll be satisfied with a solution that does either a assert(/*entry.getValue() is of type HashMap<String, Object>*/) or throws an exception when it isn't. I went down the Generics route for compile type safety and if I suppress the warning then it defeats the purpose. Thank you for your comments, ksb

    Read the article

  • Better type safety in Java collections

    - by Paul Tomblin
    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 make new classes CapabilityId extends String and GroupId extends String, but I can't. Is there a better way to keep track of which thing is the key and maybe have the compiler enforce it?

    Read the article

  • Converting a pointer for a base class into an inherited class

    - by Shawn B
    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 code I am using to pick up an item is this, it is when a different entity picks up an item it is standing over. void XEntity::PickupItem() { XEntity *Ent = MapList; // Start of a linked list while(true) { if(Ent == NULL) { break; } if(Ent->Flags & ENT_ITEM) { Ent->RemoveEntity(); // Unlink from the map's linked list XItem *Item = Ent // Problem is here, type-safety // Code to link into inventory is here break; } Ent = Ent->MapList; } } My first thought was to create a method in XEntity that returns itself as an XItem pointer, but it creates circular dependencies that are unresolvable. I'm pretty stumped about this one. Any help is greatly appreciated.

    Read the article

  • Feedback on iterating over type-safe enums

    - by Sumant
    In response to the earlier SO question "Enumerate over an enum in C++", I came up with the following reusable solution that uses type-safe enum idiom. I'm just curious to see the community feedback on my solution. This solution makes use of a static array, which is populated using type-safe enum objects before first use. Iteration over enums is then simply reduced to iteration over the array. I'm aware of the fact that this solution won't work if the enumerators are not strictly increasing. template<typename def, typename inner = typename def::type> class safe_enum : public def { typedef typename def::type type; inner val; static safe_enum array[def::end - def::begin]; static bool init; static void initialize() { if(!init) // use double checked locking in case of multi-threading. { unsigned int size = def::end - def::begin; for(unsigned int i = 0, j = def::begin; i < size; ++i, ++j) array[i] = static_cast<typename def::type>(j); init = true; } } public: safe_enum(type v = def::begin) : val(v) {} inner underlying() const { return val; } static safe_enum * begin() { initialize(); return array; } static safe_enum * end() { initialize(); return array + (def::end - def::begin); } bool operator == (const safe_enum & s) const { return this->val == s.val; } bool operator != (const safe_enum & s) const { return this->val != s.val; } bool operator < (const safe_enum & s) const { return this->val < s.val; } bool operator <= (const safe_enum & s) const { return this->val <= s.val; } bool operator > (const safe_enum & s) const { return this->val > s.val; } bool operator >= (const safe_enum & s) const { return this->val >= s.val; } }; template <typename def, typename inner> safe_enum<def, inner> safe_enum<def, inner>::array[def::end - def::begin]; template <typename def, typename inner> bool safe_enum<def, inner>::init = false; struct color_def { enum type { begin, red = begin, green, blue, end }; }; typedef safe_enum<color_def> color; template <class Enum> void f(Enum e) { std::cout << static_cast<unsigned>(e.underlying()) << std::endl; } int main() { std::for_each(color::begin(), color::end(), &f<color>); color c = color::red; }

    Read the article

  • How to make Databinding type safe and support refactoring

    - by Ian Ringrose
    When I wish to bind a control to a property of my object, I have to provide the name of the property as a string. This is not very good because: If the property is removed or renamed, I don’t get a compiler warning. If a rename the property with a refactoring tool, it is likely the data binding will not be updated. I don’t get an error until runtime if the type of the property is wrong, e.g. binding an integer to a date chooser. Is there a design-paten that gets round this, but still has the ease of use of data-binding? (This is a problem in WinForm, Asp.net and WPF and most likely lots of other systems) I have now found "workarounds for nameof() operator in C#: typesafe databinding" that also has a good starting point for a solution.

    Read the article

  • what's Static type safety ?

    - by symfony
    Static type safety – an integral property of languages of the family to which C++ belongs and valuable both for guaranteeing properties of a design and for providing runtime and space efficiency. Can someone illustrate by a demo? Thanks

    Read the article

  • Using C# 4.0’s DynamicObject as a Stored Procedure Wrapper

    - by EltonStoneman
    [Source: http://geekswithblogs.net/EltonStoneman] Overview Ignoring the fashion, I still make a lot of use of DALs – typically when inheriting a codebase with an established database schema which is full of tried and trusted stored procedures. In the DAL a collection of base classes have all the scaffolding, so the usual pattern is to create a wrapper class for each stored procedure, giving typesafe access to parameter values and output. DAL calls then looks like instantiate wrapper-populate parameters-execute call:       using (var sp = new uspGetManagerEmployees())     {         sp.ManagerID = 16;         using (var reader = sp.Execute())         {             //map entities from the output         }     }   Or rolling it all into a fluent DAL call – which is nicer to read and implicitly disposes the resources:   This is fine, the wrapper classes are very simple to handwrite or generate. But as the codebase grows, you end up with a proliferation of very small wrapper classes: The wrappers don't add much other than encapsulating the stored procedure call and giving you typesafety for the parameters. With the dynamic extension in .NET 4.0 you have the option to build a single wrapper class, and get rid of the one-to-one stored procedure to wrapper class mapping. In the dynamic version, the call looks like this:       dynamic getUser = new DynamicSqlStoredProcedure("uspGetManagerEmployees", Database.AdventureWorks);     getUser.ManagerID = 16;       var employees = Fluently.Load<List<Employee>>()                             .With<EmployeeMap>()                             .From(getUser);   The important difference is that the ManagerId property doesn't exist in the DynamicSqlStoredProcedure class. Declaring the getUser object with the dynamic keyword allows you to dynamically add properties, and the DynamicSqlStoredProcedure class intercepts when properties are added and builds them as stored procedure parameters. When getUser.ManagerId = 16 is executed, the base class adds a parameter call (using the convention that parameter name is the property name prefixed by "@"), specifying the correct SQL Server data type (mapping it from the type of the value the property is set to), and setting the parameter value. Code Sample This is worked through in a sample project on github – Dynamic Stored Procedure Sample – which also includes a static version of the wrapper for comparison. (I'll upload this to the MSDN Code Gallery once my account has been resurrected). Points worth noting are: DynamicSP.Data – database-independent DAL that has all the data plumbing code. DynamicSP.Data.SqlServer – SQL Server DAL, thin layer on top of the generic DAL which adds SQL Server specific classes. Includes the DynamicSqlStoredProcedure base class. DynamicSqlStoredProcedure.TrySetMember. Invoked when a dynamic member is added. Assumes the property is a parameter named after the SP parameter name and infers the SqlDbType from the framework type. Adds a parameter to the internal stored procedure wrapper and sets its value. uspGetManagerEmployees – the static version of the wrapper. uspGetManagerEmployeesTest – test fixture which shows usage of the static and dynamic stored procedure wrappers. The sample uses stored procedures from the AdventureWorks database in the SQL Server 2008 Sample Databases. Discussion For this scenario, the dynamic option is very favourable. Assuming your DAL is itself wrapped by a higher layer, the stored procedure wrapper classes have very little reuse. Even if you're codegening the classes and test fixtures, it's still additional effort for very little value. The main consideration with dynamic classes is that the compiler ignores all the members you use, and evaluation only happens at runtime. In this case where scope is strictly limited that's not an issue – but you're relying on automated tests rather than the compiler to find errors, but that should just encourage better test coverage. Also you can codegen the dynamic calls at a higher level. Performance may be a consideration, as there is a first-time-use overhead when the dynamic members of an object are bound. For a single run, the dynamic wrapper took 0.2 seconds longer than the static wrapper. The framework does a good job of caching the effort though, so for 1,000 calls the dynamc version still only takes 0.2 seconds longer than the static: You don't get IntelliSense on dynamic objects, even for the declared members of the base class, and if you've been using class names as keys for configuration settings, you'll lose that option if you move to dynamics. The approach may make code more difficult to read, as you can't navigate through dynamic members, but you do still get full debugging support.     var employees = Fluently.Load<List<Employee>>()                             .With<EmployeeMap>()                             .From<uspGetManagerEmployees>                             (                                 i => i.ManagerID = 16,                                 x => x.Execute()                             );

    Read the article

1