DEV Community


Compiled VS Interpreted: A Code Example

Freelance Web Developer
・1 min read

I've been writing code in python (interpreted language) for some time and as expected I, unconsciously, got habitual of writing interpreted code and python's lazy evaluation. For example, the following code works in python:

if(1 == 2):

It doesn't matter if the variable a has been declared or not, the code works because python never executes the print statement until the if condition is true.

On the other hand, when you try to do it in a compiled language like nim, it fails, because as expected of a compiler, it evaluates everything it can during compilation. So I get the error:

if(1 == 2):
    echo a #ERROR: undeclared identifier: 'a'

The above example seems extremely simple, but you can end up with this kind of gotcha anywhere, like I did when writing the following code in nim:

    htag* = object
        text: string
        level: int
        name: string

    atag* = object
        text: string
        link: string
        name: string

proc renderTag(tag: object): string =
    echo tag
    if( == "h"):
        return "<h" & $tag.level & ">" & tag.text & "</h" & $tag.level & ">"

    elif( == "a"):
        return "<a href='" & & "' target='_blank'>"

        return "nothing"

Here, if I declare one kind of object, I'll get an undeclared field error with the other.

Discussion (0)