Request for comments: overloadable constants?

Here’s an interesting thought. With Practical’s expected type capabilities, it is possible to overload constants.

E.g.:

def FibonnacciHashMultiplier : U64 = U64_MAX / GoldenRatio;
def FibonnacciHashMultiplier : U32 = U32_MAX / GoldenRatio;
def FibonnacciHashMultiplier : U16 = U16_MAX / GoldenRatio;
def FibonnacciHashMultiplier : U8 = U8_MAX / GoldenRatio;

The programmer can then multiply by FibonnacciHashMultipler regardless of which type she is using it with. The compiler will pick the correct definition based on the expected type of the expression.

Good idea or taking the concept too far?

I haven’t thought about possible issues and confusion it might cause, but I think you over-complicated the example.
In your example you mention U64_MAX, U32_MAX, U16_MAX and U8_MAX.
Overloading integer max value seems considerably more…
(wait for it)

practical
:stuck_out_tongue_closed_eyes:

1 Like

Yeah, it’s actually a good question:

def MAX : U8 = 0xff;
def MAX : U16 = 0xffff;
def MAX : U32 = 0xffff_ffff;
def MAX : U64 = 0xffff_ffff__ffff_ffff;

To be fair, we’re more likely to have something similar to D’s U8.max (or U8::max. Havn’t decided yet.)

After some thought, here is what I came up with.

The idea is sound, but it has one risk I could think of: it would allow hiding of a constant.

To resolve this, I’m thinking of making this “opt in”. You cannot overload a constant unless that constant declared itself overloadable.

Also, I don’t see the use case for overloading a variable at all, so I’m going to go with not allowing that at all, at least at this stage.