Repository navigation
[Question] Is there a way to call delegating constructor? #409
Description
Activity
It seems there's no way.
IIRC, you should also be able to delegate to any
outparameter: https://cpp2.godbolt.org/z/TGWP6fKWY.f: (out x: t) = { x = 0; } t: type = { a: i32; operator=: (out this, b: i32) = { a = b; } operator=: (out this, c: std::string) = { f(this); } } main: () -> int = t("").a;main.cpp2(6,5): error: expected 'a = ...' initialization statement - an operator= body must start with a series of 'member = value;' initialization statements for each of the type-scope objects in the same order they are declaredWell calling directly seems to work
Version: @value type = { Major: u32 = 0; Minor: u32 = 0; operator=: (out this, major: u32) = Major = major; operator=: (out this, major: u32, minor: u32) = { operator=(major); Minor = minor; } print: (this) = std::cout << "(Major)$.(Minor)$\n"; } main: () = { v1: Version = (11); v2: Version = (20, 23); v1.print(); //11.0 v2.print(); //20.23 }However technically thats not delegated constructor, but calling assign operator (which I think provided according to that diagram from wiki
Interesting. I tried with explicit
this, but it didn't occur to me to omit it.Btw is there syntax for initialization like cpp1
Ctor(a, b): a(a), b(b)?No. Just use assignments. See https://github.com/hsutter/cppfront/wiki/Cpp2:-operator=,-this-&-that#misc-notes.
Probably there is no need for delegating constructors at all and this can be solved in the future with just one constructor, default values, and named parameters in.
Like,v:= Version(.major = 5, .minor = 3)I think it's an accident that #409 (comment) works.
If you removeMajor's member initializer, it still breaks (https://cpp2.godbolt.org/z/8jzdnWEGq).
That's why #409 (comment) doesn't wok withoperator=(0);(https://cpp2.godbolt.org/z/zjzv5vvsv).Well I think it works but not exactly how it is expected.
Ideally it should automatically detect that initialization happens in all code paths.
So now it expecting straightforward either in declarationi : int = 0or inside constructori = 0
But in case of calling operator= it is not tracking that member was initialized inside,so initializing in delcaration suppress error, but constructor overwrites value later.
I think there is some overhead but it is allowed to work. It's like this codeS: type = { i: int = 2; operator=: (out this) = { i = 3; } print: (this) = std::cout << i; }Which boils down to
i=2; i=3;Edit: in other words, it's not forbidden to initialize same member different times with different values. May be thats issue, may be not.
Which boils down to
i=2; i=3;Looking closely, that (double assignment) seems to be the case
for theVersioncode that explicitly callsoperator=.Another exaggerated example for your #409 (comment)
S: type = { i: int; //compiles when change to = 0; init: (inout this) = i = 42; operator=: (out this) = init(); }cppfront doesn't track data flow that
iwill be init inside function
(Also I don't know c++ well, may be its bad to call member function before constructor is finished...)In wiki it's stated that cpp2 guarantees initialization, that means initializing at least once, but it's not clear if that means exactly once and not more.
(Also I don't know c++ well, may be its bad to call member function before constructor is finished...)
It's OK to.
I think assignment to
thisshould delegate.Also, something should be decided about
operator=.
In Cpp2,operator=is the name for all value-setting functions
-- https://github.com/hsutter/cppfront/wiki/Cpp2:-operator=,-this-&-thatBut currently, it lowers to Cpp1 as
operator=.Thanks for exploring the issue.
My understanding is that delegating constructors are not supported right now, but can be introduced in the future. Is that correct?
Should I edit this issue to match suggestion template?
Few thoughts that came to me while reading the thread:
Cpp2 encourage use of overloading while default values for arguments are not supported. Default arguments would fit nicely in the scenario with
Versiontype.Calling directly does work, but also reverse logic where most general constructors chain calls to more specialized. It feel more like workaround due to initialization code being more convoluted in the end.
Double assignment here is ok'is. Feel like compromise.
With complex types involved this can easily became no-fly zone.- Repository owner locked and limited conversation to collaborators
on Aug 30, 2023
I'm plaing with cppfront trying to port some bits of code to get a feel of new syntax.
One thing I cannot figure out is how to write
operator=:to delegate construction.I tried
operator=: (out this, major: u32) = this(major, 0, 0, 0);,operator=: (out this, major: u32) = this := (major, 0, 0, 0);, even stared withoperator=: (out this, major: u32) = Version(major, 0, 0, 0);.In cppfront source I didn't found code emitting, but I'm not yet 100% sure.
Did I missed the intended syntax or feature is not present at this time? Thanks!
For reference: