www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is this a betterC bug ?

reply learnfirst1 <learnfirst1 gmail.com> writes:
enum string[] a = ["a"];

extern(C) void main() {
         int i = 0;
         auto s = a[i];
}
---------------
Error: TypeInfo cannot be used with -betterC
Aug 14 2018
next sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Tuesday, 14 August 2018 at 13:01:57 UTC, learnfirst1 wrote:
 enum string[] a = ["a"];

 extern(C) void main() {
         int i = 0;
         auto s = a[i];
 }
 ---------------
 Error: TypeInfo cannot be used with -betterC
Yes. https://issues.dlang.org/show_bug.cgi?id=19169
Aug 14 2018
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Tuesday, 14 August 2018 at 13:16:50 UTC, Nicholas Wilson wrote:
 On Tuesday, 14 August 2018 at 13:01:57 UTC, learnfirst1 wrote:
 enum string[] a = ["a"];

 extern(C) void main() {
         int i = 0;
         auto s = a[i];
 }
 ---------------
 Error: TypeInfo cannot be used with -betterC
Yes. https://issues.dlang.org/show_bug.cgi?id=19169
This seems to be because dynamic array literals are allocated by default. So that is possibly not a bug, per se but the error message is definitely not helpful.
Aug 14 2018
prev sibling next sibling parent Seb <seb wilzba.ch> writes:
On Tuesday, 14 August 2018 at 13:01:57 UTC, learnfirst1 wrote:
 enum string[] a = ["a"];

 extern(C) void main() {
         int i = 0;
         auto s = a[i];
 }
 ---------------
 Error: TypeInfo cannot be used with -betterC
You can use `static immutable` as a workaround for now: https://run.dlang.io/is/7HEPst
Aug 14 2018
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Tuesday, 14 August 2018 at 13:01:57 UTC, learnfirst1 wrote:
 enum string[] a = ["a"];

 extern(C) void main() {
         int i = 0;
         auto s = a[i];
 }
 ---------------
 Error: TypeInfo cannot be used with -betterC
Workaround: https://run.dlang.io/is/NZykl0 Source: https://p0nce.github.io/d-idioms/
Aug 14 2018
parent reply Seb <seb wilzba.ch> writes:
On Tuesday, 14 August 2018 at 16:31:38 UTC, Paul Backus wrote:
 On Tuesday, 14 August 2018 at 13:01:57 UTC, learnfirst1 wrote:
 enum string[] a = ["a"];

 extern(C) void main() {
         int i = 0;
         auto s = a[i];
 }
 ---------------
 Error: TypeInfo cannot be used with -betterC
Workaround: https://run.dlang.io/is/NZykl0 Source: https://p0nce.github.io/d-idioms/
FYI: staticArray will be part of 2.082, it already works with dmd-nightly: https://run.dlang.io/is/tC7tix
Aug 14 2018
parent reply Mike Franklin <slavo5150 yahoo.com> writes:
On Tuesday, 14 August 2018 at 17:22:42 UTC, Seb wrote:

 FYI: staticArray will be part of 2.082, it already works with 
 dmd-nightly:
That just seems wrong. Isn't the fact that `staticArray` is needed a bug in the compiler? I think the compiler could have lowered to something like that automatically to avoid the library workaround. Mike
Aug 14 2018
parent reply Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Tuesday, 14 August 2018 at 17:49:32 UTC, Mike Franklin wrote:
 On Tuesday, 14 August 2018 at 17:22:42 UTC, Seb wrote:

 FYI: staticArray will be part of 2.082, it already works with 
 dmd-nightly:
That just seems wrong. Isn't the fact that `staticArray` is needed a bug in the compiler? I think the compiler could have lowered to something like that automatically to avoid the library workaround. Mike
It's not a bug, it's all about how the type system is set up. The type of an array literal expression like `[1, 2, 3]` is `int[]` (a slice of an array of ints), so no matter if you do: auto readonly(T)(const(T)[] x) { return x; } auto arr1 = [1, 2, 3]; auto arr2 = [1, 2, 3].readonly; const arr3 = [1, 2, 3]; enum arr4 = [1, 2, 3]; static immutable arr5 = [1, 2, 3]; scope arr6 = [1, 2, 3]; In all instances the type will be `int[]` modulo type qualifiers. Static arrays are completely different types, that just happen to accept assignments from slices. Their two defining properties are: 1. Their length is fixed at compile-time, meaning that you can do: import std.array, std.meta; auto x = [1, 2, 3, 4, 5].staticArray; enum length = x.length; pragma (msg, length); alias seq = AliasSeq!(0, 42, length); static foreach (i; 0 .. length) { } static foreach (i; seq) { } 2. Where slices are reference types, static arrays are value types which means that each assignment will copy an entire array. Basically they behave like a `struct { int _arr_0 = 0, _arr_1 = 1, _arr_2 = 2; }`. https://run.dlang.io/is/iD9ydu
Aug 15 2018
next sibling parent Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Wednesday, 15 August 2018 at 08:14:53 UTC, Petar Kirov 
[ZombineDev] wrote:
 https://run.dlang.io/is/iD9ydu
I made a typo in one of the comments. Here's the fixed version: https://run.dlang.io/is/HRqYcZ
Aug 15 2018
prev sibling parent Mike Franklin <slavo5150 yahoo.com> writes:
On Wednesday, 15 August 2018 at 08:14:53 UTC, Petar Kirov 
[ZombineDev] wrote:

 It's not a bug, it's all about how the type system is set up. 
 The type of an array literal expression like `[1, 2, 3]` is 
 `int[]` (a slice of an array of ints), so no matter if you do:

 auto readonly(T)(const(T)[] x) { return x; }

 auto             arr1 = [1, 2, 3];
 auto             arr2 = [1, 2, 3].readonly;
 const            arr3 = [1, 2, 3];
 enum             arr4 = [1, 2, 3];
 static immutable arr5 = [1, 2, 3];
 scope            arr6 = [1, 2, 3];

 In all instances the type will be `int[]` modulo type 
 qualifiers.

 Static arrays are completely different types, that just happen 
 to accept
 assignments from slices. Their two defining properties are:
 1. Their length is fixed at compile-time, meaning that you can 
 do:

 import std.array, std.meta;
 auto x = [1, 2, 3, 4, 5].staticArray;
 enum length = x.length;
 pragma (msg, length);
 alias seq = AliasSeq!(0, 42, length);
 static foreach (i; 0 .. length) { }
 static foreach (i; seq) { }


 2. Where slices are reference types, static arrays are value 
 types which means that each assignment will copy an entire 
 array.

 Basically they behave like a `struct { int _arr_0 = 0, _arr_1 = 
 1, _arr_2 = 2; }`.

 https://run.dlang.io/is/iD9ydu
Thanks for the detailed explanation; it make sense now. Mike
Aug 15 2018