r/ProgrammingLanguages • C3 - http://c3-lang.org • May 03 '26

Blog post Unsigned Sizes: A Five Year Mistake

https://c3-lang.org/blog/unsigned-sizes-a-five-year-mistake/
91 Upvotes

115 comments sorted by

View all comments

Show parent comment

3

u/Guvante May 03 '26

Constants tend to be the major problem for dropping implicit conversions.

What is the type of 0?

Sure you can force it to be a single type but that introduces its own problems.

The simplest solve is roughly "integers are the smallest type that fits" and "up casting that is perfectly safe is allowed" aka uint8 -> uint16 or even int16 is allowed since it is always safe.

But now you have implicit conversions...

11

u/mocompute May 04 '26

In my type-inferred language, integer literals like 0 are weak types until they are found in a concrete context and assigned a type. If all contexts are unconstrained, it just defaults to a signed integer. And there are no unsafe implicit conversions in the language, only the safe ones like you suggested.

I don't think there's a problem in allowing safe implicit conversions, but disallowing unsafe implicit conversions.

5

u/gmes78 May 04 '26

That's what Rust does, too.

You can even see it in error messages sometimes, the type shows up as {integer} if it hasn't been inferred yet.

2

u/flatfinger May 07 '26

Treating integers as weak types is generally good, but leaves open ambiguity as to how something like int32a = int32b*100/33; should be processed on a platform where computations with a 32-bit product/dividend would be much faster than with a 64-bit one. My personal preference would probably be to allow programmers to request that such constructs either implicitly use 64 bits or trigger a diagnostic, and have a convenient means of writing the expression that allow compilers to either use an overflow-trapped 32-bit multiply, or use a 64-bit multiply and divide and range-check the quotient, silently yielding correct results in cases where the multiply would overflow.

3

u/jonathancast globalscript May 04 '26

C and C++ already have typed integer constants: 0U, 0L, etc.

Probably needs explicit size_t and ssize_t versions (and explicit size versions? E.g., an explicit 64- or 128-bit version) but it shows the issue is not unsolvable.

(I know about this because Java allows implicit conversion of int to long but does not allow implicit conversion of int to Long. I guess they reconsidered (even widening) implicit conversions at some point.)

3

u/Lurkernomoreisay May 04 '26

c++ 23 added 0z and 0uz for ssize_t and size_t

2

u/mocompute May 04 '26

So, I also added types integer constants with suffixes, but it turned out practically that it wasn't needed, because inference took care of things. However, when I want a size_t explicitly, I usually prefer to add a type annotation to the declaration, because it's more visible.

x: CSize := 0 is equivalent to x := 0zu so I guess it's a matter of taste.

-1

u/Guvante May 04 '26

Making the user do the work isn't a solve it is a workaround.

Yes it is less ugly than static_cast<unsigned long long>(0); but given how that is almost always compiled out it sucks that it takes non zero effort.

1

u/stone_henge May 04 '26 edited May 04 '26

In Zig, an integer literal is an arbitrarily sized and signed comptime_int until, during compilation, you say otherwise, at which point literals with incompatible signedness or value become a compile-time error.

1

u/CompleteBoron May 04 '26

You a word

2

u/stone_henge May 04 '26

My predecessor was assassinated mid-sentence. I am the /u/stone_henge now.

1

u/Guvante May 04 '26

More trade offs though as now Foo(0) is different than the same function inlined.

Trade offs are fine just noting that sometimes making the thing that is obvious work that way is a bit of work.

1

u/stone_henge May 04 '26

More trade offs though as now Foo(0) is different than the same function inlined.

No, functions in Zig have return types, which prevents that from making any difference whether the calls are inlined or actual calls. Maybe I don't get your point. Can you elaborate with an example?

1

u/Guvante May 04 '26

With implicit conversions it shouldn't matter. Likely it involves type inference stacking which usually breaks down in some way when you write it down...

Something akin to template<class T> T suffix(T x) { return x + 1; } where suffix(0) is a specific but 0 + 1 is more flexibly typed.

But as you noted this is due to explicitly being typed as the language can choose a more general type here if that is usually better.