Re: impure

2024-04-08 Thread Dom DiSc via Digitalmars-d-learn
On Monday, 8 April 2024 at 07:03:40 UTC, Alexandru Ermicioi wrote: On Sunday, 24 March 2024 at 07:41:41 UTC, Dom DiSc wrote: I'm creating a library that is completely pure, but it doesn't compile with pure: at the top because of one impure unittest (which uses random to test some things only

Re: impure

2024-04-08 Thread Dom DiSc via Digitalmars-d-learn
On Sunday, 7 April 2024 at 23:32:24 UTC, MrJay wrote: A better way to apply a attribute to an entire file is to use an explicit scope you can still apply this to basically the entire file but leave the tests out of it. Better than an explicit impure (or pure=false) attribute? I don't think

Re: impure

2024-04-05 Thread Dom DiSc via Digitalmars-d-learn
On Sunday, 24 March 2024 at 09:16:20 UTC, Jonathan M Davis wrote: So, yes, you've run into a problem that it would be nice to have a better fix for, but even if we could negate attributes in general, there are good reasons to prefer to avoid mass-applying attributes. I don't see it as

Re: real.sizeof

2024-02-05 Thread Dom DiSc via Digitalmars-d-learn
On Monday, 5 February 2024 at 17:28:38 UTC, Iain Buclaw wrote: Padding. x86 ABI prefers things to be aligned, so on x86 it's 12 bytes, x86_64 16 bytes. In both cases you don't get any extra precision over the 80-bits that x87 gives you. This is exactly what I mean. The ABI may pad it, but

Re: struct initializer

2023-12-01 Thread Dom DiSc via Digitalmars-d-learn
On Thursday, 30 November 2023 at 12:15:04 UTC, Dennis wrote: The syntax was inherited from C. The 'special place' is called initialization, and it's special because the target type of the initializer is known in advance This is no different from `S fun(){ return { 5, 2 }; }` It creates a new

Re: struct initializer

2023-12-01 Thread Dom DiSc via Digitalmars-d-learn
On Thursday, 30 November 2023 at 14:10:35 UTC, zjh wrote: On Wednesday, 29 November 2023 at 16:38:36 UTC, Dom DiSc wrote: ```d struct S { int a; int b; } S2 fun3() { return S2( 5, 2 ); } ``` Here,`S2( 5, 2 );` violeit `DRY` principle. Yes. I think if we have the brackets form, it should be

Re: struct initializer

2023-11-29 Thread Dom DiSc via Digitalmars-d-learn
On Wednesday, 29 November 2023 at 16:48:09 UTC, Paul Backus wrote: You can use this syntax without an explicit constructor: struct S3 { int a; int b; } S3 fun() { return S3(5, 2); } The language spec calls this a struct literal Ok, so we have ```d struct S { int a; int b; } S s =

Re: struct initializer

2023-11-29 Thread Dom DiSc via Digitalmars-d-learn
Sorry, I meant ```d fun2({4, 4}); // doesn't work ```

struct initializer

2023-11-29 Thread Dom DiSc via Digitalmars-d-learn
```d struct S { int a; int b; } S s = { 5, 2 }; // works fine S fun() { return { 5, 2 }; } // doesn't work :-( S fun2() { S s = { 5, 2 }; return s; } // works but is ugly struct S2 { int a; int b; this(int c, int d) { a=c; b=d; } } S2 fun3() { return S2( 5, 2 ); } // works but requires

Re: interface inference

2023-11-28 Thread Dom DiSc via Digitalmars-d-learn
On Tuesday, 28 November 2023 at 11:01:14 UTC, Antonio wrote: ```d I aOrB(bool check){ if(check) return new A(); else return new B(); } ``` **Is it the expected behaviour for ternary conditional?** Here the compiler knows what type to return (from the function signature). But the

Re: D: Convert/parse uint integer to string. (@nogc)

2023-11-28 Thread Dom DiSc via Digitalmars-d-learn
On Tuesday, 28 November 2023 at 08:51:21 UTC, Mark Davies wrote: On Friday, 24 November 2023 at 09:35:00 UTC, BoQsc wrote: ``` import std.stdio; char[10] longToString(long n) @nogc ``` For a 'long' 10 characters is likely to be not enough (long max is 9223372036854775808 which has 19 chars,

Re: How does D’s ‘import’ work?

2023-06-03 Thread Dom DiSc via Digitalmars-d-learn
On Thursday, 1 June 2023 at 03:47:00 UTC, Cecil Ward wrote: I have another question if I may, what do we do about getting makefiles right given that we have imports ? You can replace your whole makefile by calling the compiler with -I (not always, but if you don't do funny things in your

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

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: Concepts like c++20 with specialized overload resolution.

2023-05-28 Thread Dom DiSc via Digitalmars-d-learn
On Saturday, 27 May 2023 at 19:16:18 UTC, vushu wrote: It depends. This example is quite small and a `static if` is sufficient, if you have a lot of cases it would make sense to split thing up into overloaded functions. Even with a lot of cases you can simply call in each static if a

Re: Proper way to handle "alias this" deprecation for classes

2023-05-17 Thread Dom DiSc via Digitalmars-d-learn
On Friday, 12 May 2023 at 15:00:48 UTC, Salih Dincer wrote: ```d struct Fraction { int num, den; this(int n, int d) { num = n; den = d; } // Cast Expression : convert float value of fraction auto opCast(T :

Re: learning D as your first language but having difficulty in making or logic building in order to make software

2023-04-12 Thread Dom DiSc via Digitalmars-d-learn
On Tuesday, 11 April 2023 at 14:13:17 UTC, slectr wrote: I want to make software like krita inkscape and my own language using D Although i previosly learned C and C++ i left it in the middle and for some reasons i dont want to learn those so i searched for alternatives and found D but

Re: How to use @safe when a C library integration needed

2023-01-23 Thread Dom DiSc via Digitalmars-d-learn
On Monday, 23 January 2023 at 16:36:21 UTC, Leonardo wrote: Hello. How to use @safe when a C library integration needed? Everything need a system function... ```d @safe fn() { // lot of safe stuff () @trusted { // in this block[*] @system function like extern C can be called.

Re: Is there a way to enforce UFCS?

2023-01-05 Thread Dom DiSc via Digitalmars-d-learn
On Wednesday, 4 January 2023 at 14:21:46 UTC, bauss wrote: ```d class Foo { int bar; void setBar(Foo foo, int value) { foo.bar = value; } } void main() { foo.setBar(100); // Not UFCS - just method call to the class foo.setBar = 100; // Not UFCS - simply a setter function call

Re: Fix template parameter

2022-08-10 Thread Dom Disc via Digitalmars-d-learn
On Tuesday, 9 August 2022 at 22:58:16 UTC, Paul Backus wrote: On Tuesday, 9 August 2022 at 22:36:23 UTC, Dom Disc wrote: On Tuesday, 9 August 2022 at 22:32:23 UTC, Dom Disc wrote: On Tuesday, 9 August 2022 at 21:16:22 UTC, Paul Backus wrote: Yes, this syntax allows anything that implicitly

Re: Fix template parameter

2022-08-09 Thread Dom Disc via Digitalmars-d-learn
On Tuesday, 9 August 2022 at 22:32:23 UTC, Dom Disc wrote: On Tuesday, 9 August 2022 at 21:16:22 UTC, Paul Backus wrote: Yes, this syntax allows anything that implicitly converts to `BigInt`; Oh, or do you mean I will get two different instances of the template, if I call it with two

Re: Fix template parameter

2022-08-09 Thread Dom Disc via Digitalmars-d-learn
On Tuesday, 9 August 2022 at 21:16:22 UTC, Paul Backus wrote: Yes, this syntax allows anything that implicitly converts to `BigInt`; for example: ```d import std.bigint; void fun(T : BigInt)(T t) { pragma(msg, "Instantiated with T = `" ~ T.stringof ~ "`"); } struct S { BigInt n;

Re: Fix template parameter

2022-08-08 Thread Dom Disc via Digitalmars-d-learn
On Monday, 8 August 2022 at 12:46:48 UTC, bauss wrote: On Monday, 8 August 2022 at 12:02:02 UTC, Dom Disc wrote: ```D pure @nogc @safe BigInt opAssign(T : BigInt)(T x); ``` This will only be included in the object file if used. ```D pure @nogc @safe BigInt opAssign(BigInt x); ``` This

Fix template parameter

2022-08-08 Thread Dom Disc via Digitalmars-d-learn
Hello. I found in the documentation functions declared like this: ```D pure @nogc @safe BigInt opAssign(T : BigInt)(T x); ``` What is the difference to declaring it like: ```D pure @nogc @safe BigInt opAssign(BigInt x); ``` To me the first declaration seems to be unnecessarily bloated, so I

Re: How do I initialize a templated constructor?

2022-08-08 Thread Dom Disc via Digitalmars-d-learn
And then you can instantiate it with ```D auto val = TestArray!10(ubyte(60)); // if you want type to be ubyte ```

Re: How do I initialize a templated constructor?

2022-08-08 Thread Dom Disc via Digitalmars-d-learn
On Monday, 8 August 2022 at 06:58:42 UTC, bauss wrote: On Monday, 8 August 2022 at 05:38:31 UTC, rempas wrote: In the following struct (as an example, not real code): ``` struct TestArray(ulong element_n) { int[element_n] elements; this(string type)(ulong number) { pragma(msg, "The

Re: Verbosity in D

2022-08-07 Thread Dom Disc via Digitalmars-d-learn
On Monday, 8 August 2022 at 00:40:11 UTC, TTK Ciar wrote: On the other hand, I've noticed that D's idiomatic brevity can be diluted by the extremely verbose function names used in the standard library. For long function names you can define short aliases, for syntax you can't. So having

Re: Arbitrary precision decimal numbers

2022-08-06 Thread Dom Disc via Digitalmars-d-learn
On Saturday, 6 August 2022 at 11:25:28 UTC, Dom Disc wrote: I once did a completely inline implementation of xlcmplx Sorry, one function is NOT inline: ```C class xlcmplx { uint32 Decimal(char* s, uint32 max) const; // string representation } ``` But you should forget about that, because D

Re: Arbitrary precision decimal numbers

2022-08-06 Thread Dom Disc via Digitalmars-d-learn
On Friday, 5 August 2022 at 14:25:39 UTC, Ruby The Roobster wrote: I'm currently working on an arbitrarily precise division algortihm based off of the done-by-hand standard algorithm, but I need to get it to work for complex numbers, [which it's just

Re: Compiler switch for integer comparison/promotion to catch a simple error

2022-05-30 Thread Dom Disc via Digitalmars-d-learn
On Sunday, 29 May 2022 at 01:35:23 UTC, frame wrote: Is there a compiler switch to catch this kind of error? ```d ulong v = 1; writeln(v > -1); ``` IMHO the compiler should bail a warning if it sees a logic comparison between signed and unsigned / different integer sizes. There is 50% chance

Re: What are (were) the most difficult parts of D?

2022-05-17 Thread Dom Disc via Digitalmars-d-learn
On Tuesday, 17 May 2022 at 06:28:10 UTC, cc wrote: having had this leave a pretty bad taste in my mouth, I now avoid the GC whenever possible, even when I don't have to. Bad idea. You should only avoid GC if your profiling shows that it is bad in a specific piece of code (e.g. some inner loop

Re: Question on shapes

2022-05-17 Thread Dom Disc via Digitalmars-d-learn
On Tuesday, 17 May 2022 at 09:30:12 UTC, forkit wrote: On Tuesday, 17 May 2022 at 04:37:58 UTC, Ali Çehreli wrote: In you OOP example, I am curious why you chose Shape to be an interface, rather than a base class. You can inherit from multiple interfaces, but only from one base class. So

Re: Why do immutable variables need reference counting?

2022-04-12 Thread Dom DiSc via Digitalmars-d-learn
On Monday, 11 April 2022 at 22:10:07 UTC, Ali Çehreli wrote: 1) I will not mutate data through this reference. For example, a parameter that is pointer to const achieves that: void foo (const(int)[] arr); 2) This variable is const: const i = 42; Well, there is the confusion: There is no

Re: abs and minimum values

2021-10-30 Thread Dom DiSc via Digitalmars-d-learn
On Friday, 29 October 2021 at 14:20:09 UTC, Ali Çehreli wrote: Unsigned!T abs(T)(const(T) x) if(isIntegral!T) { static if(isSigned!T) if(x < 0) return cast(Unsigned!T)-x; return x; } void main() { int a = -5; int b = -4; writeln(a + abs(b)); // -5 + 4 == -1? (No!) } The program

Re: abs and minimum values

2021-10-29 Thread Dom DiSc via Digitalmars-d-learn
On Friday, 29 October 2021 at 08:33:07 UTC, Imperatorn wrote: Imo abs should never be able to return a negative value Yes, but phobos defines it to return a signed type, so theoretical it can return negative values. And they won't change that. I really think it should return an unsigned

Re: abs and minimum values

2021-10-29 Thread Dom DiSc via Digitalmars-d-learn
On Thursday, 28 October 2021 at 21:26:04 UTC, kyle wrote: Okay I checked the phobos docs and it does say "Limitations Does not work correctly for signed intergal types and value Num.min." Should have looked there first, I know. Still seems pretty silly. I recommend to implement your own abs