Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use records with Bh1745 #1258

Merged
merged 11 commits into from
Nov 10, 2020
Merged

Use records with Bh1745 #1258

merged 11 commits into from
Nov 10, 2020

Conversation

richlander
Copy link
Member

@richlander richlander commented Nov 6, 2020

First test of using records in this codebase. This binding seemed like a perfect use case for records.

I made a few choices:

  • Defined the record to the primary code file. It doesn't warrant its own file. IMO, this is a best practice.
  • Defined the record first (before the primary type). If there were >2 records, I'd pull them out into their own file.
  • Defined IsExternalInit as part of the netstandard2.0 asset (required to use records pre .NET 5.0).
  • Unrelated, I also moved the (single) extension to the main file. That seemed like a poor practice, IMO.

These are all a first take. What do you think?

/// doc comments don't currently work for records due to dotnet/roslyn#44571. I don't think this should stop us from getting started with records.

Going forward, I think we should convert a small set of data-oriented classes to records, and see if we get any feedback on their usage. For this first case, I've gone with the (very pretty) immutable version of records. That seems to fit this case well. We may find that we want to enable mutable properties for some cases. That said, the with syntax is pretty compelling.

@ghost ghost added the area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio label Nov 6, 2020
@richlander richlander mentioned this pull request Nov 8, 2020
@@ -39,6 +39,10 @@
</Reference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0'">
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this condition be the other way around? Right now it is compiling the file on net5.0 but not on NS2.0

Copy link
Member

Choose a reason for hiding this comment

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

Discussed offline, we will instead move this type to iot.device.bindings and the binding itself and try to make it internal.

{
Red = 2.2, Green = 1.0, Blue = 1.8, Clear = 10.0
};
ChannelCompensationMultipliers = new (
Copy link
Member

Choose a reason for hiding this comment

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

While I don't have a big preference, I suppose that object initializer still works with records right? Like:

ChannelCompensationMultipliers = new ()
            {
                Red = 2.2, Green = 1.0, Blue = 1.8, Clear = 10.0
            };

Copy link
Member Author

Choose a reason for hiding this comment

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

If I do that, then the constructor isn't satisfied, and then I cannot use the pretty records syntax.

@@ -8,6 +8,7 @@

<ItemGroup>
<Compile Include="*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)/../Common/System/Runtime/CompilerServices/IsExternalInit.cs"/>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<Compile Include="$(MSBuildThisFileDirectory)/../Common/System/Runtime/CompilerServices/IsExternalInit.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)/../Common/System/Runtime/CompilerServices/IsExternalInit.cs">
<Link>Common/System/Runtime/CompilerServices/IsExternalInit.cs</Link>
</Compile>

Nit: Add a Link here for VS Experience.

Copy link
Member Author

Choose a reason for hiding this comment

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

If you switch the order of the TFMs in Bh1745.csproj or load these projects on a machine within .NET 5.0, you will get the following experience, with IsExternalInit.cs listed. Just ignore it.

image

@richlander richlander merged commit a0231d4 into master Nov 10, 2020
@richlander richlander deleted the records branch November 10, 2020 01:26
ZhangGaoxing pushed a commit to ZhangGaoxing/iot that referenced this pull request Mar 15, 2021
* Add records to Bh1745:

* Add tracking issue for /// docs

* Move record to primary class file

* Define records first

* Simplify sleep

* Add extension file

* Add object initializers

* Remove space

* Change extension method name

* Move IsExternalInit

* Update handling of IsExternalInit
@github-actions github-actions bot locked and limited conversation to collaborators Dec 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants