隐式主题可以用语言干净地实现吗?

发布于 2024-11-29 18:38:56 字数 1056 浏览 3 评论 0原文

我感到无聊,所以我开始用 Perl 5.14 编写自己的 HTML 模板语言,因为我可以。

这个问题是关于特定功能的解析器实现特定语言构造的可行性,这在语言中是罕见的

编辑:应要求删除了关于 PHP 的残酷且完全不必要的评论。

编辑2:变成了一个更客观的问题。

PHP 在动态处理 HTML 方面的简单性是一个优势,尽管任何 50000 行框架的用户可能会这么说。

无论如何,我正在考虑隐式主题在这种语言中如何运作。 Perl 通过 $_ 拥有它们,但它们有点令人讨厌——它们不一致,必须通过原型由 subs 显式允许,并且它们通过引用变量暴露给用户,更不用说必须弄清楚是哪个内置函数使用它们,哪些不使用它们。

我所说的是将隐式主题的概念直接连接到语言中,并使用一些定义明确的规则。例如,所有迭代构造都将其分配给每个元素,并且闭包将其用作第一个参数(一种实现 Ruby-do 块优雅的简单方法)。

现在,关键是:您不必定义默认的“it”变量,只需定义严格的语义即可推断出默认主题,而不会推断出默认主题。例如,一个参数比预期少的函数将使用它作为第一个参数。或者无论如何。您不通过变量公开主题,而是通过语言公开主题。

在像 Lisp 或 Ruby 这样的语言中,这很难做到,因为它们并不真正喜欢像循环这样的语言级构造,而更喜欢自己的方式,无论是宏还是块+方法。

但在像 Perl 这样包含语言相关结构的语言中,这可能非常有效。

但是,还有一件事是,如果正在讨论一个新主题,该语言会迫使您明确提及某些事物。例如,如果有一个 for 循环,则内部循环的主题将是其自己的当前元素,而不是外部当前元素。 Perl 用户已经这样做了,但其他语言用户可能不熟悉这个概念。

那么,是否可以将隐式主题干净地实现到一种语言中,并通过限定范围的主题强制编码人员在不明确时保持明确?

这在英语中是有道理的。如果我们讨论其他事情,我们就会偏离主题,然后我们“回到”“超级”主题,然后明确该主题是我们之前讨论的内容。然后它自动再次意味着“超级”主题。我们的演讲是动态范围的!

另外,对我来说,尝试解析这个问题的最佳方法是什么?

I was getting bored, so I started cranking out my own HTML templating language in Perl 5.14 just because I could.

This question is about parser implementation of a specific feature and the feasability of a specific language construct which is rare among languages.

Edit: Cruel and completely unnecessary remarks about PHP removed at request.

Edit 2: Turned into a more objective question.

PHP's simplicity at dynamically handling HTML is a strength, despite what any 50000-line framework users may say otherwise.

Regardless, I was thinking about how implicit topics would work in such a language. Perl has them via $_, but they're kinda nasty -- They're inconsistent, must be explicitly allowed by subs via prototypes, and they're exposed to the user via a reference variable, not to mention having to work out which built-ins use them and which ones don't.

What I'm talking about is wiring the concept of implicit topics straight into the language, with some very well defined rules. E.g. all iteration constructs assign it for each element and closures use it for the first argument (an easy way of having Ruby-do block elegance).

Now, here's the key bit: rather than defining a default 'it' variable, you just define strict semantics where the default topic is inferred and where it isn't. For example, a function that has one less argument than expected will use it as the first argument. Or whatever. You don't expose topics via a variable, rather by the language.

In a language like Lisp or Ruby this would be hard to do, since they don't really like language-level constructs like loops and prefer their own ways, whether it be macros or blocks + methods.

But in a language that embraces language-wired constructs like Perl, this could work really well.

But, and here's another thing, the language forces you to explicitly refer to things if there's a new topic being discussed. For example, if you had a for loop, the topic of the inner loop would be its own current element, not the outer current element. Perl users already do this, but other language-users may not be familiar with the concept.

So, could implicit topics be implemented cleanly into a language, with scoped topics that force the coder to be explicit when ambiguous?

This makes sense in English. We digress into subtopics were we discuss something else, and then we 'scope back out' to the 'super' topic and then make it clear the topic is what we were discussing before. It automatically then means the 'super' topic again. Our speech is dynamically scoped!

Also, what would the best way be for me to go about trying to parse this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极致的悲 2024-12-06 18:38:56

编程语言中存在各种级别的隐式主题。

在最小方面,您可以在方法中使用 Javascript 的 this 设置和 Ruby 的 self 设置。

在另一个极端,有像 J 这样的语言,其中每个角色都会施加隐式循环其应用数据的操作。

介于两者之间的是像 Perl 这样的语言,它提供了许多隐式操作,但也允许您明确需要的任何内容。

所有 Perl 的 foreach 构造(foreach 循环、map、grep)均使用 $_ 作为主题变量。默认情况下,许多内置函数又使用 $_ 。子例程使用 @_ 作为主题变量,并且一些内置函数默认使用 @_

我建议您花更多时间学习如何在 Perl 中使用主题化器和上下文来消除无关和冗余的语法。因为据我所知,您正在寻找的所有内容都已经是 Perl 的功能,正确使用这些功能取决于您,并约束自己不要使用与该目标相悖的功能(这是一个很大的问题)毕竟,并不是每个功能都是为简洁代码设计的)。

There are various levels of implicit topics in programming languages.

On the minimal side you have things like Javascript's setting of this and Ruby's setting of self in methods.

At the other extreme you have languages like J where every character imposes actions that implicitly loop around their applied data.

Somewhere in the middle are languages like Perl which provide many implicit actions, but which also allow you to be explicit about anything you need.

All of Perl's foreach constructs (the foreach loop, map, grep) each use $_ as the topic variable. Many builtins in turn use $_ by default. Subroutines use @_ as the topic variable, and several builtins use @_ by default.

I suggest you spend some more time learning about how topicalizers and context are used in Perl to eliminate extraneous and redundant syntax. Because as far as I can tell, everything you are looking for is already a feature of Perl, it is just up to you to use those features correctly, and to discipline yourself to not use features that work against that goal (it is a big language after all, not every feature is designed for terse code).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文