alliedmodders / sourcepawn

A small, statically typed scripting language.
Other
363 stars 62 forks source link

error 035: argument type mismatch #735

Open wS-SM opened 2 years ago

wS-SM commented 2 years ago
void V(int &i) {
    i = 5;
}

public void OnPluginStart()
{
    int a = 0;
    int b = 0;
    int c = 0;
    V(c ? a : b); // error 035: argument type mismatch (argument 1)
    PrintToServer("%d %d", a, b);
}

sm 1.10-1.11 In C++ this works, but SourcePawn is drunk =)

dvander commented 2 years ago

In the compiler we must convert the lvalue (int&) to an rvalue (expression result), and there is no conversion back after that. This has been the behavior since at least 1.9. In C++ this works because its type system is much finer grained and more formally specified. It has the concept of "expression that is an l-value" and our compiler does not. Eg, this would probably work in C++:

(c ? a : b) = 5;

Anyway, I'm not super excited to try and fix this - it'd be very invasive to the semantic analysis pass and is too risky for 1.11. In addition not many languages would match C++'s behavior. C#, which has reference types, matches SourcePawn here.