mindplay.dk
mindplay.dk

Reputation: 7350

Is there a programming language where functions don't take arguments?

I know, this is a weird question.

Is there a programming language where you can't pass arguments to functions?

Every language and framework in recent years struggles to use patterns like dependency injection everywhere, and it made me wonder - what if everything was automatically injected somehow, by default, everywhere, all the time, without even giving you the option of doing anything else?

So arguments to functions would always be completed from available symbols in the local source.

For one, this would force you to always name any local symbols consistently with their intended use.

For example - and this is totally fictive syntax:

class UserService {
    ...
    function login(username: string, password: string) { ... }
}

class UserController {
    function login(userService: UserService, post: PostData) {
        var username = data.get('username')
        var password = data.get('password')
        u.login() // arguments automatically filled
    }
}

Arguments to constructors would be automatically filled as well.

Object properties declared in the local source might be considered for filling arguments as well.

Arguments passed to the local function would be considered too, as they are really just local variables, and this would make things like dependency chains an implicit thing - so if your class needs a database connection not for it's own use, but just so it can pass it to some other aggregate object, the connection object could automatically be injected by just declaring the dependency in the form of a new argument; callers of that function would automatically fulfill the argument if they have a suitable arguments available, eliminating the need to pass around indirect dependencies.

Has anything like that (or similar) been attempted in any existing language?

Upvotes: 1

Views: 183

Answers (1)

Renatoc8
Renatoc8

Reputation: 28

I don't know of any. It almost seems like a feature that programming languages would try to avoid because on larger projects, you would end up with thousands upon thousands of variables, which would be very confusing to keep track of, not to mention a lot of wasted RAM.

Most programming languages try to encourage you to use only what you need, which is good for performance and lowers the RAM overhead. If you had all of your variables in the global scope, memory leaks would likely be a common problem too.

So, although this is an interesting idea, I'm not sure how practical it would be. And if a language like this does exist, it would likely be limited in what types of tasks it can/is designed to accomplish.

Upvotes: 1

Related Questions