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

Document if capable generating readonly struct #114

Closed
dzmitry-lahoda opened this issue Jan 11, 2019 · 2 comments
Closed

Document if capable generating readonly struct #114

dzmitry-lahoda opened this issue Jan 11, 2019 · 2 comments
Labels

Comments

@dzmitry-lahoda
Copy link

dzmitry-lahoda commented Jan 11, 2019

Given:

public struct Point3D
{
    public double X;
    public double Y;
    public double Z;
}

Generate:

readonly public struct ReadonlyPoint3D
{
    public readonly double X;
    public readonly  double Y;
    public readonly  double Z;
}

See Write safe and efficient C# code

Generate extension via System.Runtime.CompilerServices.Unsafe

var rw = new Point3D();
ref readonly ReadonlyPoint3D roExtension = rw.ToReadonly();
ref readonly ReadonlyPoint3D ro = UnsafeExtensions.Cast<Point3D, ReadonlyPoint3D>(ref fw);

For inspiration:
https://gitlab.com/dzmitry-lahoda/learning/blob/master/design/dod-ecs/Dod.Ecs/Ecs.cs
dotnet/csharplang#186

For:

interface IX
{
  ref Point3D P {get;set;}
}

Generate:

interface IReadonlyX
{
  ref readonly ReadonlyPoint3D P {get;}
}

May some other fun things you already do, but struct oriented.

@AArnott
Copy link
Owner

AArnott commented Jan 16, 2019

I'm not sure what you're proposing or asking for here. So you know, this project generates new code to complement the code you've written using partial types. We can't change the code the user already wrote in this model.

@dzmitry-lahoda
Copy link
Author

dzmitry-lahoda commented Jan 16, 2019

I meant if there is any logic in project to work with structs? But evident that it is not relevant here. Sorry.

I doing some weird stuff at runtime dotnet/csharplang#1620 (comment) and

       public readonly struct reflect
        {
            
            private class taga : Attribute {} [taga]
            public readonly long a;

            private class tagb : Attribute {} [tagb]
            public readonly float  b;

            private class tagc : Attribute {} [tagc]
            public readonly double  c;

            private class tagd : Attribute {} [tagd]
            public readonly decimal  d;

            private class tage : Attribute {} [tage]
            public readonly BigInteger  e;

            

            // this will fail on during type init if there is no taga or taga marked wrong type as part of static init
            public reflect UpdateA(long zz) => UnsafeToolsTests.Update<reflect, reflect.taga, long>(in this, zz);
        }

        private static class Change<TType,TTag, TValue>
            where TType:unmanaged
            where TTag: Attribute
        {
            public static int offset;

            static Change()
            {
                // VALIDATE that filed tagged by tag is TValue
                // Change<TType>.Validate() to validate that TType has all fields tagged.
                // TODO: find ofsett of filed onto tag for ZERO COST GENERIC SUTCT ACCESS later
                offset = 42;
            } 
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static TType Update<TType, TTag, TValue>(in TType v, in TValue t)
            where TType:unmanaged
            where TTag: Attribute        
        {
            var attr = Change<TType, TTag, TValue>.offset;
            var x = new TType();
            throw new NotImplementedException("Unsafe.Add v -> Unsafe.Copy t into x from v");
            return x;
        }



        [Fact]
        public void Test()
        {
            reflect rrrr = default;
            reflect rrrru = rrrr.UpdateA(123);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants