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
1 answer
64 views

Type-safe number serialization-deserialization

I have written this code for serializing and deserializing integer and floating point numbers to/from vector/array of bytes. The aim of the code is to provide a simple interface to use, but make it as ...
GameRaccoon's user avatar
2 votes
0 answers
40 views

Reusable storage for array of objects V4

Here is a thirdfollow up on Reusable storage for array of objects, Reusable storage for array of objects V2 and Reusable storage for array of objects V3, taking into account the provided answers. The ...
Oersted's user avatar
  • 299
1 vote
1 answer
45 views

Reusable storage for array of objects V3

Here is a second follow up on Reusable storage for array of objects and Reusable storage for array of objects V2, taking into account the provided answers. The following code should be compliant at ...
Oersted's user avatar
  • 299
3 votes
1 answer
50 views

Reusable storage for array of objects V2

Here is a follow up on Reusable storage for array of objects, taking into account the provided answers. The following code should be compliant at least with gcc, <...
Oersted's user avatar
  • 299
4 votes
2 answers
371 views

Reusable storage for array of objects

My goal is to have a memory pool non-template class that is used to store arrays of objects. The same memory pool object must be reusable for a different array (difference size, different type and/or ...
Oersted's user avatar
  • 299
1 vote
0 answers
21 views

Return type-erased object to the caller, use typesafe cast in a subsequent call [closed]

Libraries might need to return "context" objects to the caller, and then require the same object in subsequent calls. In some cases, the user is not required to know what's the concrete type ...
irishaccent's user avatar
3 votes
1 answer
279 views

API client that builds a response over multiple lines

I am performing a REST API call and I have copied their example code where they show how to perform such an api call. Their example is written in Javascript not Typescript though. To adapt the code I ...
sev's user avatar
  • 183
2 votes
2 answers
175 views

Nop class which can get optimized away

I designed a class for debug builds which does not incur any overhead in release mode. A usecase is for example: I have a function and I want to count how often it is called. For this I could write ...
tommsch's user avatar
  • 243
3 votes
1 answer
82 views

C Typed Vector that implements type-safety, encapsulation and lazy initialization

C Typed Vector I wasn't happy with the various dynamic array / vector implementations in C, so I made my own. In general, I'm not the biggest fan of C-preprocessor madness, but alas for this ...
Julius's user avatar
  • 171
3 votes
1 answer
183 views

Implementation of a type-safe generic dynamic array in C

I'm new to C and was trying to write a generic dynamic array which is type safe. I'm not sure if I pulled it off in the best way possible though. dynarray.h: ...
name's user avatar
  • 133
3 votes
0 answers
149 views

Replacing type-check with Visitor in Immutable Event-Sourced Aggregate

In my previous question, one of the answers mentioned that I shouldn't be changing behavior based on the Event class The whole point of classing is that you can get result without having to decide ...
Zymus's user avatar
  • 187
2 votes
1 answer
321 views

Typescript type checking of hierarchical types

I want to create a hierarchy of Typescript interfaces because I think classes would be an overkill in my case. The main obstacle is checking if an object has a specific type. I solved it this way: <...
Tom's user avatar
  • 121
2 votes
2 answers
86 views

RSA wrapper python

...
Vorac's user avatar
  • 454
2 votes
2 answers
462 views

Address class in PHP 7.4 with types

About this exercise I found out there is a form of strict types in PHP 7.4, so I freshened up my PHP coding with this simple Address class. I did not use PHP since ...
Vlastimil Burián's user avatar
8 votes
2 answers
735 views

Binary to decimal (and back) converter in c++

I'm mainly looking for error checking / type safety advice. main.cpp ...
Nadpher's user avatar
  • 889
2 votes
1 answer
31 views

Type definitions for simple NPM package that generates OAuth 1.0a authorization header for Twitter API using native https nodejs module

I added type definitions to a simple npm package. I would love to get some feedback on ways to do better for the type definitions. The package exports a single function which returns a function that ...
MauricioRobayo's user avatar
3 votes
1 answer
236 views

Haskell-like function composition function

Much like this question I'm attempting to write in C++ something which can resemble Haskell's (.) function as much as possible, which is basically Chapter 1's ...
Enlico's user avatar
  • 547
-1 votes
1 answer
29 views

Am I using the same Typescript variable? [closed]

I am passing a in as a parameter and want to return b. This works, however I feel like it is wrong because I shouldn't have to ...
user10076130's user avatar
3 votes
1 answer
82 views

How to make it more Scala compatible?

When you write a code in python code people always ask to make it more pythonic. I am not sure if there is a term for scala or not. However, I am pretty sure my following code use one or two scala ...
Gaurang Shah's user avatar
3 votes
1 answer
114 views

Efficient, typesafe runtime selection between "compiler-checked" sets of static config constant

Motivation / Task An apparently trivial problem.... A CLI app tries to make use of box drawing characters. But depending on the stdout device this may not be appropriate so there is a ...
Oliver Schönrock's user avatar
3 votes
1 answer
109 views

Time provider library using type safe handler - like chrono CPP

I'm trying to develop a c++ library that provide two kind of times: UTC time(self explainitory) System time(time since the system has went on) I have looked into the ...
nonamer92's user avatar
2 votes
0 answers
24 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 ...
intercoder's user avatar
-1 votes
3 answers
61 views

Implement state eraser for NIST DRBG and other crypto primitives using macro

As a spare-time personal project, I'm implementing a set of symmetric-key cryptographic primitives. One thing I've left vacant for a long time, is the key/state eraser, which I decide to add right ...
DannyNiu's user avatar
  • 310
4 votes
1 answer
413 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...) ...
Julien Vernay's user avatar
4 votes
0 answers
262 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 ...
Rietty's user avatar
  • 375
