Skip to main content

Questions tagged [type-safety]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2 votes
0 answers
64 views

How to deal with implementation specific implementation accross interface boundaries

What to do if you end up in the following situation The intent here is that b and c can be substitutable for a. The client creates a bar through foo_1, which later may be passed back to foo_1 through ...
user877329's user avatar
1 vote
1 answer
78 views

Is there a way to introduce some basic type safety around IntPtrs returned from PInvoke'd methods?

I have a C-Style API with methods that return pointers to native objects, e.g. EXPORT hs::Pose3d* getJointEndPose(dh::Joint* joint); And in C#, [DllImport("Solver.dll")] public static ...
sebf's user avatar
  • 285
1 vote
1 answer
243 views

Compile-time type-safe access to collection with polymorphic items, and extending it

I would like to have type-safe access to items in a collection with polymorphic items at compile-time. For this, I set up the code below as a proof of concept. Passing the DataItemProps instances to ...
Cédric Moers's user avatar
2 votes
1 answer
623 views

How could Vulkan pNext design be implemented in a "safer" way?

Vulkan introduces a member .pNext of type void* in all its core structs allowing to create handles. This member purpose is to allow to extend the structure by passing a pointer another one. This is ...
Erel's user avatar
  • 131
0 votes
2 answers
2k views

Should I use a class with only static members to encapsulate my program?

So I'm writing a network simulator in C++ as part of a university project. Right now, I have a class structure that looks something like: //includes type aliases #include "GlobalTypes.h" //main body ...
Connor Carr's user avatar
13 votes
3 answers
472 views

Using static type checking to protect against business errors

I'm big a fan of static type checking. It prevents you from making stupid mistakes like this: // java code Adult a = new Adult(); a.setAge("Roger"); //static type checker would complain a.setName(42)...
J-bob's user avatar
  • 299
1 vote
1 answer
478 views

Type safety and runtime checking of type

