TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Is `let foo = () = foo;` good or bad?

1 点作者 s-p-n将近 7 年前
I&#x27;m playing around making a language.<p>I&#x27;m a firm believer that recursion is good, but there&#x27;s a lot going in in `let foo = () =&gt; foo;`.<p>First, I think `let foo` is interpreted, and `foo` is immediately hoisted, as it will be defined.<p>Second, the value of foo is known to be a function.<p>That function code isn&#x27;t evaluated at all, and here&#x27;s where I have several thoughts going through my head.<p>I was wondering, at compile-time, should a language bother checking identifiers? I don&#x27;t particularly like hoisting and think values should be evaluated before declaration begins to take place. I&#x27;ve always liked recursion, but at the moment I thought: &quot;if I want to do recursion, I shouldn&#x27;t implement any id-validity checking in functions.&quot;<p>Let&#x27;s say, let bar = () =&gt; foo(); (function() { window.foo = () =&gt; console.log(&quot;hi&quot;); }()); bar(); &#x2F;&#x2F; console logs &quot;hi&quot;<p>So, at compile-time it seems complicated to check for the existence of &quot;foo&quot; in the function &quot;bar&quot;. I mean- sure, it&#x27;s possible- but it seems like a losing battle for a compiler to chase identifiers all around the program, simply to check if they will exist when they are needed.<p>So I suppose one solution is to just let undefined identifiers slide unchecked until runtime. Another solution is to check as much as possible, unless or until the existence of an identifier could potentially come to being during input.<p>For example, the compiler could see that an array of links in a search-robot might be built after visiting a url that has n links. Perhaps a function is generated for each link, so the compiler might see `links[i].visit()` and think, &quot;Well, that function &#x27;visit&#x27; isn&#x27;t ever created, but links is an object populated by a resource, and maybe that resource provides the necessary API at runtime to make ..visit() defined.&quot; Designing a compiler like that seems.. not simple.<p>So ya, idk. Thoughts?

1 comment

TheAsprngHacker将近 7 年前
For identifier validity checking and recursion, you should look into let-rec and its differences from ordinary let. As for API checking, you would probably be using typeclasses or interfaces to specify an API. Even if the individual URL <i>objects</i> do not exist yet, a URL <i>type</i> should exist (at compile-time) that defines what operations are valid. I don’t think that a separate function would need to be generated for each link, but you can build arbitrary operations at runtime with first-class functions.
评论 #17378822 未加载
评论 #17378843 未加载