语言设计:比较两个值时的隐式转换
我们正在与一位朋友一起开发一种名为 Simp 的新语言。它应该是一种简单但现代的脚本语言,具有良好且直观的语法。这是一个简短的例子:
var sum = 0
for i in 3..999 {
if (i % 3 == 0) or (i % 5 == 0) {
sum += i
}
}
say sum
现在我们遇到了一个问题,如果我们在比较两个值时应该使用隐式转换。具体来说,下面的程序应该输出什么?
# 1.
say (1 == '1')
# 2.
var x = 1
switch (x) {
case '1': say true; break;
case 1: say false; break;
}
# 3.
if ('1') say true;
else say false;
如果 1.
输出 true,我们可能还应该包含 ===
运算符来检查类型。但我不太喜欢那个运营商。
如果它因为比较两种不同的类型而抛出错误,那也没关系。多输入一点 (1 == int('1')
) 可以解决问题并使代码更清晰。但在这种情况下,2.
和 3.
该如何表现?
您推荐什么解决方案?
With a friend, we are making a new language called Simp. It should be a simple but modern scripting language with nice and intuitive syntax. Here is a short example:
var sum = 0
for i in 3..999 {
if (i % 3 == 0) or (i % 5 == 0) {
sum += i
}
}
say sum
Now we are standing against a problem if we should use implicit conversions when comparing two values. Specifically, what should the following program output?
# 1.
say (1 == '1')
# 2.
var x = 1
switch (x) {
case '1': say true; break;
case 1: say false; break;
}
# 3.
if ('1') say true;
else say false;
If 1.
outputs true, we should probably also include ===
operator that checks the types too. But I am not a big fan of that operator.
If it throws an error because two different types are compared, that is fine. A bit more typing (1 == int('1')
) solves the issue and makes the code clearer. But in that case, how to behave for 2.
and 3.
?
What solution do you recommend?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非常主观。如果这个网站上的每个人都回答这个问题,每个答案都会不同。取决于你想要什么。看起来你正在创造一种新语言作为一种学习体验,而不是解决现有语言的一些感知问题。这没有什么问题,但是你会遇到“你认为这意味着什么?”如果你不设定某种目标,就会出现很多问题。
Paul Graham 倡导这样的理念:最好的设计来自于为自己设计。 C 编程语言之所以流行,是因为发明者首先为自己而不是其他人设计了它。顺便说一句,这使它受到其他人的欢迎。另一方面,COBOL 是由委员会设计的,以便非程序员可以理解它并且程序员可以使用它。最终,非程序员仍然无法阅读它,程序员也讨厌使用它。
考虑到这一点,设定一个目标,即创建一种适合您的语言,并且不要过分重视其他人的意见。如果在做出某些设计决策后您不喜欢使用一种语言,请问问自己原因并进行必要的更改,直到您真正喜欢使用它为止。
在这个特定的示例中,您说您不喜欢 === 运算符。那就不要包含它。
如果您还没有研究过其他范式,这可能会对您有所帮助。流行的命令式/面向对象语言中存在的深度和概念质量可能非常有限。并不是说没有太多深度,只是说,通过将所有内容都基于您从这些范式中了解到的内容,您会将自己限制在可以包含在这种新语言中的概念的极小子集中。
Very subjective. If everyone on this site answered this question every answer would be different. Depends what you want. It seems like you're making a new language as a learning experience, rather than to solve some percieved problem with existing languages. Nothing wrong with that, but you're going to run into this "what do you think this should mean?" problem a lot if you don't set some sort of goal.
Paul Graham advocates the philosophy that the best design comes from designing for yourself. The C programming language was popular because the inventors designed it for themselves first, rather than others. Incidentally this made it popular with others. On the other hand, COBOL was designed by a commitee so that non-programmers could understand it and programmers could use it. In the end non-programmers still couldn't read it and programmers hated using it.
With that in mind, set the goal of making a language that feels right for you, and don't place too much weight on other people's opinions. If after making certain design decisions you have a language that you don't enjoy using, ask yourself why and make the necessary changes until you do enjoy using it.
In this particular example, you say you don't like the === operator. Then don't include it.
It might be beneficial for you to investigate other paradigms if you haven't already. The depth and conceptual quality present in popular imperative/Object oriented languages can be quite limiting. Not saying there isn't much depth, just that by basing everything on what you know from those paradigms, you'd be restricting yourself to an extremely small subset of concepts you could include in this new language.