Re: Implicit conversion of string to array of immutable ubytes

2025-05-23 Thread Andy Valencia via Digitalmars-d-learn
that if your code is straddling the worlds of ubyte[] and string, "representation" from std.string will give you the immutable ubyte[] (or ushort[]/uint[] as appropriate) for a string which you want to feed into a ubyte[] based API. Andy

Re: Immutable

2025-01-13 Thread Tahirandreas via Digitalmars-d-learn
ecially if you're used to languages that don't have it. But Meta says that using immutable types, like for strings, can really improve code performance and security. When strings are arrays of immutable characters, the compiler can optimize its work with them and not waste resources on

Re: Immutable (Rosetta code and learning D)

2025-01-04 Thread monkyyy via Digitalmars-d-learn
On Saturday, 4 January 2025 at 22:42:47 UTC, Andy Valencia wrote: On Saturday, 27 March 2021 at 20:44:12 UTC, Brad wrote: I was looking through lots of sample code on Rosetta Code. D has a lot of solutions out there. I'm following up to this older post, and I'm sure this is old news to many.

Re: Immutable (Rosetta code and learning D)

2025-01-04 Thread Andy Valencia via Digitalmars-d-learn
On Saturday, 27 March 2021 at 20:44:12 UTC, Brad wrote: I was looking through lots of sample code on Rosetta Code. D has a lot of solutions out there. I'm following up to this older post, and I'm sure this is old news to many... I recently realized that Rosetta Code is a very nice resource

Re: Is there a way to make a struct without a distinction between constant, immutable, and mutable?

2024-11-20 Thread Steven Schveighoffer via Digitalmars-d-learn
On Monday, 18 November 2024 at 23:43:46 UTC, Liam McGillivray wrote: Is there a way to make it so that this struct has only one qualifier? No distinction? Can you give an example of a problem and list the error messages? -Steve

Re: Is there a way to make a struct without a distinction between constant, immutable, and mutable?

2024-11-19 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 18 November 2024 at 23:43:46 UTC, Liam McGillivray wrote: I am doing work on the `units-d` library. This library contains some struct templates which don't contain any variables. Because of this, all instances are effectively constant. However, when they are placed as arguments in ot

Is there a way to make a struct without a distinction between constant, immutable, and mutable?

2024-11-18 Thread Liam McGillivray via Digitalmars-d-learn
I am doing work on the `units-d` library. This library contains some struct templates which don't contain any variables. Because of this, all instances are effectively constant. However, when they are placed as arguments in other templates, it causes problems when one is unexpectedly `const`.

Re: Mutate immutable inside shared static constructor

2024-03-24 Thread Menjanahary R. R. via Digitalmars-d-learn
On Saturday, 23 March 2024 at 21:59:57 UTC, Nick Treleaven wrote: On Saturday, 23 March 2024 at 21:53:43 UTC, Jonathan M Davis wrote: Yes, it's a bug. It's a clear violation of the type system if a non-mutable variable is ever given a value more than once. It should be initialized, and then it

Re: Mutate immutable inside shared static constructor

2024-03-23 Thread Nick Treleaven via Digitalmars-d-learn
On Saturday, 23 March 2024 at 21:53:43 UTC, Jonathan M Davis wrote: Yes, it's a bug. It's a clear violation of the type system if a non-mutable variable is ever given a value more than once. It should be initialized, and then it should be treated as illegal to ever assign to it - or to do anyth

Re: Mutate immutable inside shared static constructor

2024-03-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, March 23, 2024 3:23:23 PM MDT Nick Treleaven via Digitalmars-d- learn wrote: > I've not used static constructors before, but it seems like the > following should not be allowed: > > ```d > import std.stdio; > > immutable int x; > > @safe shared st

Mutate immutable inside shared static constructor

2024-03-23 Thread Nick Treleaven via Digitalmars-d-learn
I've not used static constructors before, but it seems like the following should not be allowed: ```d import std.stdio; immutable int x; @safe shared static this() { x.writeln(); // 0 x = 5; x.writeln(); // 5 x = 6; x++; assert(x == 7); } ``` Should I file a b

Re: Implicit conversion of string to array of immutable ubytes

2024-03-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, March 23, 2024 12:11:15 AM MDT Per Nordlöw via Digitalmars-d- learn wrote: > Why doesn't string implicitly convert to immutable(ubyte)[] in > @safe mode? Why would it? They're different types. Their elements happen to have the same size, but that doesn't mean

Re: std.string.assumeUTF() silently casting mutable to immutable?

