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
3
votes
2answers
47 views

Adding numbers of any type

I'm writing an interpreter, and I'm designing the abstract syntax tree currently. Right now, I'm trying to figure out a way to add (or do other operations with) two ...
1
vote
1answer
780 views

C++ conversion from std::string to arithmetic type using std::locale::classic()

I recently had a bug where extracting a decimal from a string failed due to locale settings. That is, some locales use a , as a decimal point, rather than a ...
3
votes
1answer
36 views

Robustness of this first difference function

Here is a first difference function I threw together for a quick check in another program: ...
5
votes
2answers
150 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....
3
votes
0answers
148 views

Deque class using typing module

I wanted to make a class for type hinting that the result of a function is a homogenous deque. ...
13
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 ...
3
votes
1answer
4k views

A small generic array in C

The generic arrays in C I found on the net used either of these methods: The preprocesser with #define and #include (a lot of ...
3
votes
1answer
138 views

Constrained type alias

What do you think about the following syntax for some very simple and intensively reusable validations? Does this: string name = (SomeText)"Hm…"; mean for you ...
3
votes
1answer
653 views

Type converter framework (v2)

This is the second version of my type converter framework. The the previous one can be found here: Type creator service & framework In this version I mostly implemented what @Dmitry Nogin ...
2
votes
2answers
129 views

Type creator service & framework

I needed a mechanism for creating types dynamicaly from strings and bytes etc. I tried really hard to use the .net's TypeConverter system as it seemed to be the ...
13
votes
0answers
202 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,...
2
votes
1answer
2k views

Simple type-safe and thread-safe Rust event system

I'm creating a relatively simple type-safe and thread-safe Rust event system. It is to be used with and within an IRC library I'm making, but should work just fine for other use-cases. It needs to be ...
4
votes
1answer
102 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 ...
1
vote
1answer
49 views

Type Storage Class

So I have designed a "type store" in C++ that can hold a single instance of any subclass of a defined type. Here is an example of how it would be used: ...
6
votes
2answers
1k 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 ...
5
votes
1answer
116 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 ...
3
votes
1answer
101 views

Creating a lightweight strongly-typed parameter infrastructure

I'm looking for some feedback on some code, designed to allow me to strongly-type parameters, which get passed to functions. I'm only really looking for comments on making my parameters more strongly ...
2
votes
2answers
504 views

Dict lookup with type conversion and fallback

Given the following working code: ...
21
votes
3answers
726 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 ...
4
votes
2answers
34k 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 ...
3
votes
2answers
218 views

C++-safe way to use using inside headers

I have this C++ 14 code in a project of mine. Enhedron/Util/Enum.h: ...
3
votes
1answer
51 views

Addition operators for length units

I'm writing some code that helps me deal with units better, by having a user defined type for things like Metres, Centimetres ...
3
votes
1answer
99 views

Constructing NewsArticle objects from a database table

This class seems to be able to represent the data stored in any possible database table: ...
3
votes
1answer
665 views

Check if JavaScript input is empty

Having a general utility function to see if the input is empty makes sense to me. Empty means different things depends on the type. I just spent ~5 minutes writing this one up, so I'm sure it's ...
2
votes
1answer
73 views

Type-safe denominations

I was interesting in having a way to convert between dollars and cents without getting into floating point numbers and losing type safety. It's very easy to get confused when you're mixing numbers ...
8
votes
2answers
708 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. ...
10
votes
1answer
992 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. ...
1
vote
1answer
29 views

A safer way to return a generic indicator object indicating failure

I'm writing a singly-linked list that implements the List interface. I decided going in that I'd try to implement every method as an exercise; even the optional ...
2
votes
0answers
46 views

Add 5 strong types to an object as properties

This is a small object that allows you to pass an obj and a property name using its helper functions to create a typed var that won't save a value to the property if it isn't the right type. This ...
21
votes
3answers
4k 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-...
19
votes
1answer
416 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 ...
5
votes
2answers
692 views

Asynchronous HTTP JSON request

For asynchronous requests, I use Alamofire. I have created one method for requesting async data. ...
5
votes
2answers
65 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 ...
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 ...
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 ...
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 ...
3
votes
1answer
3k views

Proper use of type_info in relation to mapping components to a type

I'm trying to map a components type to the component value itself in a map. The following code all works properly. My main ...
2
votes
4answers
3k views

C-string appender for buffers and strings

Is this C appender safe, and reasonably optimal? ...
6
votes
1answer
77 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 ...
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 ...
2
votes
0answers
86 views

Delegating which module's functions to use based on user input

My main concern with this code is this problem - given that this program could generate a prompt string from Bash, Zsh, Fish, etc., I would like to make this code easily extendable so people can add ...
15
votes
0answers
174 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 ...
3
votes
2answers
492 views

Removing asInstance[T] from scala code

I've written this scala code and I cannot work out how/if it is possible to remove the asInstance in the definition of class ...
3
votes
2answers
134 views

Enforcing Type with Boiler Plates

This is an implementation of Mat's List using by Pseudo-inheriting my Pythonic-List. Inheritance is not built into VBA, but it can be simulated by composing the inherited class and mimicking all it's ...
7
votes
2answers
8k 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 ...
4
votes
0answers
87 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 ...
3
votes
2answers
12k views

RESTEasy client that should use generics

I have written some code using RESTEasy to handle RESTful service calls and parse the response XML into relevant JAXB annotated classes. At the moment I have seperate methods for each return type: <...
7
votes
3answers
7k 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? ...
2
votes
1answer
158 views

Aggregating values of a path-dependent type

I have a type representing an immutable order which contains immutable order lines that belong to it. I want to use path-dependent types for this. Obviously you cannot instantiate a path-dependent ...
6
votes
2answers
1k views

Function object passing for a task scheduler

Here is my full implementation of a generic Functor-like class: ...