Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ANNOUNCEMENT] Try-Catch block #399

Open
LingDong- opened this issue Dec 25, 2019 · 7 comments
Open

[ANNOUNCEMENT] Try-Catch block #399

LingDong- opened this issue Dec 25, 2019 · 7 comments
Labels
announcement Announcement from maintainers syntax Suggestions to improve the syntax

Comments

@LingDong-
Copy link
Member

LingDong- commented Dec 25, 2019

Hi everyone, as requested by #275 and #358 , try-catch is here! Here is an overview of the syntax:

姑妄行此。
	嗚呼。「「滅頂」」之禍。

如事不諧。豈「「ReferenceError」」之禍歟。
	吾有一言。曰「「本無此物。奈何用之」」。書之。

豈「「SyntaxError」」之禍歟。
	吾有一言。曰「「不合文法。不通之甚」」。書之。

豈「「TypeError」」之禍歟。
	吾有一言。曰「「物各其類。豈可混同」」。書之。

豈「「滅頂」」之禍歟。
	吾有一言。曰「「嗚呼哀哉。伏维尚飨」」。書之。

不知何禍歟。名之曰「奇禍」。
	吾有一言。曰「「人坐家中。禍從天降」」。書之。

乃作罷。

Which is roughly equivelent to this JavaScript:

try {
	var e = new Error(); e.name = "滅頂"; throw e;
} catch(e) {
	if (e.name == "ReferenceError") {console.log("本無此物。奈何用之")}
	else if (e.name == "SyntaxError") {console.log("不合文法。不通之甚")}
	else if (e.name == "TypeError") {console.log("物各其類。豈可混同")}
	else if (e.name == "滅頂") {console.log("嗚呼哀哉。伏维尚飨")}
	else {var err = e; console.log("人坐家中。禍從天降")}
}

If you do not need to catch the errors, you can do:

姑妄行此。
	嗚呼。「「無足輕重」」之禍。
如事不諧乃作罷。

Which roughly equals to:

try{
	var e = new Error(); e.name = "無足輕重"; throw e;
}catch(_){}

You can also do this to catch any error:

姑妄行此。
	嗚呼。「「事不關心」」之禍。
如事不諧。不知何禍歟。書之。乃作罷。

Which roughly equals to:

try{
	var e = new Error(); e.name = "事不關心"; throw e;
}catch(e){
	console.log(e);
}

You can see details about the usage in ./examples/try.wy.

Please let me know what you think or found any issues! Thanks a lot!

@LingDong- LingDong- added syntax Suggestions to improve the syntax announcement Announcement from maintainers labels Dec 25, 2019
@liyishuai
Copy link

Is it a typo in JavaScript code that all conditions are the same?

@LingDong-
Copy link
Member Author

Thanks @liyishuai you're right, just fixed the typo :P

@lymslive
Copy link

lymslive commented Jan 4, 2020

今天读到 Try-Catch.md 文档,感觉这语法句式设计挺有意境。
随即又感觉有两个字可以再推敲一下。

“姑妄行此” 我觉得用 “姑妄行是” 好此,用“是”表示“此”“如下”更有点古意。或者就按原 #275 的提议用”姑妄行之“。
“不知何禍歟”我觉得用“未知何禍歟”更有古意。
“如事不諧”倒是可以用“不”字,这比较顺畅惯用。既然这句用“不”字,再用“不知何祸”,两个“不”字有点犯重了。

当然,这已经是细枝末节了。
然后我更想说的是,switch case 语句,也可以参考这句式结构。case 语句与 catch 语句是非常似的。既然 catch 用“豈……歟”,那么 case用“其……歟“表达。

之前的 issue 有提到用”省“字表达 switch 含义。结合这里的语法,用”姑妄省之“,如何?

夫‘变量甲’,姑妄省之。
其……歟,乃
其……歟,乃
其……歟,乃
未知其数 // 未知其言
乃……
作罢

switch 限定只能用数字与字符串比较是可接受的。

也许”姑妄“用在 switch 语句中感觉太严重了,超级没底气的样子,与 try 也太过相似。那么用“如若省之”怎么样?这与 if 的“若”更亲缘。“若”关键字后接“省”就表示 switch 语义。

@LingDong-
Copy link
Member Author

@lymslive , interesting suggestions!

  • 此 and 是 are similar in both meaning and pronunciation (上聲四紙).
  • 未 sounds like "not yet", while the final clause in try-catch is more "final".
  • Indeed I intend to use a similar grammar for switch :)

@Lotayou
Copy link

Lotayou commented Jan 10, 2020

@lymslive Guess the main reason @LingDong- don't use “姑妄行之” is that “之” has already been fixed as a keyword to reference the latest calculated/assigned variable.

@zxch3n
Copy link
Contributor

zxch3n commented Jan 20, 2020

Maybe this should be added to the syntax cheatsheet? @LingDong- @antfu

@antfu
Copy link
Member

antfu commented Jan 20, 2020

Good suggestion. It would be great if you can help out on our wiki https://github.com/wenyan-lang/wiki Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
announcement Announcement from maintainers syntax Suggestions to improve the syntax
Projects
None yet
Development

No branches or pull requests

6 participants