Questions tagged [type-safety]

Type safety is the extent to which a language discourages using variables in an unsafe manner, according to the variables' type.

686 questions
594
votes
9 answers
341
votes
13 answers

What is Type-safe?

What does "type-safe" mean?
327
votes
10 answers

Type safety: Unchecked cast

In my spring application context file, I have something like:
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
275
votes
11 answers

Generic type conversion FROM string

I have a class that I want to use to store "properties" for another class. These properties simply have a name and a value. Ideally, what I would like is to be able to add typed properties, so that the "value" returned is always of the type that I…
Rob Cooper
  • 28,567
  • 26
  • 103
  • 142
119
votes
17 answers

Discriminated union in C#

[Note: This question had the original title "C (ish) style union in C#" but as Jeff's comment informed me, apparently this structure is called a 'discriminated union'] Excuse the verbosity of this question. There are a couple of similar sounding…
Chris Fewtrell
  • 7,555
  • 8
  • 45
  • 63
117
votes
6 answers

The new keyword "auto"; When should it be used to declare a variable type?

Possible Duplicate: How much is too much with C++0x auto keyword Have we (as a community) had enough experience to determine when and/or whether auto is being abused? What I am really looking for is a best practices guide on when to use…
Martin York
  • 257,169
  • 86
  • 333
  • 562
86
votes
14 answers

In C#, why can't a List object be stored in a List variable
It seems that a List object cannot be stored in a List variable in C#, and can't even be explicitly cast that way. List sl = new List(); List ol; ol = sl; results in Cannot implicitly convert type…
Matt Sheppard
  • 116,545
  • 46
  • 111
  • 131
81
votes
1 answer

What is the meaning of the type safety warning in certain Java generics casts?

What is the meaning of the Java warning? Type safety: The cast from Object to List is actually checking against the erased type List I get this warning when I try to cast an Object to a type with generic information, such as in the…
Mike Stone
  • 44,224
  • 30
  • 113
  • 140
73
votes
3 answers

Template typedefs - What's your work around?

C++ 0x has template aliases (sometimes referred to as template typedefs). See here. Current spec of C++ does not. What do you like to use as work around ? Container objects or Macros ? Do you feel its worth it ?
George Godik
  • 1,716
  • 1
  • 14
  • 19
72
votes
9 answers

Generic type checking

Is there a way to enforce/limit the types that are passed to primitives? (bool, int, string, etc.) Now, I know you can limit the generic type parameter to a type or interface implementation via the where clause. However, this doesn't fit the bill…
Rob Cooper
  • 28,567
  • 26
  • 103
  • 142
71
votes
8 answers

How to make Databinding type safe and support refactoring?

When I wish to bind a control to a property of my object, I have to provide the name of the property as a string. This is not very good because: If the property is removed or renamed, then I don’t get a compiler warning. If a rename the property…
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
71
votes
8 answers

Why is C++ allowing me to assign a const char to a const char *?​!

To my astonishment, this compiles: const char* c_str() { static const char nullchar = '\0'; return nullchar; } and it introduced a bug in my code. Thankfully, I caught it. Is this intentional by C++, or a compiler bug? Is there a reason why…
user541686
  • 205,094
  • 128
  • 528
  • 886
60
votes
4 answers

How to create type safe enums?

To achieve type safety with enums in C is problematic, since they are essentially just integers. And enumeration constants are in fact defined to be of type int by the standard. To achieve a bit of type safety I do tricks with pointers like…
Lundin
  • 195,001
  • 40
  • 254
  • 396
59
votes
2 answers

Why does the Swift language guide suggest using Int "even when values are known to be non-negative"?

This is a question about programming style in Swift, specifically Int vs UInt. The Swift Programming Language Guide advises programmers to use the generic signed integer type Int even when variables are known to be non-negative. From the guide: Use…
j b
  • 5,147
  • 5
  • 41
  • 60
59
votes
10 answers

Type safe physics operations in C++

Does it make sens in C++ to define physics units as separate types and define valid operations between those types? Is there any advantage in introducing a lot of types and a lot of operator overloading instead of using just plain floating point…
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
1
2 3
45 46