2024-02-14 Thread RazvanN via Digitalmars-d-learn
can modify immutable table. This code currently happily compiles: ```d string test(const(ubyte)[] arr) { import std.string; return arr.assumeUTF; } void main() { import std.stdio; ubyte[] arr = ['a', 'b', 'c']; auto t = test(arr); wri

Re: std.string.assumeUTF() silently casting mutable to immutable?

2024-02-14 Thread Forest via Digitalmars-d-learn
On Wednesday, 14 February 2024 at 10:57:42 UTC, RazvanN wrote: This has already been fixed, you just need to use -preview=fixImmutableConv. This was put behind a preview flag as it introduces a breaking change. I just tried that flag on run.dlang.org, and although it fixes the case I posted

Re: std.string.assumeUTF() silently casting mutable to immutable?

2024-02-13 Thread Forest via Digitalmars-d-learn
On Tuesday, 13 February 2024 at 14:05:03 UTC, Johan wrote: On Tuesday, 13 February 2024 at 08:10:20 UTC, Jonathan M Davis wrote: So, there's definitely a bug here, but it's a dmd bug. Its checks for whether it can safely change the constness of the return type apparently aren't sophisticated

Re: std.string.assumeUTF() silently casting mutable to immutable?

2024-02-13 Thread Johan via Digitalmars-d-learn
etty severe bug. Some test cases: https://d.godbolt.org/z/K1fjdj76M ```d ubyte[] pure_ubyte(ubyte[] arr) pure @safe; ubyte[] pure_void(void[] arr) pure @safe; ubyte[] pure_int(int[] arr) pure @safe; int[] pure_ubyte_to_int(ubyte[] arr) pure @safe; // All cases below should not compile, yet some do.

Re: std.string.assumeUTF() silently casting mutable to immutable?

2024-02-13 Thread Jonathan M Davis via Digitalmars-d-learn
turn it typed as a UTF string. > > ubyte becomes char, ushort becomes wchar and uint becomes > > dchar. Type qualifiers are preserved. > > The declaration: > > ```d > auto assumeUTF(T)(T[] arr) > if (staticIndexOf!(immutable T, immutable ubyte, immutable > ushort,

std.string.assumeUTF() silently casting mutable to immutable?

2024-02-12 Thread Forest via Digitalmars-d-learn
erved. The declaration: ```d auto assumeUTF(T)(T[] arr) if (staticIndexOf!(immutable T, immutable ubyte, immutable ushort, immutable uint) != -1) ``` Shouldn't that precondition's `immutable T` be simply `T`? As it stands, I can do this with no complaints from the compiler... ```d st

Re: On assigning const to immutable

2023-07-13 Thread FeepingCreature via Digitalmars-d-learn
On Thursday, 13 July 2023 at 11:55:17 UTC, Ki Rill wrote: Why does the first example `class A` work, but the second one with `class B` does not? ```D class A { immutable int a; this(in int a) { this.a = a; } } class B { immutable int[] b; this(in int[] b

Re: On assigning const to immutable

2023-07-13 Thread Ki Rill via Digitalmars-d-learn
On Thursday, 13 July 2023 at 11:55:17 UTC, Ki Rill wrote: Why does the first example `class A` work, but the second one with `class B` does not? ```D class A { immutable int a; this(in int a) { this.a = a; } } class B { immutable int[] b; this(in int[] b

On assigning const to immutable

2023-07-13 Thread Ki Rill via Digitalmars-d-learn
Why does the first example `class A` work, but the second one with `class B` does not? ```D class A { immutable int a; this(in int a) { this.a = a; } } class B { immutable int[] b; this(in int[] b) { this.b = b; } } void main() { auto a = new A(1

Para que sirve o que son las variables "immutable"?

2023-06-15 Thread Danico via Digitalmars-d-learn
Tengo una duda y es que en la documentacion de std.json hay un ejemplo de codigo que por identidicador ponen "immutable", nose para que sirve o de que trata. :( alguien me diga porfis :)

Re: Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-31 Thread Cecil Ward via Digitalmars-d-learn
On Wednesday, 31 May 2023 at 09:14:49 UTC, Dom DiSc wrote: On Wednesday, 31 May 2023 at 03:29:33 UTC, Cecil Ward wrote: I have to admit that I don’t really understand immutable. I have an idea that it could mean that an object has an address in ROM, so its value will never change. Maybe const

Re: static immutable that has no initialiser - should this raise an error?

2023-05-31 Thread Dom DiSc via Digitalmars-d-learn
On Tuesday, 30 May 2023 at 04:11:00 UTC, Cecil Ward wrote: static immutable T foo; T bar() { return foo; } Should we get an error from the D compiler here as the initialiser has been forgotten? What do you think ? No. There are no un-initialized values in D. It gets its default value

Re: Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-31 Thread Dom DiSc via Digitalmars-d-learn
On Wednesday, 31 May 2023 at 03:29:33 UTC, Cecil Ward wrote: I have to admit that I don’t really understand immutable. I have an idea that it could mean that an object has an address in ROM, so its value will never change. Maybe const doesn’t give you such a strong guarantee, disallows ‘you

Re: Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-30 Thread Cecil Ward via Digitalmars-d-learn
On Wednesday, 31 May 2023 at 03:23:01 UTC, Cecil Ward wrote: On Tuesday, 30 May 2023 at 04:15:22 UTC, Ali Çehreli wrote: On 5/29/23 19:57, Cecil Ward wrote: > I wish to have one routine > that can be called with either immutable or (possibly) mutable argument > values. 'cons

Re: Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-30 Thread Cecil Ward via Digitalmars-d-learn
On Tuesday, 30 May 2023 at 04:15:22 UTC, Ali Çehreli wrote: On 5/29/23 19:57, Cecil Ward wrote: > I wish to have one routine > that can be called with either immutable or (possibly) mutable argument > values. 'const' should take both immutable and mutable. Can you show you

Re: Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-30 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/29/23 10:57 PM, Cecil Ward wrote: I have often come into difficulties where I wish to have one routine that can be called with either immutable or (possibly) mutable argument values. The argument(s) in question are in, readonly, passed by value or passed by const reference. Anyway, no one

Re: Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-29 Thread Ali Çehreli via Digitalmars-d-learn
On 5/29/23 19:57, Cecil Ward wrote: > I wish to have one routine > that can be called with either immutable or (possibly) mutable argument > values. 'const' should take both immutable and mutable. Can you show your case with a short example? > Could I make the one r

Re: Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-29 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 30 May 2023 at 02:57:52 UTC, Cecil Ward wrote: I have often come into difficulties where I wish to have one routine that can be called with either immutable or (possibly) mutable argument values. The argument(s) in question are in, readonly, passed by value or passed by const

Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-29 Thread Cecil Ward via Digitalmars-d-learn
I have often come into difficulties where I wish to have one routine that can be called with either immutable or (possibly) mutable argument values. The argument(s) in question are in, readonly, passed by value or passed by const reference. Anyway, no one is trying to write to the items passed

Re: Transform static immutable string array to tuple.

2023-02-19 Thread realhet via Digitalmars-d-learn
Awesome, Thank both of you! ``` enum a = ["A", "B"]; writeln(a); writeln(aliasSeqOf!a); writeln([aliasSeqOf!a]); ```

Re: Transform static immutable string array to tuple.

2023-02-19 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Feb 19, 2023 at 11:08:34AM +, realhet via Digitalmars-d-learn wrote: > Hello, > > Is there a better way to transform a string array to a tuple or to an > AliasSeq? > > ``` > mixin(customSyntaxPrefixes.format!`tuple(%(%s,%))`) > ``` > > I'd like to use this as variable length argument

Re: Transform static immutable string array to tuple.

2023-02-19 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 19 February 2023 at 11:08:34 UTC, realhet wrote: Is there a better way to transform a string array to a tuple or to an AliasSeq? ``` mixin(customSyntaxPrefixes.format!`tuple(%(%s,%))`) ``` https://dlang.org/phobos/std_meta.html#aliasSeqOf

Transform static immutable string array to tuple.

2023-02-19 Thread realhet via Digitalmars-d-learn
Hello, Is there a better way to transform a string array to a tuple or to an AliasSeq? ``` mixin(customSyntaxPrefixes.format!`tuple(%(%s,%))`) ``` I'd like to use this as variable length arguments passed to the startsWith() std function (as multiple needles).

Re: Error: cannot use non-constant CTFE pointer in an initializer `cast(immutable(char)*)TEST`

2022-06-11 Thread Tejas via Digitalmars-d-learn
onlineapp.d(3): Error: cannot use non-constant CTFE pointer in an initializer `cast(immutable(char)*)TEST` ``` Maybe try putting `__gshared bin_ptr = TEST.ptr;` inside a `shared static this(){}`? I mean, write it like: ```d __gshared bin_ptr; shared static this() { bin_ptr = TEST.ptr

Re: Error: cannot use non-constant CTFE pointer in an initializer `cast(immutable(char)*)TEST`

2022-06-11 Thread Tejas via Digitalmars-d-learn
pointer in an initializer `cast(immutable(char)*)TEST` ``` Maybe try putting `__gshared bin_ptr = TEST.ptr;` inside a `shared static this(){}`? I mean, write it like: ```d __gshared bin_ptr; shared static this() { bin_ptr = TEST.ptr; } __gshared const TEST = import(`onlineapp.d

Error: cannot use non-constant CTFE pointer in an initializer `cast(immutable(char)*)TEST`

2022-06-11 Thread test123 via Digitalmars-d-learn
how to work this around. ```d __gshared const TEST = import(`onlineapp.d`); extern(C) void main(){ __gshared bin_ptr = TEST.ptr; } ``` ```sh dmd2 -betterC -J. onlineapp.d onlineapp.d(3): Error: cannot use non-constant CTFE pointer in an initializer `cast(immutable(char)*)TEST` ```

Re: Why do immutable variables need reference counting?

2022-04-18 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Apr 18, 2022 at 12:55:26PM +, wjoe via Digitalmars-d-learn wrote: > On Sunday, 17 April 2022 at 14:14:37 UTC, H. S. Teoh wrote: > > Not entirely true. See paragraph 3 in: > > > > https://dlang.org/spec/unittest.html > > > > and 10.24.11.3 in: > > > > https://dlang.org/spec/ex

Re: Why do immutable variables need reference counting?

2022-04-18 Thread wjoe via Digitalmars-d-learn
On Sunday, 17 April 2022 at 14:14:37 UTC, H. S. Teoh wrote: Not entirely true. See paragraph 3 in: https://dlang.org/spec/unittest.html and 10.24.11.3 in: https://dlang.org/spec/expression.html#assert_expressions T Thanks. Either I missed that the last time I checked or it

Re: Why do immutable variables need reference counting?

2022-04-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.04.22 15:27, H. S. Teoh wrote: On Sun, Apr 17, 2022 at 01:06:36PM +, wjoe via Digitalmars-d-learn wrote: [...] On the matter of undefined behavior. Technically a program is in undefined behavior land after throwing an error, thus every unittest that continues after assertThrown is ther

Re: Why do immutable variables need reference counting?

2022-04-17 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Apr 17, 2022 at 04:09:12PM +0200, ag0aep6g via Digitalmars-d-learn wrote: [...] > Failing asserts are a messy part of the language. They are supposed to > be: > > 1) not catchable, because they indicate a bug in the program; > 2) catchable in order to be testable; > 3) assumed impossible

Re: Why do immutable variables need reference counting?

2022-04-17 Thread ag0aep6g via Digitalmars-d-learn
ating `immutable` data is not messy. It's simply not allowed.

Re: Why do immutable variables need reference counting?

2022-04-17 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Apr 17, 2022 at 01:06:36PM +, wjoe via Digitalmars-d-learn wrote: [...] > On the matter of undefined behavior. Technically a program is in > undefined behavior land after throwing an error, thus every unittest > that continues after assertThrown is therefore nonsense code, is it > not ?

Re: Why do immutable variables need reference counting?

2022-04-17 Thread wjoe via Digitalmars-d-learn
On Thursday, 14 April 2022 at 12:10:04 UTC, ag0aep6g wrote: On 14.04.22 13:42, wjoe wrote: Undefined behavior yes, but regardless the example proves it can be done in @system code. A few versions ago, possibly due to a bug or regression, the compiler didn't complain in @safe code either. Of c

Re: Why do immutable variables need reference counting?

2022-04-14 Thread ag0aep6g via Digitalmars-d-learn
On 14.04.22 13:42, wjoe wrote: Undefined behavior yes, but regardless the example proves it can be done in @system code. A few versions ago, possibly due to a bug or regression, the compiler didn't complain in @safe code either. Of course you are correct academically. However, since it's possi

Re: Why do immutable variables need reference counting?

2022-04-14 Thread wjoe via Digitalmars-d-learn
d misunderstandings regarding const, immutable and const 2.

Re: Why do immutable variables need reference counting?

2022-04-14 Thread wjoe via Digitalmars-d-learn
On Tuesday, 12 April 2022 at 22:23:18 UTC, ag0aep6g wrote: On Tuesday, 12 April 2022 at 19:54:13 UTC, wjoe wrote: Especially since it's only a promise and the compiler accepts this: void foo (const(char)[] arr) { cast(char[])arr[0..3] = "baz"; } string bar = "123"; foo(bar); assert(bar=="baz

Re: Why do immutable variables need reference counting?

2022-04-13 Thread H. S. Teoh via Digitalmars-d-learn
) there is a GC, and (2) characters in a string are immutable. Without (1), you will end up with either a memory leak or a dangling pointer; without (2), your slice may randomly mutate when you don't expect it to. T -- May you live all the days of your life. -- Jonathan Swift

