A Method Without Parameters Is Calling for an Argument

A method without parameters is calling for an argument

These methods are not class methods, they are instance methods. You must call them on an instance of the Location class, not on the class itself. Evidently, Swift can call instance methods similarly to Python: the method is a function owned by the class, and its argument is an instance of the class. But you should not call instance methods this way.

The best way to solve this problem is to construct a Location object and then call the method on it:

let city: Location = Location().getCity()

How to call method without parameter in java reflection

How to call method without parameter in java reflection?

Don't give it an argument if it doesn't expect one.

Does Java support such feature?

Yes.

Method m2 = c.getDeclaredMethod("method2");
m2.invoke(this);

or

c.getDeclaredMethod("method2").invoke(this);

BTW Technically this is the first implicit argument. If you have no arguments the method has to be static which is called like this.

static void staticMethod() { }

called using

c.getDeclaredMethod("staticMethod").invoke(null);

Calling static method without passing parameters

You're passing the function, not invoking it. This allows a different piece of code to invoke the function later, supplying the arguments.

The way this is done in C# is through delegates (since about .NET 3.5, instead of using custom delegate types, you will probably want to use Action and Func respectively). You can think of a delegate as a single-method interface - it's basically a bridge between object-oriented and true functional programming.

In older code, this was mainly used in events and callbacks. Today, as C# gets more and more functional, it's getting rather common for abstract functions to accept functions as arguments; LINQ was probably the first big example. You supply your own behaviour to other functions. You want to filter a collection? Just pass a function that does the filtering (col.Where(i => i.Name.Length > 3)).

To show you a possible way of implementing what you're seeing in your code:

public class Rule<T> : Rule
{
private readonly Func<T, Rule, InspectionResult> _inspect;

public Rule(string name, string ruleId, Func<T, Rule, InspectionResult> inspect)
: base(name, ruleId)
{
_inspect = inspect;
}

public InspectionResult Inspect(T model) => _inspect(this, model);
}

Note that when Rule<T> calls the delegate, it must supply all of the arguments.

Of course, the actual behaviour of the caller can be essentially arbitrary. The point is that you're passing a behaviour to someone else. Delegates are a very simple and quick way of doing that, especially combined with anonymous functions and all that.

A function without parameters in C

  1. Per the letter of the standard, yes. 5.1.2.2.1 says "[main] shall be defined with a return type of int[,] and with no parameters, or with two parameters [...]". int main() {} is a definition with return type int and no parameters, and 5.1.2.2.1 does not say anywhere that the definition is required to be prototyped.

    (The code fragment shown immediately after "and with no parameters" may appear to be making a requirement to write (void), but all code fragments are considered to be examples, and therefore non-normative. It doesn't actually say this anywhere in the text, but it has come up in DR responses at least a couple times; I regret I do not remember specific numbers, it was more than 15 years ago now.)

    However, because of (2), you should write int main(void) anyway.

  2. Section 6.5.2.2 (semantics of function calls), paragraph 8, says "the number and types of arguments are not compared with those of the parameters in a function definition that does not include a function prototype declarator." However, paragraph 6 of the same section says that in the event of an actual mismatch between the definition and the callsite the behavior is undefined. That means, compilers are allowed to issue an error when a function defined as T foo() {} is called with arguments, but it also means they are allowed not to issue an error.

    For the sake of backward compatibility with pre-C89 code, which regularly did make function calls with a mismatched number of arguments, many compilers will accept such code without complaint. I have seen this behavior with Clang and MSVC as well as GCC. Newer versions of Clang do give you a warning:

     $ clang --version | head -1
    clang version 9.0.1-13
    $ clang test.c
    test.c:14:14: warning: too many arguments in call to 'bar'
    bar(1, 12);
    ~~~ ^

    I don't know how long this warning's been on by default.

    Someone ought to go through all of GCC's intentional leniences for the sake of backward compatibility with really old code, and turn many of them off by default, but nobody's interested in funding this so it probably won't ever happen.

Passed function without arguments doesn't throw an error

Ts Playground

Typescript Conditional Operator to the rescue... somewhat?

type FnStrongTypedOrNever<T extends (arg: any) => any, TConstraint extends any[]> =
[...Parameters<T>, ReturnType<T>] extends TConstraint ? T : never;

function functionA<T extends (arg: any) => any>(call: FnStrongTypedOrNever<T, [string, boolean]>) {
call("")
}

function functionB() {
return true;
}

function functionD(name: string) {
return true;
}

function functionC() {
functionA(functionD); // this doesn't throw a syntax error
functionA(functionB); // this throws a syntax error
functionB(""); // this throws a syntax error
}

A quick & dirty solution using typescript's extends syntax. With some additional type magic.

The type FnStrongTypedOrNever<T, TConstraint> returns never if the function does not respect the constraint.

So, at the end of the day... the error you receive is Argument of type '() => boolean' is not assignable to parameter of type 'never'.

At the moment this is the best you can do with TS (as far as I know)

EDIT:

Updated solution

This one has a better error message via TConstraint & '' usage instead of never

Now you can clearly see that the parameter list is not matching.

This is still a type of hack, and as far as I know you cannot change the ambient types to make this globally work.

How to pass a variable between methods without parameters and arguments

You have a spelling error in your getTotalPayment() method.

What your trying to do is call the method getmonthlyPayment() when you should be calling getMonthlyPayment().

Incase you missed the suttle difference in my answer you have a lowercase 'm' when you want an uppercase 'M'.

Im not entirety sure if this is your problem, but its the only syntax error my IDE is telling me.



Related Topics



Leave a reply



Submit