Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Why when the method use a generic I can return structs ?

Discussion in 'Burst' started by lberte, Jan 17, 2024.

  1. lberte


    Mar 19, 2016
    With the code :
    Code (CSharp):
    2. [BurstCompile]
    3. public static class TestUtil
    4. {
    5.     [BurstCompile]
    6.     public static int3 ItDoesntWork()
    7.     {
    8.         return;
    9.     }
    11.     [BurstCompile]
    12.     public static int3 ItWorksWithGeneric<T>()
    13.     {
    14.         return;
    15.     }
    16. }

    Only the first method generates an error while compile the job : "Burst error BC1064: Unsupported return type `Unity.Mathematics.int3`".

    When my method use a generic, I don't have an error. Why ?
    In my code, I have a lot of static methods that return structs. Should I avoid this ? What are the consequences ?
    Last edited: Jan 17, 2024
  2. Spy-Master


    Aug 4, 2022
    The second one doesn't "work." Each entry point is compiled to one method, so what type argument should it use? The answer is "you can't choose," so it shouldn't be compiled as a Burst entry point. You can check it yourself in the Burst Inspector, the method shouldn't be there. Calling that method from other Bursted code will be Burst-compiled since you have a known type argument, and managed code calling the method will only be calling the original, managed method.

    Not being able to pass structs (besides single field handle structs) is a known constraint on Burst entry points.
    You can just add an out parameter for the return value instead.

    Other pages:
    lberte likes this.
  3. lberte


    Mar 19, 2016
    Thank you for your detailed response. :)

    I didn't understant that there was a difference if the method was called from other Bursted code or from managed code.
    I'm still surprised that the method with the generic doesn't cause at least a warning.