Is there a way to know in advance if a function definition will overwrite a previous one?

To answer the question from your title:
Generally no and I think the question might be a X-Y-Problem.

To go into a bit more detail: You can of course try to detect if there is currently a method with a given signature but this is not good enough. The method could simply not be defined yet because it is loaded later, or generated later or… IIUC from your example, you would consider foo(x::Int) to not overwrite a definition of foo(x::Any) but this is not really what Julia’s semantics reflect in my view.

The aim of your question is likely connected to type piracy (perhaps inspired by this current thread: How to detect/avoid type piracy?). If that is the goal, then no meta programming can really help you. Even checking whether a method “exists” before you provide your own definition does not safe you from committing type piracy. Also that check should be stricter than what you showed in your post. Defining foo(::Int) for a foo you do not own that has a definition for foo(::Any) is definitively type piracy and can bite you.
Perhaps one can argue that defining foo(::String) if you don’t own foo(::Number) is not quite as bad, as hopefully nobody relied on the MethodError being hit, but it still stays type piracy.

2 Likes