Should casting to a contract instance perform an EIP-165 check

It is sometimes assumed that by passing a contract or interface type to a function (or casting an address to an contract or interface) some type check happens. Currently nothing like this is happening and it would be necessary to manually write this check. Should something like this happen more automatically (similar to how overflows and underflows are checked by default)?

I think we would do something like this or at least consider it, if it was clear how it could be done consistently and safely - but how exactly would it work? We need backwards compatibility with older compiler releases and existing deployed contracts… we can’t just assume every contract implements EIP-165… we could in general promote EIP-165 to a language feature, but even that wouldn’t solve backwards compatibility…

If there was a reasonably cheap, safe, backwards-compatible and uncontroversial way to do this, I think we’d be very open for it, but at least out of my head I don’t see a really good way to do it.

With the new conversion syntax proposed by @axic in #11284 we could have a type of cast that does the check. E.g. something like: try<IER20>(myAddress) or eip160_cast<IER20>(myAddress).

If we had generics we could also just make it a function in stdlib.

1 Like

Yes this would be a good alternative. I do agree that the extrem way I proposed (to just change the existing behaviour and keeping the syntax) is not the best way. Especially since this is quite a lot more gas heavy :wink:

I like the try annotation, as it could be used for other purposes too. A checked_cast<IERC20>(myAddress) might also work.