A method literal is a method declaration surrounded by enclosing syntax so that it can be embedded as a literal value in an Essence# expression.

The EBNF for method literals:

MethodLiteral       = "[", MethodDeclaration, "]";

 

A method literal must be enclosed between a single beginning "[" character and a single ending "]" character, making its syntax rather similar to that of a block. The key difference between the syntax of a block and the syntax of a method literal is that the construct that immediately follows the beginning "[" character must be unambiguously a method header. And that, in fact, is one of the reasons that the "##" (method header) token exists, and is required in some cases, but optional in others: When the "##" token is required, it's because its absence would create syntactical ambiguity, such that it would not be possible for the parser to distinguish a block from a method literal.

For more information on the syntax of a method declaration, please see the article on that topic.

Methods defined in Essence# class libraries declare methods as method literals, instead of as method declarations that are the root of their respective parse trees. Using method literals for that purpose obviates any need to encode method names as filenames; or alternatively, it obviates any need to define a special syntax for dealing with sequences of method declarations, or for syntactically embedding method declarations inside of class declarations. So there's no need for a special "file in" syntax, nor any need for a special parser that can consume a special "file in format.".

That said, the ANSI standard does require that a conforming implementation support the Smalltalk Interchange Format. Essence# does not currently support that format, but will do so before it leaves beta.

Using Class Specifications

A method declaration may optionally use a class specification construct--but only if a method header token is also used. That means a method literal may also use a class specification construct, since its syntax is defined as an embedding of a method declaration enclosed in between the tokens "[" and "]".

The presence or absence of the class specification construct may change the behavior of the compiler:

If there is no class specification in the method header, then whether or not the compiler will attempt to bind non-local variable references depends upon how the compiler is invoked. If the compiler is not provided with a binding context for non-local variables when it's invoked, and if there is no class specification in the method header to provide one, then the compiler won't check whether any references to non-local variables might be undeclared (however, that check is always performed whenever a method is added to a class or trait.)

On the other hand, if the method header includes a class specification, then the compiler will always attempt to bind references to non-local variables, using whichever class is specified by the class specification construct as the binding context. In that case, any undeclared variables will be treated as compilation errors.

In other words, the compiler interprets the presence of a class specification construct in a method header as a command to verify that there would be no undeclared variables referenced by the method it's compiling, if that method were to be added to the specified class. Conversely, it interprets the absence of a class specification as a command to defer any such checks until the method is actually added to a class or trait.

When compiling either self expressions or "do its" (initializers or scripts,) the compiler is not configured to provide any default binding context for method declarations--and therefore is also not configured to do so for method literals. That's because there's no way to know a priori what the "right" binding context might be in such cases.

Since methods are checked for any references to undeclared variables when they are added to a class or to a trait (which is usually the proper time, because that's when the right binding context is known absolutely,) there are no system integrity issues raised by this binding paradigm. And that's why the method literals in "methods.instance" and "methods.class" files don't use class specification constructs in their method headers. There's no need, really.

However, there are compilation use cases other than compiling "methods.instance" and "methods.class" files. And some of those use cases do require that the compiler bind all variable references during initial compilation--which is why class specification syntax is present as on option for method headers.

 

Last edited Aug 10, 2014 at 11:25 AM by Strategesis, version 25