Created
November 16, 2022 07:25
-
-
Save cdiggins/202880ab1476e78fc39d0782fbdb6093 to your computer and use it in GitHub Desktop.
Overview of different ways to represent types in C# compared to Plato
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Here is the basic class | |
public class Vector2_ClassNaive | |
{ | |
public double x, y; | |
} | |
// Once we decide immutability is a good thing | |
public class Vector2_ClassWithFields | |
{ | |
public readonly double x, y; | |
} | |
// Same thing, I guess? | |
public class Vector2_ClassWithProperties | |
{ | |
public double x { get; init; } | |
public double y { get; init; } | |
} | |
// Instead of a class we can also use a struct (might be faster) | |
public struct Vector2_Struct | |
{ | |
public readonly double x, y; | |
} | |
// Structs let us declare the whole things as "Readonly" (which they should be) | |
public readonly struct Vector2_ReadOnlyStruct | |
{ | |
public readonly double x, y; | |
} | |
// What is a "ref struct" and does it have advantages? Not clear from the docs. Answer is no. | |
public readonly ref struct Vector2_ReadOnlyRefStruct | |
{ | |
public readonly double x, y; | |
} | |
// This is similar to the class, but gives us some boilerplate | |
public record Vector2_Record(double x, double y); | |
// Can't use readonly, so could write instead | |
public record Vector2_RecordLongForm | |
{ | |
public readonly double x, y; | |
} | |
// Now in C# 10 we can also do this, which is great but not widely supported | |
public readonly record struct Vector2_RecordStruct(double x, double y); | |
// In Plato we write, get a lot more boilerplate, and are backwards compatible | |
// See: https://github.com/cdiggins/plato/blob/main/PlatoStandardLibrary/math.types.plato.cs | |
// And: https://github.com/cdiggins/plato/blob/main/PlatoStandardLibrary/math.types.plato.g.cs | |
[Vector] | |
class Vector2 | |
{ | |
double x, y; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment