GithubHelp home page GithubHelp logo

Comments (9)

03l54rd1n3 avatar 03l54rd1n3 commented on May 20, 2024 4

Unlikely may not mean certain. Still, certain as of a few days ago this attribute will be available starting from C# 10 onwards. See dotnet/roslyn/pull/54839

from guard.

julealgon avatar julealgon commented on May 20, 2024 2

In the meantime

@safakgur now that C#10 has officially launched, do you know when support for CallerArgumentExpression will be in place? I'm actually planning a migration to Guard in a sizeable solution soon and it would be really nice to have this supported by then as it would remove the need for an additional cleanup pass in all the code removing the parameter name argument later.

from guard.

safakgur avatar safakgur commented on May 20, 2024 1

Sorry everyone, I neglected Guard a lot during the last year because of other responsibilities (the pandemic definitely didn't help). But I'm still working on v2, our next major version which I hope to release before March.

@julealgon - Yes, it's coming with v2! The new version will also also have some generic validations that accept a bool condition marked with this attribute, so both the parameter name and the message containing the failed condition can be retrieved automatically (Think of Argument(age).Range(age >= 18) throwing an ArgumentOutOfRangeException with the param name "age" and the message "Precondition failed: age >= 18").

With that said, v2 will include a lot of other changes, some of which will be breaking changes, so you may not want to migrate a sizeable solution to use Guard before that.

I hope that helps!

from guard.

alexrp avatar alexrp commented on May 20, 2024

The attribute has been added to the BCL but it does not actually do anything yet: dotnet/csharplang#287

from guard.

julealgon avatar julealgon commented on May 20, 2024

Oh wow! I was one of those who got misled by the documentation. Had no idea this was not yet supported.

Thanks @alexrp for clarifying.

I wonder though, would it make sense to add the needed code to support it right now, so that once it starts working there is no extra work to do? Since they have already published the attribute it's unlikely it will suffer any further breaking changes.

from guard.

patrikmolsson avatar patrikmolsson commented on May 20, 2024

In the meantime - in order to utilize this functionality - you can make a small abstraction on top of this library in your own code:

public static class MyGuard
{
    public static Dawn.Guard.ArgumentInfo<T> Argument<T>(T argument, [CallerArgumentExpression("argument")] string? argumentName = default)
    {
        return Dawn.Guard.Argument(argument, argumentName);
    }
}
public class GuardTests
{
    [Fact]
    public void SourcesMemberName()
    {
        // Arrange
        var expectedErrorMessage = $"year cannot be less than 0. (Parameter 'year'){Environment.NewLine}Actual value was -1.";

        // Act
        var ex = Assert.Throws<ArgumentOutOfRangeException>(() => new Example(-1));

        // Assert
        Assert.Equal(expectedErrorMessage, ex.Message);
    }

    private class Example
    {
        public Example(int year)
        {
            MyGuard.Argument(year).Min(0);
        }
    }
}

from guard.

julealgon avatar julealgon commented on May 20, 2024

I'm still working on v2, our next major version which I hope to release before March.

@safakgur do you have any updates to share on v2?

from guard.

sgf avatar sgf commented on May 20, 2024

@safakgur

from guard.

safakgur avatar safakgur commented on May 20, 2024

Hi folks, this was one of the things I wanted for v2 but I'm retiring Guard - I apologise for any inconvenience, and thank you for all the support. The readme has details.

from guard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.