3 votes
1 answer
69 views

Range validator for multiple types

I have an existing overloaded set of functions that I cannot change and need to be tested: ...
ordonezalex's user avatar
3 votes
1 answer
12k 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 ...
mrseanbaines's user avatar
5 votes
1 answer
139 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 (...
Mehdi Saffar's user avatar
3 votes
1 answer
1k 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 : ...
user8426627's user avatar
0 votes
1 answer
222 views

Potential type-safety issues on object parsing function in DXL

I have inherited the maintenance of a DXL script (for IBM Doors). In this, I came across various examples of stuff that make me scratch my head. Take this example: ...
Daemon Painter's user avatar
1 vote
1 answer
56 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 ...
I wrestled a bear once.'s user avatar
2 votes
1 answer
1k 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 ...
Natalie's user avatar
  • 85
19 votes
2 answers
1k 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 system,...
Der Kommissar's user avatar
5 votes
2 answers
600 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 ...
einpoklum's user avatar
  • 1,995
2 votes
1 answer
104 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 ...
Jose A's user avatar
  • 213
1 vote
2 answers
220 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 ...
Ananda's user avatar
  • 111
2 votes
0 answers
57 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 ...
CD86's user avatar
  • 173
4 votes
1 answer
118 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 ...
CD86's user avatar
  • 173
4 votes
2 answers
2k 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 ...
Tobias Hermann's user avatar
2 votes
1 answer
409 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 ...
Thomas Barrett's user avatar
3 votes
3 answers
187 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, ...
John Hascall's user avatar
8 votes
3 answers
1k 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 ...
Thomas Ward's user avatar
  • 2,518
1 vote
0 answers
104 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 ...
Fathy's user avatar
  • 111
4 votes
1 answer
891 views

Wrapping an IntPtr in a Struct for safer Interop

Consider the following C#/C interop scenario: ...
Riley G's user avatar
  • 41
1 vote
2 answers
75 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 ...
doubleOrt's user avatar
  • 263
0 votes
1 answer
1k views

Scala type matching map

I was working with some complicated generics system, and I found the need to make an abstraction for a typesafe mapping between K[T] and V[T] where T is the same for individual pairs. In the case ...
Phoenix's user avatar
  • 267
5 votes
1 answer
456 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 ...
gridDragon's user avatar
1 vote
1 answer
58 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. ...
Damian Nadales's user avatar
8 votes
1 answer
343 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 ...
Ignat Insarov's user avatar
4 votes
1 answer
105 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 ...
Willy Goat's user avatar