Giter Club home page Giter Club logo

Comments (7)

ArielG-NV avatar ArielG-NV commented on June 4, 2024

Tentative Plan:

  1. For an initialization list of a struct to be legal, it must match up to a constructor.
  2. We will only auto-gen a constructor with a parameter-list that is 1:1 to visible struct members. We cannot generate a ctor if a private member is part of a struct and uninitialized. We cannot generate a ctor if a internal member is part of a struct and uninitialized and we are not inside the original def'ed module scope. This calling rule is effectively inheriting the smallest scope visible member as the visibility of the __init {private,internal,public}.
  3. We (for now) only allow initializing all visible variables at once.
  4. We won't auto-gen constructors if an already valid constructor exists. We will use the valid constructor instead.
  5. init-lists may call user-defined constructors
  6. We will demote a default ctor to internal if internal variables are being init'd.
  7. initializer lists ignore all varDecl init expressions.

Examples:


  1. assume
struct S
{
     private int m1;
     public int m2;
}

S test()
{
     S s = {1,2};
     return s;
}

This will error, there are more init list elements than public variables


  1. assume
struct S
{
     private int m1;
     public int m2;
     __init(int in1, int in2)
     {
         m1 = in1;
         m2 = in2
     }
}

S test()
{
     S s = {1,2};
     return s;
}

This will work, user made constructor has priority


  1. assume
struct S
{
     public int m1;
     public int m2;
}

S test()
{
     S s = {1,2};
     return s;
}

This will work, 2 public int members, 2 elements inside the parameter-list


  1. assume
struct S
{
     private int m1;
     public int m2;
     private int m3;
     public int m4;
}

S test()
{
     S s = {1,2};
     return s;
}

This will error, we cannot create an implicit ctor from an initializer-list when uninitialized private variables are inside a struct.


  1. assume
struct S
{
     private int m1;
     public int m2;
     private int m3;
     public int m4;

     __init(int in1, int in2)
     {
          m1 = in1;
          m2 = in2;
     }
}

S test()
{
     S s = {1, 1};
     return s;
}

This will work, user made constructor has priority


  1. assume
struct S
{
     private int m1 = 0;
     public int m2;
     private int m3 = 1;
     public int m4;
}

S test()
{
     S s = {1,2};
     return s;
}

This will work, we can create an implicit ctor from an initializer-list when all private variables are initialized inside a struct.


  1. assume
// myFile_1.slang
struct S
{
internal int m1 = 0;
public int m2;
}
...
S s = {1,2}; // works from the same module.
S s = {1}; // does not work from the same module
// myFile_2.slang
S s = {1,2}; // does not work if called from a different module because m1 is internal (not visible).
S s = {1}; // does work since only m2 is visible and m1 is default initialized.
  1. assume
struct S
{
     private int m1 = 0;
     public int m2;
     private int m3 = 1;
     public int m4;
}

S test()
{
     S s = {1};
     return s;
}

???

from slang.

csyonghe avatar csyonghe commented on June 4, 2024

We should make 6 an error, because private members must have an explicit initializer for implicit generation of ctor. If we have uninitialized private members, do not generate a ctor, or generate a ctor that will result an error message if it is ever called.

from slang.

ArielG-NV avatar ArielG-NV commented on June 4, 2024

updated 👍 (also changed the example numbers to make more sense)

from slang.

csyonghe avatar csyonghe commented on June 4, 2024

Just want to add that


struct S
{
     private int m1 = 0;
     public int m2;
}

S s = {1}; // works.

from slang.

csyonghe avatar csyonghe commented on June 4, 2024

struct S
{
internal int m1;
public int m2;
}
S s = {1,2}; // works from the same module.

S s = {1,2}; // does not work if called from a different module, because ctor is not visible.

from slang.

ArielG-NV avatar ArielG-NV commented on June 4, 2024

I have a question about when an initializer-list is a partial-completion to the public/internal member list:

struct S
{
     private int m1 = 0;
     public int m2;
     private int m3 = 1;
     public int m4;
}

S test()
{
     S s = {1};
     return s;
}

This would error since no user-ctor && not 1:1 for each public member? Or not error and allow partial init?

from slang.

csyonghe avatar csyonghe commented on June 4, 2024

I think we just don't allow this for now.

from slang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.