Questions tagged [type-safety]

Use this tag for reviews of code where type safety is a concern, for example code involving type conversions or casting which may result in type errors at run-time.

Filter by
Sorted by
Tagged with
44
votes
3answers
11k views

List<T> implementation for VB6/VBA

Recently I decided VB6's Collection wasn't enough for my needs, so I decided to implement something like C#'s List<T>. ...
27
votes
3answers
1k views

A Failable<T> that allows safe returning of exceptions

This was inspired by a conversation in chat, that started with the discussion of C#7.0 tuples and out parameter declarations, which led to the idea that there is no 'good1' way to return an error ...
22
votes
4answers
37k views

Type-safe Dictionary for various types

Assume the following situation: you have an object that can store any object based on a key (basically, IDictionary<string, object>). You want to store ...
19
votes
1answer
364 views

Suggesting “safe enum” class for ISO C

There are quite some questions and answers about how to make enum really type-safe and I didn't find a solution that ensures both type safety and valid values. So I ...
17
votes
2answers
854 views

Enforcing string validity with the C# type system

One of my biggest complaints about .NET is that there's no way to guarantee a string match a certain type in the type-system. Note This started as more of a proof-of-concept than a real usable ...
16
votes
3answers
514 views

Type-safe Date class with total encapsulation

I want to teach about "encapsulation" and chose Date with Year, Month and ...
16
votes
2answers
452 views

Formatted print without the need to specify type matching specifiers using _Generic

This code allows printing in C with a style similar to C++ ostream. By using _Generic() (see ...
14
votes
1answer
2k views

C++ FlagSet: typesafe usage of enumeration as bitset/bitmask

Below is my attempt at providing a type-safe way to combine enumeration and bitset together. The resulting class is intended to be use as a set of flags, where each flag is a member of the "user-...
13
votes
1answer
2k views

Compile-time printf-style format checking

Inspired by this open ticket on Boost, this seeks to complete the work there. Given a printf-style format string and associated arguments, a static_assert is ...
13
votes
0answers
152 views

Encoding the problem domain of products and prices into the typesystem in F#

I'm trying to design a model for purchasing amounts of products in F#, following the ideas for designing for correctness (source). The idea is to use the type system to capture the different legal ...
12
votes
4answers
3k views

Type system for different representations of angle value

I want to implement a Type system for different representations of an angle value. Motivation to implement this as a type system comes from this question. Angle can be represented using the ...
12
votes
1answer
2k views

C++ identity function

I've implemented an identity function (well, actually a functor struct) in C++. The goal is that every occurrence of an ...
11
votes
2answers
904 views

Nullable<T> Implementation for VB6/VBA

Because I was spoiled with C# and the .NET framework, whenever I have to work with VB6 I feel like something's missing in the language. A little while ago I implemented a ...
10
votes
1answer
87 views

Methods creating transform functionality on Collections

I have written a few simple-to-be-used methods in Java 8, and am wondering what could be improved upon those: ...
10
votes
1answer
857 views

Attempt at type-safe enums in JavaScript

I wrote this tiny library yesterday. The goal was to implement enums in JavaScript with type-safety. I modeled the implementation similar to enums in Java, since that is what I am most familiar with. ...
10
votes
0answers
171 views

Multi-function memoizator and multi-type container

I'm trying to write a multiple functions memoizator, which I've talked about here. The main problem is creating a container containing different and heterogenous functions. I found a working solution,...
8
votes
3answers
705 views

'StrictInt' Python object class that prohibits casting numbers with non-integer components to `int`

Inspired by and created because of python3 utility: convert safely to int and driven partly by the 'refactor' in my answer. The aforementioned question and my answer drove me to go and do a thing ...
8
votes
1answer
272 views

Embedded conditional code compaction

I'm porting some AVR code from PROGMEM/PGM_P to __flash, and I want to reduce the amount of ...
7
votes
3answers
5k views

Simplifying a series of type checks and casts in a generic method

The if/else statements below are not good. How can I improve this method? ...
7
votes
2answers
7k views

Traversing and printing complex Dictionary types (Scripting.Dictionary) - Dictionary inside of a Dictionary

Based on this SO post - also reposted on vba4all.com with a few more details and explanations. Please notice there currently is no error handling whatsoever as I didn't analyse and consider any traps ...
7
votes
2answers
14k views

Pattern for storing object of varying type

I have a situation where I need to store fields of differing types of some data structures along with some similar metadata (The application takes data from one of many sources, some data possibly ...
7
votes
2answers
354 views

Revisited IsTypeSafe method implementation for “type-safe” List

Following up on List<T> implementation for VB6/VBA, I'd like some thoughts about the revisited IsTypeSafe function, below. The previous version pretty much ...
7
votes
2answers
1k views

Implementing both poor-man's MVP and MVPVM framework

I'm actually building some kind of framework to promote code reuse without over-reusing it. Besides, sometimes as it occurs with generic types, one expects type parameters to be constrained for the ...
7
votes
1answer
212 views

A type for prime numbers

I have recently discovered that in type theory there is a concept of a "predicate type" which is a type \$A\$ formed out of all members of the underlying type \$U\$ that satisfy a given predicate ...
7
votes
2answers
546 views

Strict types decorator (works only with Python 3.5)

I wrote a decorator which makes Python 3.5 raise exceptions if the arguments that are passed to a type-hinted function are of the wrong type. ...
6
votes
2answers
984 views

Type-Safe Event System

I have recently started writing C# from a strong JavaScript background, and found myself wanting to do something I do all the time in JavaScript: events. Since I have a type system at my disposal, I ...
6
votes
1answer
3k views

Validating a list of dictionaries of names and tags

I created a Python function to check the types from a list and also check the keys inside an dictionary within that list. I have the following data: ...
6
votes
1answer
73 views

Designing a Variable Set

In my application, I need to allow the user to store variables. Variables can only be of specific types, but I effectively handle all variables the same way no matter the type. I would also like to ...
6
votes
2answers
994 views

Function object passing for a task scheduler

Here is my full implementation of a generic Functor-like class: ...
6
votes
2answers
1k views

Type safe program uniform manipulation in OpenGL

I've made an attempt at writing class wrappers around basic OpenGL objects to make managing them easier and more intuitive. Writing a generic one for program uniforms proved to require a little bit ...
6
votes
0answers
188 views

Value semantic type eraser

I've been using std::unique_ptr for cases where I want to store a derived-type value with type erasure, and felt that unique_ptr being nullable was an undesirable ...
5
votes
2answers
661 views

Asynchronous HTTP JSON request

For asynchronous requests, I use Alamofire. I have created one method for requesting async data. ...
5
votes
2answers
118 views

add_const or remove_const based on another type

Here is a type trait which - I feel - could fit in std next to std::add_const and ...
5
votes
2answers
60 views

Getting a set of subscribers from a subscriberMap

In my Java event bus project, I have a private generic method that retrieves a Set<Subscriber<E>> from a private ...
5
votes
1answer
204 views

Improving General Algebraic DataType type-safe code for AA trees

As an exercise on GADTs I wrote a type-safe implementation of AA trees. I'm quite happy that the AANode data type correctly grasps the properties of the tree. Just ...
5
votes
1answer
112 views

Safe, fully standards-following integer abs in C

Here's a bit tricky one, because AFAIK there is no existing platform, where conditionally compiled part of the code would actually get included by compiler, so it is entirely up to the human review to ...
5
votes
1answer
334 views

Simplified encryption API wrapper for RSA and AES

I'm working on an encryption library to simplify how my team uses encryption for api credentials and receiving encrypted messages from other teams/projects. This code is wrapping java.security and ...
5
votes
1answer
2k views

Compile-time printf format checking

Compile time checking of printf-like format strings Inspired by this open ticket on boost, this seeks to complete the work there Given a printf-style format string and associated arguments, a ...
5
votes
1answer
35 views

Typescript monad for data being loaded

I have been struggling to make typescript happy about the types. I am pulling data from our backend API and I wanted to give context to the data. Basically it is a monad with 4 shapes: Initial (...
4
votes
2answers
26k views

Linq-to-Sql Contains an int? inside a list of int

Curious if there is a better way to write a repository method that receives argument List<int> and checks if the database ...
4
votes
2answers
2k views

Portable C++98 thread class akin to std::thread

I want a homegrown version of C++11's std::thread. My motivation is to avoid manual memory management of the arguments passed to the thread function. I'm restricted ...
4
votes
1answer
384 views

Wrapping an IntPtr in a Struct for safer Interop

Consider the following C#/C interop scenario: ...
4
votes
2answers
144 views

TextBoxes for various number types

Is there a way I could somehow have all this classes condensed into one, while keeping the code as clean as possible? They are all doing essentially the same thing, just for the different number type....
4
votes
1answer
71 views

SI type safe unit calculations

I wrote a small type-rich MKS Unit system for the consistent and safe calculation of physical units in everyday use. I realized some operators' implementations via the Barton-Nackman trick while ...
4
votes
2answers
643 views

Type-safe JSON deserialization

In a Python (3.6) application I receive messages from Kafka in JSON format. (The code base makes heavy use of static type annotations, and every file is automatically checked using ...
4
votes
1answer
69 views

Convertible templated math vector

I've made a templated math vector struct with a templated type and templated dimension count. I want my vectors to be convertible so I can easily make, for example, a ...
4
votes
1answer
45 views

Single-thread Task Scheduler with type erasure and allocator-aware

I made a task scheduler to practice allocators and type erasure. With my project, you can delay execution of any callable (functions (using std::ref), lambdas...) ...
4
votes
1answer
98 views

A class to encapsulate a pair of ints with range-checking

The goal is to have a helper class to wrap the concept of a resolution safely, encapsulating the range check into the class, since otherwise it can get lost by a client not so cautious. I think this ...
4
votes
0answers
70 views

Type & Thread-safe Logger Implementation

So I had previously posted this question and asked for some feedback regarding my (now) header-only logging system and figured it would be a good time to go back to the changes I made it and see if it ...
4
votes
0answers
83 views

Existential Types and IO

In my attempts to grasp Existential Types in Haskell I decided to implement an integer-based fixed-length vector data type. I'm using ghc 7.8.3. Specifically I wanted to write a program which asks ...