Nim: SCF not applied when compiling stdin

Created on 11 Jun 2020  路  3Comments  路  Source: nim-lang/Nim

Source code filters are ignored when nim code is compiled via stdin rather than from a file.

Example

$ echo '#? stdtmpl'$'\n''foo' | nim -r --hints:off c -

Current Output

stdinfile.nim(2, 1) Error: undeclared identifier: 'foo'

Expected Output


Possible Solution

Apply calls to scfs in stdinfile.nim

Additional Information

  • Discovered while exploring how to reduce boilerplate so nim can be used as a full-on awk replacement
$ nim -v
Nim Compiler Version 0.1.2
# make sure to include the git hash if not using a tagged release
Low Priority

Most helpful comment

But please prove me wrong with a SCF example where the nim equivalent is inferior.

To directly answer your question:

Can you please come up with a more convincing example, that example does nothing but confirm what I said above.

  • menagerie of esoteric syntax only used in support of that feature (end for|case|if, different file extension, ? stdtmpl(emit="stdout.write"))
    => that's a tell-tell sign of a misfeature
  • more lines of code (21 vs 12)
  • doesn't interact with many features in nim
  • indentation is lost (and compiler won't tell you if indentation is off)
  • no syntax highlighting and code looks bad in my editor

I don't see any benefit in implicit echo; if you have some boilerplate, use a template or macro.

Ever seen a textual interactive program this concise?

yes, the nim version is more concise / readable and correctly syntax highlighted + indented:

import os
if paramCount() == 0: echo "Oh you did not add an argument. Try foo, bar, fuz or buz."
else:
  case paramStr(1):
  of "foo": echo "Oh look you wrote foo."
  of "bar": echo "Oh look you wrote bar"
  of "fuz": echo "Oh look you wrote fuz"
  of "buz": echo """Man I'm glad I don't have to write echo
in front of every one of these conditions"""
  for i in 0..5:
    echo "especially when I have complex nested stuff like this"
  echo "implicit echo is positively liberating."

All 3 comments

SCF is IMO a perfect example of a mis-feature that un-necessarily complicates the language (edge cases etc, complex feature interaction), and is best replaced by standard nim code (strformat + friends) with more flexibility, no worse readibility. Standard separation of concerns. I'd like for it to be deprecated (even if with a long deprecation cycle).

But please prove me wrong with a SCF example where the nim equivalent is inferior.

I understand that stuff like SPF can complicate development through edge cases, however it is also extremely useful. I know of two ways you can get string templating this good using language features: Nim SPFs, and PHP (PHP can be nasty but it does a couple of things really well). And it's the only way of getting string templating this good into an executable.

SPF goes really well with strformat enabling crazy powerful hacks. Also the last time this was discussed strformat couldn't have loops and branches.

The number of special cases like this one are finite. I understand that maybe there shouldn't be two dozens more features like this one, but let's keep this one!

Edit: @timotheecour To directly answer your question:

#? stdtmpl(emit="stdout.write")
# import os
# if paramCount() == 0:
Oh you did not add an argument. Try foo, bar, fuz or buz.
# else:
# case paramStr(1):
# of "foo":
Oh look you wrote foo.
# of "bar":
Oh look you wrote bar
# of "fuz":
Oh look you wrote fuz
# of "buz":
Man I'm glad I don't have to write echo
in front of every one of these conditions
# for i in 0..5:
especially when I have complex nested stuff like this
# end for
implicit echo is positively liberating.
# end case
# end if

Ever seen a textual interactive program this concise?

But please prove me wrong with a SCF example where the nim equivalent is inferior.

To directly answer your question:

Can you please come up with a more convincing example, that example does nothing but confirm what I said above.

  • menagerie of esoteric syntax only used in support of that feature (end for|case|if, different file extension, ? stdtmpl(emit="stdout.write"))
    => that's a tell-tell sign of a misfeature
  • more lines of code (21 vs 12)
  • doesn't interact with many features in nim
  • indentation is lost (and compiler won't tell you if indentation is off)
  • no syntax highlighting and code looks bad in my editor

I don't see any benefit in implicit echo; if you have some boilerplate, use a template or macro.

Ever seen a textual interactive program this concise?

yes, the nim version is more concise / readable and correctly syntax highlighted + indented:

import os
if paramCount() == 0: echo "Oh you did not add an argument. Try foo, bar, fuz or buz."
else:
  case paramStr(1):
  of "foo": echo "Oh look you wrote foo."
  of "bar": echo "Oh look you wrote bar"
  of "fuz": echo "Oh look you wrote fuz"
  of "buz": echo """Man I'm glad I don't have to write echo
in front of every one of these conditions"""
  for i in 0..5:
    echo "especially when I have complex nested stuff like this"
  echo "implicit echo is positively liberating."
Was this page helpful?
0 / 5 - 0 ratings