Skip to content

[Bug]: #370 [ObservableAsProperty] on partial property not initializing backing field -> Doesn't work with empty string #406

@MartinFayRAX

Description

@MartinFayRAX

Describe the bug 🐞

A brief initial report, please let me know if you'd like a full repro.

Setting InitialValue for ObservableAsProperty on a (not-nullable) string to empty string doesn't work. FYI in this case the initialization is I think practically a placeholder as the IObservable will provide the "real" value once set. This makes me wonder if InitialValue should actually be necessary in this case, i.e. should initialization to empty string occur automatically?

(I've also been idly pondering if InitialValue should be typed to object? which is what the NUnit [TestCase] uses for parameters allowing values to be written "normally" i.e. no quotes on an int literal and so on - and shock horror, this also makes me wonder if I should be tiptoeing towards technical contribution...)

This user code:
[ObservableAsProperty(InitialValue = "")]
public partial string PLCActive { get; }

Leads to this generated code:
///
private string _pLCActive;

It should be:
///
private string _pLCActive = "";

Step to reproduce

  1. Create a non-nullable string OAPH property without InitialValue, this will lead to a nullability warning "not initialized"
  2. Add InitialValue = "", still "not initialized.
  3. Change this to i.e. InitialValue = "?", no longer a warning

Reproduction repository

https://github.com/reactiveui/ReactiveUI

Expected behavior

Empty string should be supported, and maybe there should be no need for an InitialValue in this case?

Screenshots 🖼️

No response

IDE

Visual Studio 2022

Operating system

Windows

Version

11

Device

No response

ReactiveUI Version

ReactiveUI.SourceGenerators 2.6.30

Additional information ℹ️

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions