Hacker News new | ask | show | jobs
by enriquto 1798 days ago
> make sure that you only add a method to a function if it does the "same thing"

But this only concerns when I'm writing the code myself. If I read some code and I see a few nested function calls, there's a combinatorial explosion of possible types that gives me vertigo.

> complex and rational numbers can be constructed using anything that has arithmetic defined.

seriously? this does not seem right, it cannot be like that. If I build a complex number out of complex numbers, I expect a regular complex number, not a "doubly complex" number with complex coefficients, akin to a quaternion. Or do you? There is surely some hidden dirty magic to avoid that case.

1 comments

There's no hidden dirty magic, but you're right: Complex numbers require `Real` components and Rationals require `Integer` numerators and denominators. Both `Real` and `Integer` are simply abstract types in the numeric type tree, but you're free to make your own. You can see how this works directly in their docstrings — it's that type parameter in curly braces that defines it, and `<:` means subtype:

    help?> Complex
    search: Complex complex ComplexF64 ComplexF32 ComplexF16 completecases
    
      Complex{T<:Real} <: Number
    
      Complex number type with real and imaginary part of type T.