wenyan icon indicating copy to clipboard operation
wenyan copied to clipboard

Integration suggestion for else-if statement

Open S-Asakoto opened this issue 4 years ago • 11 comments

It seems that recently the only way to write else-if clauses is to write it as two blocks -

image

I have a suggestion on integrating such into one using 又若 (= or if) or something similar:

image (The screenshot is edited using image editing software, not the display of the IDE)

S-Asakoto avatar Dec 24 '19 02:12 S-Asakoto

或若,君以为何如?

lymslive avatar Dec 30 '19 08:12 lymslive

@lymslive My original idea was to use 或若, but 或若 could be used independently meaning only "if" (as in 《敦煌變文集· 前漢劉家太子傳》:“汝緣年少,或若治國不得, 有人奪其社稷者,汝但避投南陽郡。”), so I chose 又若 that carries the character meaning "and".

S-Asakoto avatar Dec 30 '19 10:12 S-Asakoto

I don't think it's problem. 不能拘泥于某篇古文献“或若”曾有“if”用意,就否定,只要在 wenyan-lang 中不会有语法解析歧义就可考虑,而且我觉得“或若”这词的古风意境比“又若”更适合 wenyan-lang

lymslive avatar Dec 30 '19 12:12 lymslive

I think neither 又若 nor 或若 is conventionally used to mean "else if", so we can probably decide based on the semantic of the characters themselves (unless someone come up with another phrase). Among the two, I personally prefer 或若 slightly more, since I feel like 或 hints the fact that it's an "either-or" relation, whereas 又 is more like "additionally". But I'm happy to hear more discussion :)

LingDong- avatar Dec 30 '19 14:12 LingDong-

Here update argument. Actually in C, there is no so called "else if" statement, only "if else" statement.

if (cod1) {
    state1;
}
ese if (cod2) {
    state2;
}
else {
    state3;
}

is truely equivalent to

if (cod1) {
    state1;
}
ese {
    if (cod2) {
        state2;
   }
    else {
        state3;
   }
}

Because else can only accept one statement without {} block. So the second if else is just the only one statement for the previous else, and since there is no other statement, there is no need for extra {}.

Now back to wenyan-lang, else if can just view as 若非若, let me reformat your sample code(I ignore the single quote for identifier as poor input method):

若甲不大于五者
   乃得“小”
若非若甲不大于十者
  乃得“中”
若非
  乃得“大”
也也

The only strange view may end with tow s .

While on other hand, we can easily know how many if branch have written through counting the number of , they must be matched. (用写中文文章的章法来说,这叫前后呼应)So it may not be too bad. If you have to write too many s, that means you have designed too many branches, that won't be beautiful code anyway.

Then it's term for editor plugin to reformat 若非若 .

lymslive avatar Jan 02 '20 01:01 lymslive

@lymslive You're exactly right. Languages like C and JavaScript gets else if for free because single statement does not need to be enclosed by {}. Languages that got rid of the {}'s such as python, ruby, lua pay a price and had to define separate elseif elif or even elsif keywords.

I think 也也也 is very ugly and sounds like "yeah yeah yeah" or "yay yay yay" and goes against the philosophy of this language. We can mitigate the issue by using 云云 and 也 alternatingly (that's in fact why 云云 is added as an alias keyword).

I understand your point that code with too many branches are ugly anyway, but I think a series of else-if's are sometimes necessary, especially since we don't have switch statement yet.

In summary I think a separate elseif keyword like in python, ruby, etc. would be more readable, but I'm also happy to discuss other alternatives.

Thanks!

LingDong- avatar Jan 02 '20 01:01 LingDong-

Thought it may not related to this issue, I'm curious 云云 is alias to what keyword? I want get more ideal about your thought on design wenyan-lang,however I'm js expert since not used often in daily work, and so have not read the source. I have heard someone said the 云云 may something map to } .That's not good way if try to do such direct mapping, it definitely can't void multi `}}`` block.

And no matter how to implement, I image that it is still hard to treat nest loop and if case. For example, two for loop is very common in practical coding, end with two } if no extra statement in the outer loop. Then in wenyan-lang we must say 云云云云, that also not sound good as 也也也也.

Some times we seams no way to tolerate, and balance tradeoff.

lymslive avatar Jan 02 '20 05:01 lymslive

@lymslive 云云 is alias to , so }} case is never a problem. You can spot this usage in many ./examples. When there are three or more }, I would recommend repeating the 云云 and not the , because 云云 In classical Chinese is a bit similar to 等等 ("and so on"), and such repetition (等等等等) is much more acceptable.

LingDong- avatar Jan 02 '20 06:01 LingDong-

I agree 云云 can means "and so on", that's good used in loop, but not so good in condition if. Especially when only one if, you use one , but when two or more if, suddenly replace with 云云云云, that's not consistent. After all, normal user cannot notice that 云云 is alias to , that is technique detail or tricky.

lymslive avatar Jan 02 '20 07:01 lymslive

我还是用中文跟你讨论一个有关汉语的特有现象吧,前面看到你说 "alternatingly" 还以为你是想要找那样一个可轮换叠韵且意义相同的词组。

汉字大部分是单音节字,一个汉字能表示一个意思,但也有极少部分是双音节词(多音节词就更少了),它单独一个字没法表示意义,须两个字合起来才能表达意义,例如:

  • 忐忑
  • 叮咚 嘀哒 这可能不很准确,拆开来单用似乎也可以,但多为联用
  • 蝴蝶 也奇怪,可单独使用,但没见过单独使用的

我以为你要为多层嵌套的 if 结束符 }}}.... 找一个类似 叮咚叮咚叮咚……嘀哒嘀哒嘀…… 的词组,两个同义的字轮换使用,不致音律太单调,然后在任何一个音节停止也不致太违和。

这种语义说起来其实是很适合多层 if ... else if ... else if ... 的程序条件,因为后者看起来也像是不确定地轮换跳跃着,但最终落到一个分支上。

但古汉语中有没有类似的词组,我也一下子想不起来,集思广义吧。

噫吁戲这样的虚词,算是多音节词,其中单字不用或少用。但它不是叠韵的,不可重复使用。

字开头的多音节虚词,暂只想到元杂剧有 也么哥, 不过这偏口语俚语,不那么有古意,不过试着朗读下:

兀的不痛杀我也       // }
兀的不痛杀我也么      // }}
兀的不痛杀我也么哥     // }}}
兀的不痛杀我也么哥也    // }}}}
兀的不痛杀我也么哥也么   // }}}}}
兀的不痛杀我也么哥也么哥  // }}}}}}
兀的不痛杀我也么哥也么哥也 // }}}}}}}

(开心娱乐下 ^_^)

lymslive avatar Jan 02 '20 08:01 lymslive

也            // }
然也          // }}
然也哉        // }}}
然也然也      // }}}}
然也矣哉      // }}}}
然也然也矣哉  // }}}}}}

:smile:

cuixiping avatar Jan 11 '20 07:01 cuixiping