REPL semantics

jmh530 john.michael.hall at gmail.com
Thu Jul 12 22:04:39 UTC 2018


On Thursday, 12 July 2018 at 21:15:46 UTC, Luís Marques wrote:
> On Thursday, 12 July 2018 at 20:33:04 UTC, jmh530 wrote:
>> On Thursday, 12 July 2018 at 19:07:15 UTC, Luís Marques wrote:
>> Most REPLs I've used are for languages with dynamic typing. 
>> Perhaps take a look at a C REPL and see what it does?
>
> Well, cling calls the original function:
>
> [cling]$ #import <stdio.h>
> [cling]$ void foo(long x) { printf("long\n"); }
> [cling]$ void bar() { foo(42); }
> [cling]$ void foo(int x) { printf("int\n"); }
> [cling]$ bar()
> long
>
> ...but to me that doesn't mean much. If it was the other way 
> around (bar was updated to call foo(int)) I think I could 
> safely conclude that it was an intended consequence. But the 
> actual behavior can easily be explained by the fact that that's 
> the most straightforward implementation (especially for a REPL 
> that uses an existing C++ frontend, like clang). I was looking 
> for a more fundamental answer: what would the user prefer to 
> happen?

I think most people, at least most people who have used REPLs 
before, would think that the above should print int. But this is 
because most REPLs are used with dynamic languages. I don't doubt 
that it makes sense that it is easier to implement such that it 
prints long. You're compiling each line as it comes in, so bar 
compiles to some machine code that can only depend on the 
definition of foo at the time it is compiled.

I think the mental model of someone coming from a dynamic 
language would be as if bar is dynamically re-compiled when the 
foo(int x) is entered.


More information about the Digitalmars-d mailing list