Skip to content

Conversation

@michaelnebel
Copy link
Contributor

@michaelnebel michaelnebel commented May 26, 2023

It turns out that there is yet another way, one can define a parameter default value by using the attribute (DefaultParameterValue) to instruct the compiler, how the default value should be constructed using an implicit conversion operator.

The following is valid C# code:

public struct MyStruct
{
    public static implicit operator MyStruct(string s) => new MyStruct();
}

public class MyClass
{
    public void M([Optional, DefaultParameterValue("mystring")] MyStruct arg) => throw null;
}

The compiler checks that there exists a way to construct (in this case an implicit conversion operator) a value of type MyStruct from a string.
Intuitively, the definition of M is equivalent to (which is not legal C# code).

public void M(MyStruct arg = "mystring") => throw null;

In this PR we try to emulate this behaviour.

@github-actions github-actions bot added the C# label May 26, 2023
@michaelnebel michaelnebel force-pushed the csharp/paramdefaultimplicitconversion branch from 4bca664 to bfe5ef9 Compare May 31, 2023 09:09
@michaelnebel michaelnebel force-pushed the csharp/paramdefaultimplicitconversion branch from bfe5ef9 to 5f42b7c Compare May 31, 2023 12:45
@michaelnebel michaelnebel force-pushed the csharp/paramdefaultimplicitconversion branch from 5f42b7c to 83a8e3b Compare May 31, 2023 12:47
@michaelnebel michaelnebel added the no-change-note-required This PR does not need a change note label Jun 1, 2023
@michaelnebel michaelnebel marked this pull request as ready for review June 1, 2023 06:47
@michaelnebel michaelnebel requested a review from a team as a code owner June 1, 2023 06:47
hvitved
hvitved previously approved these changes Jun 1, 2023
Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me 🎉

@hvitved
Copy link
Contributor

hvitved commented Jun 1, 2023

Did you run DCA already?

tamasvajk
tamasvajk previously approved these changes Jun 1, 2023
@michaelnebel
Copy link
Contributor Author

Did you run DCA already?

Not yet - but I will do so :-)

@michaelnebel michaelnebel dismissed stale reviews from tamasvajk and hvitved via 9aeb238 June 1, 2023 08:21
@michaelnebel michaelnebel requested review from hvitved and tamasvajk June 1, 2023 16:54
@michaelnebel
Copy link
Contributor Author

DCA looks good!

@michaelnebel michaelnebel merged commit 3a3f9a2 into github:main Jun 2, 2023
@michaelnebel michaelnebel deleted the csharp/paramdefaultimplicitconversion branch June 2, 2023 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C# no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants