i don't keep up with it as well as i should, i sup...
# cfml-general
w
i don't keep up with it as well as i should, i suppose, but do the most recent versions of acf/lucee/boxlang support function overloading?
b
I just asked llama3.2,and it said yes... but I would test it to be sure. Maybe try cffiddle. Java apparently supports it, so it would make sense. Hope that helps.
b
No, overloading is not something dynamic languages can really do
In Java, all arguments to a method MUST be passed and they must exactly match the type, and the types must be known at compile time. Overloaded methods have the same name, but different arguments and/or types.
In CF, arguments are not only optional, but additional arguments can be passed, or an argument collection, or some combination thereof. There's no real concept of overloading methods, but we don't need it. We're already flexible enough such that you can make a single generic method respond dynamically to what's passed in.
CF DOES support method overriding, however. This is an OO construct in which methods on a child class "hide" the method on the parent class.
z
yup, cfml only checks that an argument can be cast to a type, it doesn't actually cast them https://trycf.com/gist/9ea69e5c718e71a54c2c348fde1728b9/lucee7?theme=monokai
Copy code
<cfscript>
    function test(string str, numeric num){
        dump(arguments);
        for (var a in arguments){
            dump(arguments[a].getClass().getName())
        }
    }
    
    test(1,2);
    test("1","2");
    test(1,"2");
    test("1",2);
    flush;
    try {
        test("1","lucee");
    } catch(e){
        echo(e);
    }
</cfscript>
b
Note BoxLang is unique in that it WILL cast the arguments inside the method to the declared type. Since we do the work of checking if we can cast it, it seemed a shame to not just use the casted value and save subsequent casting inside the method.
But the point remains, the variable types don't choose the method. The method enforces the variable types.
w
roger. thanks for the replies
b
Thanks bdw429 and zackster. Those are much better answers than mine. I learn stuff every day here.