nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.42k stars 1.47k forks source link

SCF not applied when compiling stdin #14637

Open capocasa opened 4 years ago

capocasa commented 4 years ago

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

foo

Possible Solution

Apply calls to scfs in stdinfile.nim

Additional Information

$ nim -v
Nim Compiler Version 0.1.2
# make sure to include the git hash if not using a tagged release
timotheecour commented 4 years ago

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.

capocasa commented 4 years ago

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?

timotheecour commented 4 years ago

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.

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."