I was mildly surprised to find this function in our code base: public static double ToDouble( object value ) { if ( value is double ) { return (double)value; } else if ( value is ...
Anton Shepelev's user avatar
4 votes
1 answer
2k views

Should opaque pointers be pointers or types?

A common way to implement "PIMPL" in C is to do this: typedef struct _Opaque Opaque; Opaque* createOpaque(); void doSomething(Opaque *opaque); Or: typedef struct _Opaque* Opaque; Opaque ...
user112513312's user avatar
5 votes
2 answers
4k views

Type safety - GO vs C pointers

C is a static-typed language that is not type-safe, because pointers(void *y) let you do pretty much anything you like, even things that will crash your program. GO is also a static typed language ...
overexchange's user avatar
  • 2,285
6 votes
2 answers
6k views

Can an interpreted statically typed language be considered type safe?

First let me explain what is my understanding of the terms statically typed language and type safety: Statically typed language: a language that does not allow you to change the type of a variable at ...
user247763's user avatar
4 votes
3 answers
168 views

How do I model this relationship so that it is valid by construction?

Imagine a device class that represents a physical real world mobile device, with fields like Enabled, Platform, Model IMEI, etc. Then, an operation class, which is something that needs to be done to a ...
julealgon's user avatar
  • 287
30 votes
3 answers
3k views

Why is XML type safe?

Why do they say that XML provides type safety and how is it expressed in the XML itself? How is it different from JSON (for example) which (as I understand) is not type safe?
user3339411's user avatar
21 votes
2 answers
2k views

Is it possible to "bake dimension into a type" in haskell?

Suppose I want to write a library that deals with vectors and matrices. Is it possible to bake the dimensions into the types, so that operations of incompatible dimensions generate an error at compile ...
mitchus's user avatar
  • 321
10 votes
2 answers
3k views

Why can't the Scala compiler give pattern matching warning for nonsealed classes/traits?

If I use an unsealed trait or abstract class in Scala and then use pattern matching, I wonder, does the compiler not know at compile time for this particular patternmatch what possible implementations ...
valenterry's user avatar
  • 2,429
9 votes
5 answers
12k views

Why do we have to tell printf() the type of data in C?

Let's consider this C code: #include <stdio.h> main() { int x=5; printf("x is "); printf("%d",5); } In this, when we wrote int x=5; we told the computer that x is an integer. The ...
user106313's user avatar
3 votes
2 answers
661 views

Parameterized Java Types (Generics)

Consider this "legacy" code: public interface IPersistentCollection { IPersistentCollection cons(Object o); } Genericized in Java, it could become something like this: public interface ...
GlenPeterson's user avatar
  • 14.9k
2 votes
1 answer
805 views

How to define different names for the same type and have the compiler check them? [duplicate]

I would like to define several names for the same type and have the compiler distinguish between them. My motivation is that different int variables could represent very different units, and I would ...
Vorac's user avatar
  • 7,129
53 votes
3 answers
27k views

C++ strongly typed typedef

I've been trying to think of a way of declaring strongly typed typedefs, to catch a certain class of bugs in the compilation stage. It's often the case that I'll typedef an int into several types of ...
Kian's user avatar
  • 633
4 votes
1 answer
1k views

What is the evidence that an API has exceeded its orthogonality in the context of types?

Wikipedia defines software orthogonality as: orthogonality in a programming language means that a relatively small set of primitive constructs can be combined in a relatively small number of ways ...
hawkeye's user avatar
  • 4,829
0 votes
1 answer
1k views

Can JSP Expression Language provide type safety like scriptlets in Eclipse IDE?

Almost everywhere you can read that you should use EL instead of scriptles in JSP. And I agree that you should not use any JAVA code in JSP except for calling getter methods on model objects. This is ...
Kacper86's user avatar
  • 527
7 votes
2 answers
3k views

Should I feel "uncomfortable" using auto in C++? [duplicate]

C++11 features the new auto type declaration, allowing you to work with an object without ever knowing its actual type. I use only strongly typed languages - C++, Delphi (Object Pascal), Go, etc and ...
Vector's user avatar
  • 3,192
1 vote
1 answer
384 views

Type Safety of Spray.io

Yesterday I read a tweet disparaging Jersey with JAX-RS due to run-time errors. Spray.io was mentioned in the tweet: Not even 5 minutes using Jersey / JAX-RS and already a runtime error due to a ...
Kevin Meredith's user avatar
14 votes
1 answer
17k views

Changing method signature for implementing classes in PHP

Is there any decent work around to PHP's lack of Generics that allow static code inspection to detect type consistency? I have an abstract class, that I want to sub-class and also enforce that one of ...
Danack's user avatar
  • 345
49 votes
8 answers
9k views

What are the safety benefits of a type system?

In JavaScript: The Good Parts by Douglas Crockford, he mentions in his inheritance chapter, The other benefit of classical inheritance is that it includes the specification of a system of types. ...
MistakesWereMade's user avatar
6 votes
2 answers
344 views

Abstracting user interface from domain

My course text suggests creating a 'facade' class to completely abstract the user interface from the domain layer. That is, the UI uses only basic types, and if it needs a reference to a domain object,...
just me's user avatar
  • 653
105 votes
18 answers
68k views

Is it poor programming practice to pass parameters as Objects? [duplicate]

So, we've got a guy who likes to write methods that take Objects as parameters, so they can be 'very flexible.' Then, internally, he either does direct casting, reflection or method overloading to ...
17 votes
5 answers
4k views

Would it make sense to use objects (instead of primitive types) for everything in C++?

During a recent project I've been working on, I've had to use a lot of functions that kind of look like this: static bool getGPS(double plane_latitude, double plane_longitude, double plane_altitude, ...
zackg's user avatar
  • 291
17 votes
3 answers
21k views

Is Google's Go a type-safe language?

this page http://golang.org/doc/go_faq.html writes: although Go has static types the language attempts to make types feel lighter weight than in typical OO languages So my question is ...
Pacerier's user avatar
  • 4,993
116 votes
20 answers
45k views

Is static typing worth the trade-offs?

I began coding in in Python primarily where there is no type safety, then moved to C# and Java where there is. I found that I could work a bit more quickly and with less headaches in Python, but then ...
44 votes
4 answers
17k views

What arguments are there in favor of weak typing?

This came up in a discussion with a friend, and I found myself hard-pressed to think up an any good arguments. What benefits do weak typing confer?
Fishtoaster's user avatar
  • 25.9k