This code compiles|runs fine.
def powmodint(b, e, m)
r = typeof(m).new(1)
b = b % m; b = typeof(m).new(b)
while e > 0
r = (r &* b) % m if e.odd?
e >>= 1
b = (b &* b) % m
end
r
end
def powmod(b, e, m)
if m > UInt64::MAX
r = powmodgmp(b, e, m)
elsif m <= UInt32::MAX
r = powmodint(b, e, m.to_u64)
else
r = powmodint(b, e, m.to_u128)
end
end
When I shorten it to this:
def powmodint(b, e, m)
r = typeof(m).new(1)
b = b % m; b = typeof(m).new(b)
while e > 0
r = (r &* b) % m if e.odd?
e >>= 1
b = (b &* b) % m
end
r
end
def powmod(b, e, m)
if m > UInt64::MAX
r = powmodgmp(b, e, m)
else
m = (m <= UInt32::MAX) ? m.to_u64 : m.to_u128
r = powmodint(b, e, m)
end
end
It gives this error:
In primesutilstestyb15c_ctznew1b2.cr:22:17
22 | r = typeof(m).new(1)
^--
Error: wrong number of arguments for '(UInt128 | UInt64).new' (given 1, expected 0)
Overloads are:
- Union(*T).new()
I think this should not be a compiler error, and no worse than a warning.
I understand why at compile m
is considered a Union
type.
But there’s no runtime paradox, as m
will collapse to be one type in powmodint
.
If the first code form compiles with no errors why shouldn’t the second?