We’re rewarding the question askers & reputations are being recalculated! Read more.

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
4
votes
0answers
3k views

Event system using callback functions in C++

I am learning C++ and have been trying to create an event system for use in a small game. This will be the mechanism by which game entities communicate. I would be eternally grateful if someone with ...
3
votes
2answers
1k views

“Hell Difficulty” Haskell Fast & Hard

I'm a complete neophyte to Haskell with only a smidge of FP experience – I did some Racket programming academically and have written some semi-FP JavaScript professionally, but now I'm trying to learn ...
3
votes
1answer
657 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 ...
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: <...
3
votes
2answers
209 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
141 views

Area under curve

The following code is a solution to a Hackerrank problem in Haskell. Given a list of polynomial coeficients a and exponents b, ...
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 ...
3
votes
3answers
559 views

An attempt at a simple type safe python enum

There have been many posts here about enums in python, but none seem to be both type safe and simple. Here is my attempt at it. Please let me know if you see anything obviously wrong with it. ...
3
votes
3answers
123 views

Safely & portably read a value into a predefined integral type of varying size like mode_t

I need to read a value from a text file and put it into a predefined type, in this particular case mode_t. This typedef can have a variety of types underlying it, ...
3
votes
2answers
46 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 ...
3
votes
1answer
50 views

Python simple type checking decorator

I'd like to know your opinions on this minimal type-checking decorator (with @ annotations) to make type checking of a method while debugging like : ...
3
votes
2answers
470 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
119 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 ...
3
votes
1answer
53 views

Range validator for multiple types

I have an existing overloaded set of functions that I cannot change and need to be tested: ...
3
votes
1answer
2k views

All combinations of 2-value variables

Introduction This code was part of one of my projects where it was used to generate boolean input values for truth tables. I have since generalized it for combinations of variables with only 2 values,...
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: ...
3
votes
1answer
2k 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
134 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
98 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 ...
3
votes
1answer
97 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
1k views

Loading an Object from File with Type-Safety and Thread-Safe access

I'm attempting to write a bit of code that allows for easy and safe access to objects in a file. It seems to work great but I was curious if there was an easier way to do this or if Java already has ...
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
324 views

Pythonic type coercion from input [closed]

My context is a very simple filter-like program that streams lines from an input file into an output file, filtering out lines the user (well, me) doesn't want. The filter is rather easy, simply ...
3
votes
0answers
134 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. ...
2
votes
4answers
2k views

C-string appender for buffers and strings

Is this C appender safe, and reasonably optimal? ...
2
votes
2answers
376 views

Dict lookup with type conversion and fallback

Given the following working code: ...
2
votes
1answer
2k views

TypeScript implicit 'any' type with computed property key

(irrelevant code removed for demo purposes) I am new to using TypeScript and I have this React component which has an object literal inside the component, which I'm then accessing using a computed ...
2
votes
2answers
126 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 ...
2
votes
1answer
1k 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 ...
2
votes
1answer
279 views

Login system with password reset sent to e-mail using PHP and PDO

I really like to use PDO because it's simple and easy to make a safe query, i prepared all queries and used placeholders, i think it's safe but i'm not sure at all, and I'm thinking if i used ...
2
votes
1answer
531 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
1answer
50 views

TypeScript 3 form validation object

Please, correct me with anything that I say in here (The actual question is below the code). I've been quickly prototyping a project (ASP.NET Core 2.1) and haven't found the need to structure it with ...
2
votes
1answer
103 views

Detect single-character xor (OCaml)

I have been working through the Cryptopals challenges in Ocaml. My interest in Ocaml is to better learn functional programming techniques and effective usage of the type system. Set 1 Challenge 4 ...
2
votes
1answer
69 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 ...
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 ...
2
votes
0answers
22 views

Returns either an object containing two values or an exception if values or not a valid range

I have two functions, one that returns two values min and a max and another function that serves as a guard. However I would ...
2
votes
0answers
47 views

SI type safe unit calculations (revised)

This is the continuation of the questions raised in this thread I did include the improvements that were mentioned but still feel like, I do more copies than I need to. Also I am unsure about the ...
2
votes
0answers
45 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 ...
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 ...
1
vote
2answers
86 views

Function to map letters to numbers

I have a method that converts characters to numbers. It expects a single alphabetical character and returns the equivalent number. For example, if A is provided it returns 1, for B it returns 2. If an ...
1
vote
2answers
65 views

Function that checks if array only contains elements of a specific type

The function checks whether an array is of a specific type (only contains elements of that type), the types the function accepts for checking is basically all the JavaScript types plus any types that ...
1
vote
3answers
290 views

Can I somehow tidy up this (overuse?) of generics?

I'm building a generic flat file reader which looks something like this. ...
1
vote
1answer
42 views

Generating Image & Serving via HTTP with Go

What is does I'm reading The Go Programming Language. Below is my code for the last exercise in the first chapter, which is to create a webserver, generate an image, and serve the image via the ...
1
vote
1answer
52 views

Functions that build a path to the model and command files for some tests

I'm currently working in a model-based testing tool. I'm writing some integration tests for it, which require two kind of input: Model files: which contain models to be parsed and run by the tool. ...
1
vote
1answer
530 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 ...
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: ...
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 ...
1
vote
0answers
42 views

typeOf in Python [closed]

I've been working in statically typed languages for awhile, and I've had to write some Python utilities recently. As part of those tools, I needed a semi-sane way to implement type checking to make it ...
1
vote
0answers
69 views

Redux-like implementation in TypeScript

I need to write a custom reducer utility. It should take a dictionary of reducers and a publish subject to return a state, with a dispatch method to dispatch ...
1
vote
0answers
33 views

Type conversion between Node, Const_node, and their views

Each node is a location within a layer. A Location may be a pointer or an index. A Layer is a container. There are 4 types of ...