Search code examples
language-agnosticcomments

Comment wizardry


I am not a level 60 wizard yet, so I've got a question based on the following sentence in the Wikipedia article on magic:

Any comment that has an effect on the code is magic.

How is that? In what languages is such a thing possible? More specifically, what effect can a comment have on the code?


Solution

  • Magic comments take various forms. In Ruby, a comment can be placed at the top of the file, which specifies an encoding that the interpreter will use when reading in the file:

    # -*- coding: UTF-8 -*-
    

    more


    In Unix systems, the first line can be a shebang. In any language with octothorpes for comments, this ammounts to a comment that the OS reads to decide how to interpret the file

    #!/bin/bash
    echo hello from bash
    

    more


    In Haskell, certain types of comments known as pragmas can be used to switch on (or off) language features.

    {-# LANGUAGE OverlappingInstances, NoTypeFamilies #-}
    

    more