I don’t really see the advantage. How is run mySpecialName { ... }
better than run { ... }
. Ok you can name the block, but that’s nothing more than a comment with a special syntax, since the name is never used again.
The idea about renaming arguments is nice, but I don’t think it’s important enough to create an entire new language feature for it. After all it would be the same as
run {
val someNewName = theOldName
// doSomething
}
One thing I don’t understand from your post is this part of code
fun replace(): String {
val symbolPattern = Pattern.compile("\\$([a-zA-Z]\\w*)")
val symbolMatcher = symbolPattern.matcher(stringToReplace)
while (symbolMatcher.find()) {
val symbolName: String = symbolMatcher.group(1)
run replaceAllInstances(symbolName) {
if (getSymbol(symbolName) != null &&
symbolName !in alreadyReplaced) {
alreadyReplaced.add(symbolName)
stringToReplace = stringToReplace.replace(
"$" + symbolName, translate(symbolName))
}
}
}
return stringToReplace
}
Why does your new kind of run
block need symbolName
as a parameter:
run replaceAllInstances(symbolName) {...
Why not just
run replaceAllInstances {
symbolName
is captured anyways (which has no cost due to inlining). Which leads back to my earlier question why we need a name for the block at all, simplifying it to
run { ...
Ok, I understand the idea that you want to have nice looking code like
fun replace(): String {
val symbolPattern = Pattern.compile("\\$([a-zA-Z]\\w*)")
val symbolMatcher = symbolPattern.matcher(stringToReplace)
while (symbolMatcher.find()) {
val symbolName: String = symbolMatcher.group(1)
replaceAllInstances(symbolName)
}
return stringToReplace
}
private fun replaceAllInstances(symbolName: String) = TODO()
And I agree, that with code folding, the named version could be made to look like that, but this is also true if you use a comment.
I personally don’t like code folding much. IMO code is bad, if you need code folding to understand it.
Instead I would love to see improvements made to the way inner functions work. Maybe a way to explicitly state, that you don’t want to capture any value or the ability to declare your inner functions at the end of the main function (just throwing some ideas out there).