Open kiljacken opened 8 years ago
This is working as intented IMO. The generic function doesn't know if the +
operator is valid on type T
. What is needed is a restriction for types that can be used with arithmetic operators.
The problem with that is, that our concept of type restrictions is intended to use interfaces. This would mean that operators would have to be implemented as an interface, or we'd have to special case the heck out of this scenario. Not that doing operator overloading as an interface is impossible, it would just require generic interfaces (Which is a patchwork solution for using implicit interfaces instead of explicit traits)
I just think special-case "interfaces" for + - * /
would be fine.
Well, we'd need it for all binary operators and some unary operators. This (should be) the complete list:
!
~
-
==
+ - * / %
> < >= <=
& | ^
<< >>
|| &&
That's a lot of interfaces to special case, so I'm not so sure.
As for ! || &&
, I don't see any reason for them in generic functions, since really you should just pass a bool
, as we don't allow overloading of these.
As for the other ones, how about:
Equable
- == !=
Number
- + - - * / > < >= <= Equalable
Int
- Number %
Bits
- & | ^ ~ << >>
We could probably could it down to around 6 pseudo-interfaces, that's still a lot though:
+ - * / %
)But I guess it's doable
Currently the AST visitor checks the actual body with unresolved generic parameters when doing semantic checks. This will cause errors for otherwise correct programs like the following:
Solving the problem will require introducing a boolean field to the AST visitor for signalling that we want to visit the function variations instead of the actual function body. This part is simple. Actually traversing and substituting types in the function bodies will be a fair bit of work. Below is the new VisitFunction method I created for checking the available information: