Ok, I feel very inadequate for not finding definite answer for this, but is there any way I could use lisp like question mark proc names? eg. instead of
proc isLarge(p: Penis): bool =
return p.owner != "wiltzutm"
I would like to use the typical lisp style:
proc large?(p: Penis): bool =
return p.owner != "wiltzutm"
import std/strutils
proc `youOk?`(message: string): bool = not ("penis" in message)
echo `youOk?`("some message")
+1, really miss that feature (although I'm from Ruby, not Lisp).
... Though you would have to use backticks each time you call a proc with such name.
That's not a good solution, nobody going to use that in practice.
Thank you for the quick reply. Actually I tried that backtick trick, but for some reason I trusted my IDE/linter whining about it and didn't properly compile it for testing. But it is good to know that it should and will work.
As you said, it loses the whole point of using the question mark when one has to use the backticks for calling! :) Also I'm assuming I can call it like a method call even with the backticks like this:
proc youOk?(post: ForumMessage): bool = not ("penis" in post.text)
let myMessage: ForumMessage = initForumMessage(poster:"wiltzutm", text: "*frustrated raging for not getting the question mark working*... penis")
# This line here:
if myMessage.`youOk?`:
echo "I'm fine, how about you?"
else:
echo "I feel terribly ashamed for not fullfilling the criteria of this forum and common decency. For that I'm sorry! I'm also eternally thankful that despite my rudeness you still answered to my message."
Indeed the question mark syntax is totally subjective matter. I prefer the question mark. Of course I understand that it isn't so subjective/superficial thing when you are the guy who has to implement it in the language, so if there's no point in it on your behalf then it is the end of discussion. :)
My question was more like if it would be possible somehow (without modifying the Nim itself), but it seems I have to call it using the backticks which defeats the whole purpose to my use-case.
Thank you for the opinion.
I don't miss the question mark from Ruby that much, but
myseq.sort! vs. myseq.sort
was not that bad. Well for sort we have sorted(), but that does not work always, so some proc names there is no ed form available.
With template we can do this:
import strutils
template `?`(b: bool, c: untyped) = (if b: c)
"nimble".contains("nim")?
echo "nim inside"
https://play.nim-lang.org/#ix=2MOU
But if we could define a postfix operator ?, it may work with if
import strutils
func `?`(b: bool): bool = b
if "nimble".contains("nim")?:
echo "nim inside"
else:
echo "no nim"
Not all functions could be prefixed with is_, let's say a.match?(b) or table.has?(key) or set.contains?(key)
Well if we're on a quest of readabillity, alias those procedures with the following
a.doesMatch(b)
a.matchesWith(b)
a.has(key) # already gramatically correct
key in set
I would like to use the typical lisp style
(defun is-large-p (foo) ...)
I'm not into Lisp, only into Emacs, and there these tests are called "predicate" and more often than not have a trailing p or -p) in their names.
In any case, I wouldn't use Lisp as an example on how to name things. It's quite inconsistent.
Not proposing doing this, just for fun :)
It should be possible to tune VSCode to render different symbols. So you may use names like table.has7(key) or table.hasqm(key) and VSCode would render it as table.has?(key).
I would like to use the typical lisp style...
You can use underscores and patch your font so that it looks more like a dash... :-)
You can use ? from unicode instead of ? in ascii code without enclosing it in backticks. There are more charactors in unicode that might improve Nim code readability. ×÷∀∃∈∑√∩∪≒≡≠ See also: https://en.wikipedia.org/wiki/Glossary_of_mathematical_symbols
Sample code: https://play.nim-lang.org/#ix=2Nsf
I think source code written with many kind of charactors is easier to read than source code written only with small kind of charactors, but harder to write or edit.
In a text with many alphabets, mathematical symbols are easier to find than alphabet words. Also I think recognizing one charactor from many charactor set is easier than recognizing words consist of small charactor set. So "if foo ∉ bars:" is easier to read than "if foo notin bars:".
It is possible to create a language that use only 2 charactors but it would be very hard to read even if you can recognize each words.
10110000111110100100001010001101
00000010110001010110000110110111
11111010001011110000100100100100
11101001000100000100010011111000
10100100100001100110011101110000
11010111101001101000010101111101
11000011001011111100101110110011
01101000110100101001101000111010
Languages that uses smaller charactor set is closer to this binary language than languages that use larger charactor set.