Re: Why do immutable variables need reference counting?

2022-04-13 Thread Ali Çehreli via Digitalmars-d-learn
fileName = fileName; > report(); >} > >~this() { report(); } > >void report(string func = __FUNCTION__) { > import std.stdio; > writefln!"%s\nworking with %s"(func, fileName); >} > } > alias T1 = const(char)[]; > alias T

Re: Why do immutable variables need reference counting?

2022-04-12 Thread Salih Dincer via Digitalmars-d-learn
with %s"(func, fileName); } } alias T1 = const(char)[]; alias T2 = const char[]; alias T3 = const(char[]); // T3 == T2 alias T4 = immutable char[]; // Not compiling! void main() { auto fileName = "foo.txt".dup; auto s = S!T1(fileName); fileName[0..3] = "bar"; }/* Results:

Re: Why do immutable variables need reference counting?

2022-04-12 Thread Ali Çehreli via Digitalmars-d-learn
On 4/12/22 12:54, wjoe wrote: > I.e. immutable is constant data which is created at compile time - like > laws of physics, For completeness, we already have 'enum' and 'static const' for that. > should get a better name - maybe 'in' and get rid

Re: Why do immutable variables need reference counting?

2022-04-12 Thread ag0aep6g via Digitalmars-d-learn
On Tuesday, 12 April 2022 at 19:54:13 UTC, wjoe wrote: Especially since it's only a promise and the compiler accepts this: void foo (const(char)[] arr) { cast(char[])arr[0..3] = "baz"; } string bar = "123"; foo(bar); assert(bar=="baz"); But I could cast away const and modify the string bar.

Re: Why do immutable variables need reference counting?

2022-04-12 Thread wjoe via Digitalmars-d-learn
f ROM.) I was thinking during compile time. By initializing a variable with immutable data or a pointer that points to an address e.G. an EEPROM. Even 'const' cause confusions because it's used in at least two different ways (even e.g. in C++): 1) I will not mutate data throug

Re: Why do immutable variables need reference counting?

2022-04-12 Thread Ali Çehreli via Digitalmars-d-learn
> >> 2) This variable is const: >> >> const i = 42; >> >> Well, there is the confusion: There is no "reference" in the second >> case at all! > I think this second case should not be allowed. Use > > immutable i = 42; > > instead. The mea

Re: Why do immutable variables need reference counting?

2022-04-12 Thread Dom DiSc via Digitalmars-d-learn
"reference" in the second case at all! I think this second case should not be allowed. Use immutable i = 42; instead. The meaning is identical, but we could remove the burden of two different meanings from const if it is not allowed. const should only be allowed in function decla

Re: Why do immutable variables need reference counting?

