Definition of Method in Top Level

How to define a method following top-level statements

You can keep using top-level statements and append additional members with a partial Program class.

using System;
Console.WriteLine("toplevel");
ThisShouldBeAMethodOfProgramClass();

public static partial class Program
{
public static void ThisShouldBeAMethodOfProgramClass()
{
Console.WriteLine("Static in Program class");
}
}

Where are methods defined at the ruby top level?

First of all, this behavior and the underlying reasoning have always existed; it's nothing new to 1.9. The technical reason it happens is because main is special and treated differently than any other object. There's no fancy explanation available: it behaves that way because it was designed that way.

Okay, but why? What's the reasoning for main to be magical? Because Ruby's designer Yukihiro Matsumoto thinks it makes the language better to have this behavior:

Is so, why are top-level methods not made singleton-methods on this object,
instead of being pulled in as instance methods on the Object class
itself
(and hence into all other classes i.e. more namespace pollution than is
usually intended). This would still allow top-level methods to call other
top-level methods. And if the top-level object were referred to by
some
constant like Main, then those methods could be called from anywhere
with
Main.method(...).


Do you really wish to type
"Main.print" everywhere?

Further on in the discussion, he explains that it behaves this way because he feels the "assumption is natural."

EDIT:

In response to your comment, your question is aimed at why main's eigenclass seems to report hello as a private instance method. The catch is that none of the top-level functions are actually added to main, but directly to Object. When working with eigenclasses, the instance_methods family of functions always behave as if the eigenclass is still the original class. That is, methods defined in the class are treated as being defined directly in the eigenclass. For example:

class Object
private
def foo
"foo"
end
end

self.send :foo # => "foo"
Object.private_instance_methods(false).include? :foo # => true
self.meta.private_instance_methods(false).include? :foo # => true

class Bar
private
def bar
"bar"
end
end

bar = Bar.new
bar.send :bar # => "bar"
Bar.private_instance_methods(false).include? :bar # => true
bar.meta.private_instance_methods(false).include? :bar # => true

We can add a method directly to main's eigenclass, though. Compare your original example with this:

def self.hello; "hello world"; end

Object.instance_methods.include? :hello # => false
self.meta.instance_methods.include? :hello # => true

Okay, but what if we really want to know that a given function is defined on the eigenclass, not the original class?

def foo; "foo"; end  #Remember, this defines it in Object, not on main
def self.bar; "bar"; end #This is defined on main, not Object

foo # => "foo"
bar # => "bar"

self.singleton_methods.include? :foo # => false
self.singleton_methods.include? :bar # => true

Why can't I define top-level extension methods in C# 9?

The C# Language Specification says:

When the first parameter of a method includes the this modifier, that method is said to be an extension method. Extension methods can only be declared in non-generic, non-nested static classes.

According to the language specification, extension methods must be declared in a static class.

It does not matter that top-level methods are implemented by placing them in a hidden static class. Top-level methods are (by definition) not declared in any class, and therefore cannot be extension methods according to the specification.

As with all language design questions, this is the way that it is because this is the way that the language design team designed the language. Presumably the same concerns which prevent you from defining extension methods inside non-static classes also apply to top-level methods.

You can open a discussion in the csharplang repo or ask a question on Gitter if you want someone with more authority to possibly give more detail.

What's the difference between Low-level functions & Top-level functions?

The closer it is to human language, the higher-level the function is.

The closer it is to machine language, the lower-level the function is.

I'm simplyfying but here are some examples:

High level functions:

Car.Start()
Car.MoveTo(Home)

Low level functions:

Car.Insert(Key);
if (Car.IsKeyInserted() == False)
return False;

Car.StartEngine();
Car.ApplyAccelerator(0.1f);
Car.ChangeGear();
Car.RotateWheel(-25);

To which object are top-level methods assigned in Ruby?

There is top-level object in Ruby -- main

def method_name(args)
# body
end
self
# => main

self.methods.grep(/method_name/)
# => [:method_name]

main is an instance of Object. Any methods defined in main become instance methods of Object

Object.private_instance_methods.grep(/method_name/)
# => [:method_name]

This makes them available everywhere (because all classes are descendants of Object), meaning that we can call the method without a receiver inside classes

For example

def foo
puts "foo"
end

class X
def y
foo
end
end

# will print foo
X.new.y

# will raise private method `foo' called for #<X:0x000055e406a0f060> (NoMethodError)
# to reproduce don't use irb, just usual file
X.new.foo

Read more

Confusion regarding top-level function

When you import express, what you get is a factory function. A factory function is a function that, when you call it, it creates an object for you. It's similar to using new with a constructor, but a factory function doesn't expose the constructor directly or the class directly. It just exposes you to a function that when you call it, it creates an object that you can then interact with by calling methods.

Imagine this behind the scenes:

class Express {
constructor() {
...
}
get() {
...
}
post() {
...
}
use() {
...
}
}

// factory function
function createExpressInstance() {
let obj = new Express();
// maybe do some other setup on the object
return obj;
}

// define function that will be referenced as express.static()
createExpressInstance.static = function() {
...
}

// export the factory function
module.exports = createExpressInstance;

A factory function is one design pattern for an API that can create new objects. Rather than exposing the class definition directly or the exporting the constructor directly, it "hides" the actual implementation. You call a function and get an object back.

There are some specific situations where a factory function is a preferred way of constructing a new object (particularly when asynchronous things might be involved in constructing the object), but in other situations (like this), it's just a style choice for how to write the code and how to design a public API.

In Javascript, is a constructor function the same as a class?

Yes and no. The class is the whole definition, including the methods. The constructor is just one method of that class definition.

But, you call the constructor by using the name of the class. So if (in my example above), you did console.log(typeof Express), you would get "function" because the symbol Express represents the constructor function.

What is a top-level statement in Python?

It's not just variable declarations (and there aren't any variable declarations anyway). It's pretty much anything that starts at indentation level 0.

import sys         # top-level

3 + 4 # top-level

x = 0 # top-level

def f(): # top-level
import os # not top-level!
return 3 # not top-level

if x: # top-level
print 3 # not top-level
else:
print 4 # not top-level, but executes as part of an if statement
# that is top-level

class TopLevel(object): # top-level
x = 3 # not top-level, but executes as part of the class statement
def foo(self): # not top-level, but executes as part of the class statement
print 5 # not top-level

What is top-level in C++?

This is not a strictly defined term. But in [dcl.init.list]/note-7 that you linked, "at the top level" seems to mean "written directly in a braced list, rather than in a nested expression".

So, in int x[] = {1.0, f(2.0)};, 1.0 is at the top level, because it's written directly in the braced list, but 2.0 is not because it's nested in a function-call expression.



Related Topics



Leave a reply



Submit