Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

System.Diagnostics.Contracts (compared to UnityEngine.Assertions)

Discussion in 'Scripting' started by svobodajak, Aug 12, 2020.

  1. svobodajak

    svobodajak

    Joined:
    Jun 27, 2017
    Posts:
    11
    Hi!

    I would like to ask someone with experience of using C#'s standard Contracts in Unity:
    a) How well does Ensures, Requires, Invariant, OldValue work in Unity?
    b) Does the static analysis work as expected?
    c) What is the support for this in release?
    d) Is there some more significant performance hit?

    I am asking because I only found one post of someone saying that something from Contracts does not work and not much else about it.

    I know about the Unity's own Assertion library, that it is built to work nicely with Unity's ecosystem, that they are pretty light-weight and so on. But Contracts are much more powerful and I'd like to use them, so are there any big downsides compared to the Assertion library?

    Background info: I'm currently working on a piece of code that I might consider releasing later as a library/asset. To that end I thought it would be good to use some Assertions or Contracts.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I have never used any of this newage chuff, never even looked it up in a Unity context, don't even know if the Unity version of C# supports it.

    It sounds like perhaps you are the person best-suited to do some experiments and let us all know what you find!
     
  3. mannyhams

    mannyhams

    Joined:
    Feb 6, 2015
    Posts:
    34
    @svobodajak How did you find using the Contract class in Unity projects? I'm new to doing serious stuff in Unity and wondering about this, too.

    EDIT: Rider prompted me to select how I want to do null checking... more options than I expected to evaluate!
     
    Last edited: Jan 12, 2021