Search Unity

Using ref/out with default values in functions

Discussion in 'Scripting' started by coppertones, Jun 15, 2018.

  1. coppertones

    coppertones

    Joined:
    Mar 31, 2018
    Posts:
    16
    I want to know how to make a function that works in a very specific way: with both out/ref and default values (in the same parameter)
    Code (CSharp):
    1. public static void decomposeName(string name, ref string type, ref int sides, ref bool flip, ref float twist) {
    2.     //Edit values of type, sides, flip & twist based on name
    3. }
    If I try default values, it gives
    Cannot specify a default value for the `ref' parameter
    I've also tried declaring in the same line (i know it's not reader friendly), but it throws
    Feature `declaration expression' cannot be used because it is not part of the C# 4.0 language specification
    I don't know if there's a way to do what I want, or if I'm better of assigning outputs to global variables.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You have to change your .Net target in Unity to use part of that. However, as the other errors states, you cannot have default values for those, anyways.
     
  3. coppertones

    coppertones

    Joined:
    Mar 31, 2018
    Posts:
    16
    Actually, I realized the only reason I was thinking of using default values was because that makes them optional. Is there a way to do that? (Do I need recursive functions with the same name?)
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    The idea of a ref/out having a default value doesn't make sense. Since the ref/out implies that it needs to access a part of memory scoped outside of the function. An optional parameter subverts that entire behaviour. What is being 'ref'd if nothing is passed in to be ref'd?

    ...

    Anyways, as you stated, you just want to have optional values:
    And yes, this is done with... well, not 'recursive functions', because a 'recursive function' is a function that calls itself (recursively looping in on itself).

    What you're looking for is 'overloaded functions/methods'. Where you have 2 or more methods named the same but with different parameters. Like this:

    Code (csharp):
    1.  
    2. public static void DecomposeName(string name)
    3. {
    4.     //one version
    5. }
    6.  
    7. public static void DecomposeName(string name, ref string type)
    8. {
    9.     //another version
    10. }
    11.  
    The compiler figures out which you want called based on the parameters passed (count and type... note, type resolution can be an issue if you have 2 overloads with implicitly converted types).

    And if you want a 'default value', you can just do something like:
    Code (csharp):
    1.  
    2. public static void DecomposeName(string name)
    3. {
    4.     string type = "SomeDefaultValue";
    5.     DecomposeName(name, ref type);
    6. }
    7.  
    8. public static void DecomposeName(string name, ref string type)
    9. {
    10.     //another version
    11. }
    12.  
    This is actually how C# accomplished 'optional parameters' in older versions of C# before it was added.

    Question though... why are all these parameters ref/out parameters? I mainly ask because the concept of 'optional ref parameters' like in your OP not working demonstrates a degree of not fully understanding the purposes of ref/out. So I'm wondering if you truly 'need' ref/out... I don't mean to offend with this, it's just the statements come off as novice, and misuse of ref/out is also a novice thing as well, so it may be good to use this as a learning moment if that is the case. If instead it's just a lack of knowing the language technicalities and you DO need ref, ignore this all together.
     
    shekalo likes this.