hadashiA / VYaml

The extra fast, low memory footprint YAML library for C#, focued on .NET and Unity.
MIT License
295 stars 16 forks source link

Type error when using constructor initialization #95

Closed MonoLogueChi closed 3 months ago

MonoLogueChi commented 4 months ago

eg

[YamlObject]
public partial class MyClass
{
  public MyClass(int a = 0, float b = 0f)
  {
    A = a;
    B = b;
  }
  public int A { get; set; }
  public float B { get; set; }
}

will generate

var __A__ = 0;
var __B__ = 0;

The type of B should be float, not int

MonoLogueChi commented 4 months ago

This method may solve the problem, but I'm not familiar with SourceGenerator and I'm not sure if this is correct.

https://github.com/hadashiA/VYaml/blob/b06c5ca126852eafdd669c0841f68db98d509dfd/VYaml.SourceGenerator/VYamlIncrementalSourceGenerator.cs#L442

- codeWriter.Append($"var __{memberMeta.Name}__ = ");
+ codeWriter.Append($"{memberMeta.FullTypeName} __{memberMeta.Name}__ = ");
MonoLogueChi commented 3 months ago

Enum also has the same problem and will be defined as int type

IXLLEGACYIXL commented 3 months ago

it would be better to generate it like so

var __TEMP__T= default(double);
var __TEMP__X= default(int);

this code is allready used to generate the temp vars in deserialize

hadashiA commented 3 months ago

Thanks for the report. Fixed in #102.