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

[Proposal] primitive struct as primitive #16095

Open
Thaina opened this issue Dec 24, 2016 · 5 comments
Open

[Proposal] primitive struct as primitive #16095

Thaina opened this issue Dec 24, 2016 · 5 comments

Comments

@Thaina
Copy link

Thaina commented Dec 24, 2016

Primitive type such as string and int have a special ability. Such as make it const, use it on default parameter (because it is const), or make it a base class of enum (actually it only for integer but that also included in this proposal)

I think it useful if we could have struct that contain only primitive things also being primitive. And if it contains only integer it should also be integer. So struct that which is primitive can be const field. Can be set default parameter. And if it is integer struct it would be able to make enum from it

public struct Color
{
    public byte R,G,B,A;
}

public enum Colors : Color // all field is byte so it is also integer
{
    Red = new Color(255,0,0,255),
    Green = new Color(0,255,0,255),
    Blue = new Color(0,0,255,255),
}

public struct Vector3
{
    public float X,Y,Z;
    public const Zero = new Vector3(0,0,0);
}

public struct Config
{
    public Vector3 Up;
    public string Name;
    public static void SetConfigToEnv(Config con = new Config("test",new Vector3(0,1,0)))
    {
    }
}

This should also cope with Tuple and other generic struct. If its generic contains only primitive it would became primitive

@Unknown6656
Copy link

Mildly related: #15079

@ufcpp
Copy link
Contributor

ufcpp commented Dec 25, 2016

Related to const string, I want const binary data. const strings are stored in the User String matadata table. the User String stream is actually a blob heap and can contain any binary data. For instance, const binary is required for Utf8String literals. This should be general mechanism for not only Utf8String but arbitrary types.

@svick
Copy link
Contributor

svick commented Dec 28, 2016

The enum part of this proposal sounds pretty similar to #6739.

@Thaina
Copy link
Author

Thaina commented Jan 18, 2017

Have found idea #16193

I think my proposal could be extend a bit to cover this

By count enum as integer type. Then count struct that contains only enum and integer type as integer type

Like so it could be done as

struct Thing
{
    public Colors Color;
    public Models Model;
}

enum Things : Thing
{
    RedBox = new Thing(Models.Box,Colors.Red),
    BlueBox = new Thing(Models.Box,Colors.Blue),
    RedSphere = new Thing(Models.Sphere,Colors.Red),
    BlueSphere = new Thing(Models.Sphere,Colors.Blue),
}

enum Colors : Color
{
    Red: new Color(255,0,0),
    Green: new Color(0,255,0),
    Blue: new Color(0,0,255),
}
enum Models
{
    Sphere:0,
    Box:1,
}

////

var obj = Things.RedBox;
var objColor = Things.RedBox.Color;

@Thaina
Copy link
Author

Thaina commented Oct 22, 2023

I don't remember why I close this issue, might be mistake. Is it possible to move this into https://github.com/dotnet/csharplang repo?

@Thaina Thaina reopened this Oct 22, 2023
@arunchndr arunchndr added this to the Backlog milestone Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants