
Is `let foo = () = foo;` good or bad? - s-p-n
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?
======
TheAsprngHacker
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 _objects_ do not exist yet, a URL _type_ 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.

~~~
s-p-n
Thanks! I'll look into let-rec, I've never heard of it before :)

Everything in my language is an expression, so I guess you could say every
literal in the language is first-class.

I was thinking for i/o, I could use promises. The promise would resolve to
whatever type makes sense for that resource- defined either by a library or by
the program.

Of course, that brings up "arbitrary operations at runtime". My target after
transpiling is JavaScript, which has a wonderful JIT compiler already. I need
to learn source mapping and stuff so I can make error-tracing work nicely, as
it's simply not possible to catch any possible error at compile-time.

