After reading this page in existential languages in Haskell, I was forced to check the limits of this behavior, so I wrote the following code fragment:
{-
The Showable type Showable very similar to the ShowBox type created in the above link. Then I created this contrived example to illustrate my question.
showSomething :: Bool -> Showable showSomething True = pack 1 showSomething False = pack "ABC" main :: IO () main = do x <- getLine let y = read x print $ showSomething y
This code (which works fine) asks the user for input (which should be “True” or “False”), and then outputs 1 if it is True or "ABC" if it is False.
But I can’t fully understand how the system does this. Mathematically this makes sense. But I do not see how the computer can solve it. For me, it seems that at runtime, the system decides whether to call the Int show or String show function, but that would mean the existence of something like C ++ vtables, I believe that Haskell has a concept.
My question is: how does he resolve this? The system cannot know in advance whether I will enter true or false, so it cannot know which show call at compile time, but it clearly works in both cases.
source share