Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# Compiler Default Interfaces Support

Discussion in 'Experimental Scripting Previews' started by jGate99, Jun 5, 2020.

  1. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,935
    Hi there,

    As we already have a C# 8 Support Thread and C# 8 arrived in 2020.2 Alpha recently without Default Interfaces. So i'm creating this thread for this specific and highly anticipated feature. I hope @JoshPeterson keep sharing progress here regarding it.

    Thanks
     
    dadude123 likes this.
  2. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    789
    Default Interfaces comes with .NET standard 2.1 and they are currently working on a mono update to make this possible. But there is no ETA.
     
  3. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    I'll share details here when we have them. Thanks!
     
    Kirsche and jGate99 like this.
  4. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    296
    I am very interested in this feature as it allows me to create mixin classes.

    For example in a starfighter, I have guns, shields, and engine systems
    I created a mixin reactor interface to tie these elements to a power system.

    Since there's no default interface implementation support, I basically cut and paste the same code into my GunReactored, ShieldsReactored, and EngineReactored code!
     
    markkmlam and jGate99 like this.
  5. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    789
    What speaks against helper or abstract base classes here?
     
    VolodymyrBS likes this.
  6. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    296
    There is absolutely no relationship between guns, shields, or engines for anything in the game. In fact the gun code is general enough to be used in an FPS or an unrelated game. Likewise with the shields. An abstract base class at the bottom of these three systems would "work", but creates a sort of needless coupling.

    The idea of using a mixin to link their behaviors to a reactor core is a more proper way to decouple the behavior of these systems. The problem is, without default interfaces, I have to rewrite the actual implementation for each GunReactored or ShieldReactored class.

    In fact, doing away with Object Oriented Programming is even better for this use case. If I implemented this in ECS a Reactor component could simply read values from the Gun and GunReactored component, for example. Unfortunately, Unity DOTS won't be ready for another few years (if ever), so when default interfaces are supported, I'll be planning on refactoring a bit of my codebase
     
    jGate99 likes this.
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    For it to work as a default interface method, it needs to only access the members of the interface. That means that it could already be done with either extension methods or by calling static methods.

    Could you paste the public interfaces, because I'm failing to see what problem you're solving better though default interface implementations. It sounds like the control flow is wrong, but it's hard to tell from just type names.
     
  8. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    789
    Default Implementation have only acces public members of the same interface or parameters. And the default implementation is only available if you are working with the interface type. This also means that you have no access to the default implementation in a class that implements the Intrerface.
     
    Last edited: Dec 24, 2020
  9. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,084
    Step 1: Make IReactored interface
    Step 2: Make extension method for IReactored
     
  10. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    296
    This is probably a bit of a noob question, but what about instances where you. have a substantial amount of logic in a get/set property? I'm guessing I'll still have to cut/paste extension method calls into these properties independently?
     
  11. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,084
    Or rather call extension method that uses these properties.
     
  12. Imakhiil

    Imakhiil

    Joined:
    Oct 11, 2013
    Posts:
    96
    Any updates on this? It'd be very useful with the Burst compiler.
     
  13. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    789
  14. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,935
    It'd be really great if we also had default members
     
  15. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Can you clarify what you mean?
     
  16. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,935
    I know its not related to unity, but a C# limitation. I actually started implementing it and realized i have to write those fields explicitly in all the implemented classes.

    Now if C# itself had default members, then it'd be quite a time saver



    public interface IAdminDependencies
    {



    //member fields, i have to write these in all implenting classes
    IABusyIndicator ManagerBusyIndicator { set; get; }
    IAManagerDialogs ManagerDialogs{ set; get; }
    ABackendService ManagerBackend{ set; get; }
    IAScreensManagerNavigatorBase ManagerScreenNavigator{ set; get; }
    IAScreensManagerHistory ManagerScreenHistory{ set; get; }


    //default interface, no need to write code
    void SetupDependencies(IABusyIndicator managerBusyIndicator, ABackendService managerBackend, IAScreensManagerNavigatorBase managerScreenNavigator, IAManagerDialogs managerDialogs, IAScreensManagerHistory managerScreenHistory)
    {

    ManagerScreenHistory = managerScreenHistory;
    ManagerDialogs = managerDialogs;
    ManagerBackend = managerBackend;
    ManagerBusyIndicator = managerBusyIndicator;
    ManagerScreenNavigator = managerScreenNavigator;

    }


    }




     
  17. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Thanks for the information. This would need to be done in the C# language spec before Unity can pick it up.
     
    jGate99 likes this.
  18. eskivor

    eskivor

    Joined:
    Aug 5, 2015
    Posts:
    13
    'I'll share details here when we have them. Thanks!'
    Any news ?
     
  19. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    What details are you looking for? Sorry, I'm not clear on what you are asking.
     
  20. jiraphatK

    jiraphatK

    Joined:
    Sep 29, 2018
    Posts:
    293
    if you meant is Default Interface working? Yes it is. I've been using it on 2021.3