2022-04-11 Thread Ali Çehreli via Digitalmars-d-learn
On 4/11/22 05:57, wjoe wrote: > To my understanding immutable data should reside in a separate data > segment which itself could reside in ROM. We are getting into implementation details which a programming language acts as not to care (but has to do especially when it's a system

Re: Why do immutable variables need reference counting?

2022-04-11 Thread Ali Çehreli via Digitalmars-d-learn
On 4/11/22 08:02, Paul Backus wrote: > any pointers or references To add, Salih and I were in an earlier discussion where that concept appeared as "indirections." Ali

Re: Why do immutable variables need reference counting?

2022-04-11 Thread Paul Backus via Digitalmars-d-learn
like this. ```d struct S { . . . string toString() { return ""; } } //S test(inout S s)/* S test(S s)//*/ { s.i = 2; return s; } void main() { immutable s = S(1); test(s).writeln; "Hello".writefln!"%s D!"; } ``` If the inout is set, it does not allow c

Re: Why do immutable variables need reference counting?

2022-04-11 Thread wjoe via Digitalmars-d-learn
On Monday, 11 April 2022 at 03:24:11 UTC, Ali Çehreli wrote: On 4/10/22 20:05, norm wrote: > On Sunday, 10 April 2022 at 23:19:47 UTC, rikki cattermole wrote: > In my mind immutable data > means the data will not change and neither will the result of reading > that data, ever. Yes

Re: Why do immutable variables need reference counting?

2022-04-11 Thread Salih Dincer via Digitalmars-d-learn
s; } void main() { immutable s = S(1); test(s).writeln; "Hello".writefln!"%s D!"; } ``` If the inout is set, it does not allow compilation. Thanks, SDB79

Re: Why do immutable variables need reference counting?

2022-04-11 Thread user1234 via Digitalmars-d-learn
On Sunday, 10 April 2022 at 23:05:24 UTC, norm wrote: Hi All, I am clearly misunderstanding something fundamental, and probably obvious :D Reading some of the discussions on __metadata I was wondering if someone could explain why a immutable reference counting type is needed. By definition

Re: Why do immutable variables need reference counting?

2022-04-11 Thread IGotD- via Digitalmars-d-learn
On Sunday, 10 April 2022 at 23:19:47 UTC, rikki cattermole wrote: immutable isn't tied to lifetime semantics. It only says that this memory will never be modified by anyone during its lifetime. Anyway, the real problem is with const. Both mutable and immutable become it automatically

Re: Why do immutable variables need reference counting?

2022-04-10 Thread rikki cattermole via Digitalmars-d-learn
Storage classes like immutable/const/shared are not tied to any memory management strategy. Nor does it dictate memory lifetime. It only dictates how it can be interacted with when you have a reference to it.

Re: Why do immutable variables need reference counting?

2022-04-10 Thread Ali Çehreli via Digitalmars-d-learn
On 4/10/22 20:05, norm wrote: > On Sunday, 10 April 2022 at 23:19:47 UTC, rikki cattermole wrote: > In my mind immutable data > means the data will not change and neither will the result of reading > that data, ever. Yes. > I don't get how you can have thread safety

Re: Why do immutable variables need reference counting?

2022-04-10 Thread norm via Digitalmars-d-learn
On Sunday, 10 April 2022 at 23:19:47 UTC, rikki cattermole wrote: immutable isn't tied to lifetime semantics. It only says that this memory will never be modified by anyone during its lifetime. This is clearly where I am misunderstanding. In my mind immutable data means the data wil

Re: Why do immutable variables need reference counting?

2022-04-10 Thread rikki cattermole via Digitalmars-d-learn
immutable isn't tied to lifetime semantics. It only says that this memory will never be modified by anyone during its lifetime. Anyway, the real problem is with const. Both mutable and immutable become it automatically.

Why do immutable variables need reference counting?

2022-04-10 Thread norm via Digitalmars-d-learn
Hi All, I am clearly misunderstanding something fundamental, and probably obvious :D Reading some of the discussions on __metadata I was wondering if someone could explain why a immutable reference counting type is needed. By definition a reference counter cannot be immutable, so what

Re: How do you properly use immutable on class members?

2022-03-29 Thread Fruitful Approach via Digitalmars-d-learn
On Tuesday, 29 March 2022 at 18:59:41 UTC, H. S. Teoh wrote: On Tue, Mar 29, 2022 at 05:58:11PM +, Fruitful Approach via Digitalmars-d-learn wrote: [...] 1) `immutable immutable(Prop)[]` is identical to `immutable(Prop[])`. Immutable is transitive. There is no need to spell it in

Re: How do you properly use immutable on class members?

2022-03-29 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 29 March 2022 at 19:26:51 UTC, Ali Çehreli wrote: Better yet, and as I know you know :), and as it comes up occasionally but I usually forget in my own code; 'in' is much better than 'const' on function parameters because it has super powers when compiled with -preview=in: Hel

Re: How do you properly use immutable on class members?

2022-03-29 Thread Ali Çehreli via Digitalmars-d-learn
s object const." Nope! 'const' is replaced with 'auto' instantly. :) 'const' and 'immutable' on member variables are trouble because they make objects unassignable. (But you said 'class', so there is no such issue with them in D anyway.) But is that too bad? Is assignment overrated anyway? I don't know... :/ Ali

Re: How do you properly use immutable on class members?

2022-03-29 Thread Ali Çehreli via Digitalmars-d-learn
On 3/29/22 11:59, H. S. Teoh wrote: > As a general principle, const should be used when you're on the > receiving end of data that should not be changed (e.g., function > parameters) Better yet, and as I know you know :), and as it comes up occasionally but I usually forget in my own code; 'in'

Re: How do you properly use immutable on class members?

2022-03-29 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Mar 29, 2022 at 05:58:11PM +, Fruitful Approach via Digitalmars-d-learn wrote: > I have immutable members: > > `immutable immutable(Prop)[] axioms` which I can't initialize with > Prop[], so I call this "immutable-poisoning" of the rest of my code. > Can

How do you properly use immutable on class members?

2022-03-29 Thread Fruitful Approach via Digitalmars-d-learn
I have immutable members: `immutable immutable(Prop)[] axioms` which I can't initialize with Prop[], so I call this "immutable-poisoning" of the rest of my code. Can you provide a 10 line example of how to best work with immutable types, specifically immutable members, toge

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-18 Thread Denis Feklushkin via Digitalmars-d-learn
On Saturday, 18 December 2021 at 12:50:17 UTC, Tejas wrote: As Ali said, this is an implementation issue. So I guess the answer to your question is that this is a bug. Please file a report at [issues.dlang.org](issues.dlang.org) Looks like this is same case: https://issues.dlang.org/show_bug

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-18 Thread Tejas via Digitalmars-d-learn
On Saturday, 18 December 2021 at 11:01:53 UTC, Denis Feklushkin wrote: On Friday, 17 December 2021 at 19:03:05 UTC, Tejas wrote: Well, I got completely mislead by my experiment 😓 ```d struct S { ~this() immutable {} } ``` Interesting what discussed behaviour isn't affects method

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-18 Thread Denis Feklushkin via Digitalmars-d-learn
On Friday, 17 December 2021 at 19:03:05 UTC, Tejas wrote: Well, I got completely mislead by my experiment 😓 ```d struct S { ~this() immutable {} } ``` Interesting what discussed behaviour isn't affects method what implements same functionality as dtor and called explictly at

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Tejas via Digitalmars-d-learn
On Friday, 17 December 2021 at 18:51:56 UTC, Ali Çehreli wrote: On 12/17/21 10:01 AM, Tejas wrote: > [...] Storage, There is no such requirement nor guarantee. [...] Well, I got completely mislead by my experiment 😓 ```d struct S { ~this() immutable {} } void main() { immutabl

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Ali Çehreli via Digitalmars-d-learn
On 12/17/21 10:01 AM, Tejas wrote: > I think since `immutable` objects are kept in Read Only Storage, There is no such requirement nor guarantee. > you > can't call destructors on them Destructor is nothing but a piece of code that is executed when an object's life ends

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Tejas via Digitalmars-d-learn
On Friday, 17 December 2021 at 18:32:43 UTC, Denis Feklushkin wrote: On Friday, 17 December 2021 at 18:02:52 UTC, Tejas wrote: I improved your sample: ```d immutable struct S { ~this() {} } immutable struct S2 { S sss; ~this() {} } void main() { S2 s = S2(); } ``` ``` Error

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Denis Feklushkin via Digitalmars-d-learn
On Friday, 17 December 2021 at 18:02:52 UTC, Tejas wrote: I improved your sample: ```d immutable struct S { ~this() {} } immutable struct S2 { S sss; ~this() {} } void main() { S2 s = S2(); } ``` ``` Error: `immutable` method `serializer_bug.S.~this` is not callable using a

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Tejas via Digitalmars-d-learn
On Friday, 17 December 2021 at 18:19:34 UTC, Denis Feklushkin wrote: On Friday, 17 December 2021 at 18:01:03 UTC, Tejas wrote: I think since `immutable` objects are kept in Read Only Storage Some of them can be stored in ROM in some cases, but actually "immutable" keyword means &q

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Denis Feklushkin via Digitalmars-d-learn
On Friday, 17 December 2021 at 18:01:03 UTC, Tejas wrote: I think since `immutable` objects are kept in Read Only Storage Some of them can be stored in ROM in some cases, but actually "immutable" keyword means "not mutable for whole its lifetime"

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Tejas via Digitalmars-d-learn
On Friday, 17 December 2021 at 18:01:03 UTC, Tejas wrote: On Friday, 17 December 2021 at 17:34:05 UTC, Denis Feklushkin wrote: On Friday, 17 December 2021 at 17:27:53 UTC, Denis Feklushkin wrote: [...] ("serializer_bug" is just name of my local .d file) I think since

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Tejas via Digitalmars-d-learn
On Friday, 17 December 2021 at 17:34:05 UTC, Denis Feklushkin wrote: On Friday, 17 December 2021 at 17:27:53 UTC, Denis Feklushkin wrote: ~this() {} // Comment out this to fix this compilation error: // Error: `immutable` method `serializer_bug.Imm.~this` is ("serializer_bu

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Denis Feklushkin via Digitalmars-d-learn
On Friday, 17 December 2021 at 17:27:53 UTC, Denis Feklushkin wrote: ~this() {} // Comment out this to fix this compilation error: // Error: `immutable` method `serializer_bug.Imm.~this` is ("serializer_bug" is just name of my local .d file)

This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Denis Feklushkin via Digitalmars-d-learn
```d /+ dub.json: { "name": "test", "dependencies": { } } +/ struct S { ~this() {} } immutable class Imm { S s; // this is immutable value because whole class is immutable this() { s = S(); } ~this() {} // Comment out this

Re: Efficient way to create/copy immutable struct instance with modified data

2021-11-19 Thread Adam D Ruppe via Digitalmars-d-learn
On Friday, 19 November 2021 at 17:38:53 UTC, Merlin Diavova wrote: I'm trying to figure out the most efficient way to create modified instances of immutable structs. This might not be the best way but it is a kinda cool trick anyway: structs have a `tupleof` property you can slice. S

Efficient way to create/copy immutable struct instance with modified data

2021-11-19 Thread Merlin Diavova via Digitalmars-d-learn
Hi all, I'm trying to figure out the most efficient way to create modified instances of immutable structs. Currently, I'm doing the following: ```d immutable struct Node { string label; Node parentNode; NetworkPort port; auto withLabel(strin

Re: Run-time setting of immutable variable?

2021-09-03 Thread DLearner via Digitalmars-d-learn
On Thursday, 2 September 2021 at 23:12:28 UTC, Steven Schveighoffer wrote: [...] immutable means "I can never change and *everything I point at* can never change". [...] If that is how the language defines the keyword 'immutable' when used in the definition of a point

Re: Run-time setting of immutable variable?

2021-09-02 Thread Ali Çehreli via Digitalmars-d-learn
On 9/2/21 9:01 AM, DLearner wrote: Suppose there is a variable that is set once per run, and is (supposed) never to be altered again. An accessor function can be a solution, which supports your other comment about data potentially mutating by other means: // Assume these are in a module // v

Re: Run-time setting of immutable variable?

2021-09-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 9/2/21 1:17 PM, DLearner wrote: I am looking for a mutable Arr but would like an immutable ArrPtr. Then you want const not immutable. Here is the reason: ```d void main() { int x = 5; immutable int *ptr = cast(immutable int *)&x; assert(*ptr == 5); // ok x = 6; as

Re: Run-time setting of immutable variable?

2021-09-02 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Sep 02, 2021 at 05:17:15PM +, DLearner via Digitalmars-d-learn wrote: [...] > The following clean-compiled and produced the expected result: > ``` > ubyte[10] Arr; > > immutable void* ArrPtr; > shared static this() { > ArrPtr = cast(im

Re: Run-time setting of immutable variable?

2021-09-02 Thread jfondren via Digitalmars-d-learn
On Thursday, 2 September 2021 at 17:17:15 UTC, DLearner wrote: Surely there is no inconsistency - at run time the array is in a fixed place, so ArrPtr is (or at least should be) a constant, but the contents of the array can vary as the program runs. In the case of `immutable(T)* ArrPtr

Re: Run-time setting of immutable variable?

2021-09-02 Thread Kagamin via Digitalmars-d-learn
If you want only address, you can keep it as size_t: ubyte[10] Arr; immutable size_t Address; static this() { Address = cast(size_t)(&Arr[0]); }

Re: Run-time setting of immutable variable?

2021-09-02 Thread DLearner via Digitalmars-d-learn
, variable is 'ArrPtr'; ``` ubyte[10] Arr; // immutable void* ArrPtr; void* ArrPtr; void main() {    ArrPtr = cast(void*)Arr[0]; // modify ArrPtr> } ``` Is there a way of getting D to guarantee that ArrPtr is never modified after ```    ArrPtr = cast(void*)Arr[0]; ```] You

Re: Run-time setting of immutable variable?

2021-09-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 9/2/21 12:01 PM, DLearner wrote: Suppose there is a variable that is set once per run, and is (supposed) never to be altered again.  However, the value to which it is set is not known at compile time. Example below, variable is 'ArrPtr'; ``` ubyte[10] Arr; // immutable void* Ar

Re: Run-time setting of immutable variable?

2021-09-02 Thread H. S. Teoh via Digitalmars-d-learn
e case of `immutable`. Using the example you gave, you'd move the initialization of ArrPtr into a static module constructor: immutable void* ArrPtr; shared static this() { ArrPtr = ...; // initialize it here } void main() { ... // ArrPt

Run-time setting of immutable variable?

2021-09-02 Thread DLearner via Digitalmars-d-learn
Suppose there is a variable that is set once per run, and is (supposed) never to be altered again. However, the value to which it is set is not known at compile time. Example below, variable is 'ArrPtr'; ``` ubyte[10] Arr; // immutable void* ArrPtr; void* ArrPtr; void main() {

Re: Creating immutable arrays in @safe code

2021-07-18 Thread zjh via Digitalmars-d-learn
On Sunday, 18 July 2021 at 10:02:07 UTC, ag0aep6g wrote: On 17.07.21 15:56, ag0aep6g wrote: +1. IMO,`strong pure` is `the outside world` has no impact on me. `Funcs` don't have any `indirect`.

  1   2   3   4   5   6   7   8   9   10   >