質問

I am designing a language. I am being troubled by what to call "else if". My language uses indentation for blocks, so I need a keyword for "else if".

Python uses "elif" (meh...) and Ruby uses "elsif" (yuck!). Personally, I hate to use abbreviations, so I don't want to use either of these. Instead, I am thinking of just using "else if", where an arbitrary number of spaces can appear between "else" and "if".

I've noticed that this doesn't occur very often in programming languages. C# has "yield return" as a keyword, but that's the only example I can think of.

Is there an implementation concern behind this? I've created my lex file and it accepts the keyword with no issues. I am worried there is something I haven't thought of.

役に立ちましたか?

解決

As long as you don't allow inline comments/newlines, there's nothing wrong with multi-word keywords. The only thing is that else if might be confusing for your language users, tempting them to write else while or else for. You'll have hard time explaining them that your else if is a keyword and not two statements following each other.

他のヒント

Out of curiosity, why bother having an "else if" keyword? You can just have an "else" keyword and the next thing is an expression... an "if" expression.

<expression> := IF | somethingelse
IF <expression> THEN <expression> ELSE <expression>

The idea being that the if in "else if" is just the start of the expression after the "else" keyword.

A sample of what I mean, per the comments

if (x == 1)
    return 5
else return 4

if (x == 1)
    return 5
else if (x == 2)
    return 6
else
    return 7

The concept above being that the return 4 is no different than the if and it's "arguments". As long as you allot the start of the next command on the same line (even if it's not considered good form), you can treat the if in else if as just the start of the next expression.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top