Re: struct initializer

2023-11-29 Thread Antonio via Digitalmars-d-learn
On Wednesday, 29 November 2023 at 16:48:09 UTC, Paul Backus wrote: ... it even supports named arguments: - Witch version of DMD supports named arguments? Is it an experimental compiler option?

Re: interface inference

2023-11-28 Thread Antonio via Digitalmars-d-learn
On Tuesday, 28 November 2023 at 15:46:18 UTC, Paul Backus wrote: On Thursday, 23 November 2023 at 19:17:20 UTC, Antonio wrote: Basically, the ternary conditional ```?:``` result type is not inferred even if the type returned by the two possibilities are the same. **Is it a bug or the

Re: interface inference

2023-11-28 Thread Antonio via Digitalmars-d-learn
On Tuesday, 28 November 2023 at 14:10:30 UTC, Dom DiSc wrote: 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

Re: interface inference

2023-11-28 Thread Antonio via Digitalmars-d-learn
On Thursday, 23 November 2023 at 19:17:20 UTC, Antonio wrote: ```d interface I { bool check(); } class A : I { bool check() =>true; } class B : I { bool check() =>false; } I aOrB(bool check) => check ? new A() : new B(); void main() { assert( aOrB(true).check ); } ```

Re: interface opEquals

2023-11-27 Thread Antonio via Digitalmars-d-learn
On Saturday, 25 November 2023 at 01:15:34 UTC, Alexandru Ermicioi wrote: On Friday, 24 November 2023 at 17:39:10 UTC, Antonio wrote: ... Dunno if this might help, but I noticed that `==` sometimes calls `opEquals(const Object) const` instead of overload defined on class/interface, you might

Why this compiles?

2023-11-26 Thread Antonio via Digitalmars-d-learn
In this example, ```a``` and ```b``` vars are not of the same type and don't implement the same interface. **why ```assert(a==b)``` compiles?** ```d interface IOpt(T) { bool opEquals(const IOpt!T) const @safe pure; } class None(T) : IOpt!T { bool opEquals(const IOpt!T other) const

Re: interface opEquals

2023-11-24 Thread Antonio via Digitalmars-d-learn
On Thursday, 23 November 2023 at 21:52:56 UTC, Jonathan M Davis wrote: I'd have to take the time to study your code in detail to see whether what exactly you're seeing makes sense or not ... My apologies... I should have proposed a more specific example. ```d interface IOpt { bool

Re: Doubt about type Inference on templates

2023-11-23 Thread Antonio via Digitalmars-d-learn
On Wednesday, 22 November 2023 at 19:37:58 UTC, Paul Backus wrote: This is a bug/limitation in the compiler. I couldn't find an existing report on issues.dlang.org, so I've reported it myself as [issue 24255][1]. Wow: It is a very concise bug example. I tested with ```ldc``` ant it fails

interface opEquals

2023-11-23 Thread Antonio via Digitalmars-d-learn
```d interface IOpt(T) { T value(); bool empty(); bool opEquals(IOpt!T other); } class None(T) : IOpt!T { bool empty() => true; T value(){ throw new Exception("None has not a value"); } bool opEquals(IOpt!T other)=>other.empty; } class Some(T) : IOpt!T { this(T value) {

interface inference

2023-11-23 Thread Antonio via Digitalmars-d-learn
```d interface I { bool check(); } class A : I { bool check() =>true; } class B : I { bool check() =>false; } I aOrB(bool check) => check ? new A() : new B(); void main() { assert( aOrB(true).check ); } ``` Compiler error: ```d x.d(11): Error: cannot implicitly

Doubt about type Inference on templates

2023-11-22 Thread Antonio via Digitalmars-d-learn
Just for fun, I'm trying to implement an alternative base library to avoid template/mixin/static/traits code with only one objective: make "intelliSense" code analyzers tasks easier. I need "Generics"... but D has not generics: I use templates in the "simplest" possible way I.E.: ```d

Re: What is :-) ?

2023-11-21 Thread Antonio via Digitalmars-d-learn
On Monday, 20 November 2023 at 16:47:13 UTC, Paul Backus wrote: You can put the `delegate` keyword in front of the function literal: ```d auto createCounter = delegate (int nextValue) => () => nextValue++; ``` This syntax is documented in the spec's section on [Function Literals][2]

Re: What is :-) ?

2023-11-21 Thread Antonio via Digitalmars-d-learn
On Monday, 20 November 2023 at 16:32:22 UTC, evilrat wrote: ```d // this is a function returning a delegate auto createCounter(int nextValue) => auto delegate() => nextValue++; Thank you!!!. Compiler forces me to omit "auto" keyword ```d auto createCounter(int nextValue) => delegate ()

Re: What is :-) ?

2023-11-20 Thread Antonio via Digitalmars-d-learn
On Monday, 20 November 2023 at 13:25:48 UTC, Paul Backus wrote: On Monday, 20 November 2023 at 08:47:34 UTC, Antonio wrote: - What is the way to do ```writeln``` work with ```Counter``` function the same way it works with ```next``` function? `writeln()` should do it. It does not do the

Re: What is :-) ?

2023-11-20 Thread Antonio via Digitalmars-d-learn
On Monday, 20 November 2023 at 09:11:07 UTC, evilrat wrote: if you meant to take the function/delegate and not invoke try `` instead, otherwise it expects the parameters. If you execute ``` writeln( "'Counter' is ", ); ``` It shows the Counter address: ``` 'Counter' is 557F2567F940 ```

What is :-) ?

2023-11-20 Thread Antonio via Digitalmars-d-learn
If I run this code ```d import std.stdio; void main(){ auto next = Counter(10); next().writeln; next().writeln; next().writeln; // What is "next" function? writeln( "'next' is ", next ); // What is "Counter" function? This fails // writeln( "'Counter' is ", Counter ); } auto

Re: how to assign multiple variables at once by unpacking array?

2023-10-11 Thread Antonio via Digitalmars-d-learn
On Sunday, 8 October 2023 at 06:02:14 UTC, Jonathan M Davis wrote: The problem is that the compiler needs to be able to verify that the types match, and when the return type of a function is a dynamic array such as int[], it has no way of knowing how many elements the array has and

Re: My programs issues

2022-08-12 Thread Antonio via Digitalmars-d-learn
On Wednesday, 10 August 2022 at 13:13:20 UTC, Adam D Ruppe wrote: On Wednesday, 10 August 2022 at 12:36:42 UTC, pascal111 wrote: 1) I used "exit()" from "core.stdc.stdlib;" module, but someone can say this isn't the D way to exit the program. It is better to simply return a value from main

Re: BASIC "sgn" function equivalent

2022-07-28 Thread Antonio via Digitalmars-d-learn
On Thursday, 28 July 2022 at 15:03:34 UTC, H. S. Teoh wrote: Just use the source, Luke! Phobos is open source for a reason. https://github.com/dlang/phobos/blob/8280b1e7de6cca4dc9a593431591054a5b3aa288/std/math/traits.d#L694 T :-) ```d // @@@TODO@@@: make this faster ```

Re: BASIC "sgn" function equivalent

2022-07-28 Thread Antonio via Digitalmars-d-learn
On Thursday, 28 July 2022 at 12:13:31 UTC, Antonio wrote: Use isFloating!T and isIntegral!T traits. The standard library **sng** function is a good example: https://dlang.org/library/std/math/traits/sgn.html ```d import std.traits : isFloatingPoint, isIntegral; int sgn(T)(T x) if

Re: BASIC "sgn" function equivalent

2022-07-28 Thread Antonio via Digitalmars-d-learn
On Thursday, 28 July 2022 at 12:02:54 UTC, pascal111 wrote: I'm making an equivalent of "sgn" function of BASIC language, and I used "(T)" in its definition, but the function can receive wrong data by passing string data to it, how we can solve it? Use isFloating!T and isIntegral!T traits.

Re: null == "" is true?

2022-07-20 Thread Antonio via Digitalmars-d-learn
On Wednesday, 20 July 2022 at 13:35:14 UTC, Kagamin wrote: On Tuesday, 19 July 2022 at 18:05:34 UTC, Antonio wrote: In a relational database, `NULL` is not the same that `""`... and `NULL` is not the same that `0`. Are semantically different and there are database invariants (like foreign

Re: null == "" is true?

2022-07-20 Thread Antonio via Digitalmars-d-learn
On Tuesday, 19 July 2022 at 16:55:39 UTC, Kagamin wrote: As I understand, in your scenario there's no difference between null string and empty string, they both work like empty string, and D treats them as empty string. That's what I mean when I said that distinction between null and empty

Re: null == "" is true?

2022-07-19 Thread Antonio via Digitalmars-d-learn
On Tuesday, 19 July 2022 at 17:05:27 UTC, Kagamin wrote: Also what's the difference between null and empty phone number? In a relational database, `NULL` is not the same that `""`... and `NULL` is not the same that `0`. Are semantically different and there are database invariants (like

Re: null == "" is true?

2022-07-19 Thread Antonio via Digitalmars-d-learn
On Tuesday, 19 July 2022 at 08:10:25 UTC, Kagamin wrote: On Monday, 18 July 2022 at 21:23:32 UTC, Antonio wrote: I will study it in detail and report (if required). May be, I will write the DTO problem with D article if I find time in august. In my experience null and empty in DTOs usually

Re: null == "" is true?

2022-07-18 Thread Antonio via Digitalmars-d-learn
On Monday, 18 July 2022 at 17:20:04 UTC, Kagamin wrote: ... If you want such difference, use the Nullable wrapper or Algebraic. I do :-) In fact, I use algebraic types supporting Null and Undefined for DTOs representation (and REST APIs). But I discovered some "rare" side effects in

Re: null == "" is true?

2022-07-12 Thread Antonio via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 20:36:03 UTC, Antonio wrote: Honestly, it is difficult to understand for newcomers... there is a reason, but there is a reason in javascript for `0 == ''` too Correction ```d string a = null; assert(a is null); assert(a == ""); string b = ""; assert(b !is null);

Re: null == "" is true?

2022-07-12 Thread Antonio via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 18:56:43 UTC, Paul Backus wrote: On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: Because an empty string is, by default, represented by an empty slice of the null pointer. Do not rely on this, however; it's possible sometimes to get an empty string

null == "" is true?

2022-07-12 Thread Antonio via Digitalmars-d-learn
It works ```d void main() { assert(null==""); } ``` why?

vibe.d Serialize/Deserialize SumType to/from json

2022-07-04 Thread Antonio via Digitalmars-d-learn
D offers `SumType` struct to manage tagged Union types. But there is no support for managing it's Json serialization/deserialization (using vibe.d) Is it a way to add `fromRepresentation` and `toRepresentation` to `SumType` (or a way for creating a custom struct "inheriting" SumType with

Re: How to check if something can be null

2022-07-01 Thread Antonio via Digitalmars-d-learn
On Friday, 1 July 2022 at 15:35:00 UTC, Adam D Ruppe wrote: On Friday, 1 July 2022 at 13:48:25 UTC, Antonio wrote: I has been using this pattern each time something needs special treatment when it can be null: i'd prolly check `static if(is(typeof(null) : T))` which means if the null literal

Re: How to check if something can be null

2022-07-01 Thread Antonio via Digitalmars-d-learn
On Friday, 1 July 2022 at 13:48:25 UTC, Antonio wrote: -Why? I realized Json is an struct (not an object)... and I supose, it is managing null asignation manually (as a way to build Json(null)). -Whats the correct whay to test if something can be null? That's my question :-p

How to check if something can be null

2022-07-01 Thread Antonio via Digitalmars-d-learn
I has been using this pattern each time something needs special treatment when it can be null: ```d void doSomething(T)(T v) { import std.traits: isAssignable; static if( isAssignable!(T, typeof(null))) { if(v is null) writeln("This is null"); else writeln("This is not

Re: int | missing | absent

2022-06-27 Thread Antonio via Digitalmars-d-learn
On Monday, 27 June 2022 at 23:05:46 UTC, Steven Schveighoffer wrote: On 6/27/22 9:03 AM, Antonio wrote: On Wednesday, 22 June 2022 at 01:09:22 UTC, Steven Schveighoffer wrote: On 6/2/22 9:24 AM, bauss wrote: I feel it's too loose to make a best effort, and leave the rest up to initial

Re: int | missing | absent

2022-06-27 Thread Antonio via Digitalmars-d-learn
On Monday, 27 June 2022 at 23:05:46 UTC, Steven Schveighoffer wrote: ... Maybe you can provide an example, and there may be a solution that you haven't thought of. -Steve I first posted this "issue" to vibe-d: https://github.com/vibe-d/vibe.d/issues/2673

Re: int | missing | absent

2022-06-27 Thread Antonio via Digitalmars-d-learn
On Wednesday, 22 June 2022 at 01:09:22 UTC, Steven Schveighoffer wrote: On 6/2/22 9:24 AM, bauss wrote: I feel it's too loose to make a best effort, and leave the rest up to initial values, or just ignore possibly important information during parsing. -Steve May be for your case Steve. I

Re: Static Initialization of Structs syntax

2022-06-22 Thread Antonio via Digitalmars-d-learn
On Wednesday, 22 June 2022 at 09:41:55 UTC, Antonio wrote: I'm so sorry, I know that the above example doesn't work: ```d main(){ Struct PersonDTO { string name; string surname; } void create(PersonDTO personData){ // ... } create( {name: "Peter", surname: "Ustinov"} );

Static Initialization of Structs syntax

2022-06-22 Thread Antonio via Digitalmars-d-learn
I'm so sorry, I know that the above example doesn't work: ```d main(){ Struct PersonDTO { string name; string surname; } void create(PersonDTO personData){ // ... } create( {name: "Peter", surname: "Ustinov"} ); } ``` -Is it there any alternative to initialize "inline"

Re: int | missing | absent

2022-06-21 Thread Antonio via Digitalmars-d-learn
On Thursday, 2 June 2022 at 13:24:08 UTC, bauss wrote: On Thursday, 2 June 2022 at 08:27:32 UTC, Antonio wrote: JSON properties can be - a value - null - absent What's the standard way to define a serialziable/deserializable structs supporting properties of any of this 4 kinds?: * int *

Re: destroy and @safe

2022-06-21 Thread Antonio via Digitalmars-d-learn
On Tuesday, 21 June 2022 at 16:20:32 UTC, Antonio wrote: My code starts to be a @safe/@trusted mess (because external libraries). The only solution I have is to "wrap" them or to trust all code by default (I'm using vibe.d that forces @safe code) Only as a comment: I can remember now when

Re: destroy and @safe

2022-06-21 Thread Antonio via Digitalmars-d-learn
On Tuesday, 21 June 2022 at 15:14:43 UTC, Steven Schveighoffer wrote: You delegate doesn't seem to be marked @safe as well. Thanks a lot Steve, I didn't found a way (or example) to specify the delegate must be @safe until I have found vibe.d.db.postgress implementation (that you

Re: destroy and @safe

2022-06-21 Thread Antonio via Digitalmars-d-learn
On Tuesday, 21 June 2022 at 15:13:36 UTC, Paul Backus wrote: If the destructor is `@system`, then the only way to call `destroy` in `@safe` code is to (1) determine the conditions necessary to call the destructor without violating memory safety, (2) ensure that those conditions are met (using

destroy and @safe

2022-06-21 Thread Antonio via Digitalmars-d-learn
I'm using explicitly destroy!false(obj) for a "deterministic" resources release. I replicate the c# "using" pattern, or the python "with" pattern with my own "use" template supposing object are RAII i.e.: ```d Item[] items = query("...").use( (Answer a) =>

Re: Enforce not null at compile time?

2022-06-20 Thread Antonio via Digitalmars-d-learn
On Monday, 20 June 2022 at 19:08:32 UTC, max haughton wrote: On Monday, 20 June 2022 at 17:48:48 UTC, Antonio wrote: Is there any way to specify that a variable, member or parameter can't be null? You can use an invariant if it's a member of an aggregate but be warned that these are only

Enforce not null at compile time?

2022-06-20 Thread Antonio via Digitalmars-d-learn
Is there any way to specify that a variable, member or parameter can't be null?

Re: UFCS limit

2022-06-17 Thread Antonio via Digitalmars-d-learn
On Friday, 17 June 2022 at 12:26:05 UTC, Antonio wrote: UFCS vs Functional curring... nice battle :-) **UFCS & CFTE** vs **Functional currying**... nice battle :-)

Re: UFCS limit

2022-06-17 Thread Antonio via Digitalmars-d-learn
On Friday, 17 June 2022 at 01:04:28 UTC, Paul Backus wrote: On Thursday, 16 June 2022 at 23:59:06 UTC, Antonio wrote: Is it there any way to apply UFCS on the returned method in the same expression? Nope. The way UFCS works is that allows you to call free functions using member-function

UFCS limit

2022-06-16 Thread Antonio via Digitalmars-d-learn
```d auto doSomething(string text) { return (string text2) { import std.stdio; writeln(text,",",text2); }; } void main() { doSomething("Hello")("X"); "X".doSomething("Hello")(); } ``` Compiler error: ``` ... onlineapp.d(13):expected 1 argument(s), not 2 ``` I tried

Re: map! evaluates twice

2022-06-12 Thread Antonio via Digitalmars-d-learn
On Friday, 10 June 2022 at 20:47:14 UTC, Steven Schveighoffer wrote: On 6/10/22 4:33 PM, Antonio wrote: ... `map` calls the lambda for each call to `front`. If you want a cached version, use `cache`: Thank you very much, Steve

map! evaluates twice

2022-06-10 Thread Antonio via Digitalmars-d-learn
When mapping and filtering, the last mapped element is evaluated twice... Is it the expected behaviour? ```d void main() { import std.algorithm, std.stdio; [1,2,3,4,5]. map!((x){ writeln("mapping ", x); return x; }).

Re: Range to Nullable conversion

2022-06-10 Thread Antonio via Digitalmars-d-learn
On Friday, 10 June 2022 at 18:00:20 UTC, Paul Backus wrote: On Friday, 10 June 2022 at 17:22:53 UTC, Antonio wrote: Can this code be written as a **simple** expression? (without having to write helper methods). ```d import std.range, std.typecons; Nullable!(ElementType!R) maybeFront(R)(auto

Re: Range to Nullable conversion

2022-06-10 Thread Antonio via Digitalmars-d-learn
On Friday, 10 June 2022 at 17:37:13 UTC, Ali Çehreli wrote: On 6/10/22 10:22, Antonio wrote: > Is there any alternative to ***range front*** that returns a Nullable > (i.e. **frontAsMonad** or **frontAsNullable**)? import std; // Spelling? :) auto nullablelize(R)(R range) { ... } void

Range to Nullable conversion

2022-06-10 Thread Antonio via Digitalmars-d-learn
Is there any alternative to ***range front*** that returns a Nullable (i.e. **frontAsMonad** or **frontAsNullable**)? I'm using Vibe.d and Nullable is the standard way to return an optional element: ```d @safe Nullable!Item getItem(int _id) { import std.algorithm : filter; with

int | missing | absent

2022-06-02 Thread Antonio via Digitalmars-d-learn
JSON properties can be - a value - null - absent What's the standard way to define a serialziable/deserializable structs supporting properties of any of this 4 kinds?: * int * int | null * int | absent * int | null | absent Whats the best library to manage this JSON requirements? (all the

Re: UFCS doubt

2021-07-08 Thread Antonio via Digitalmars-d-learn
On Thursday, 8 July 2021 at 22:31:49 UTC, Dennis wrote: On Thursday, 8 July 2021 at 22:24:26 UTC, Antonio wrote: I supossed that ```mfp(c,20)``` and ```c.mfp(20)``` should be equivalent because UFCS in second example, but it is not... why? UFCS does not work for nested functions. Functions

UFCS doubt

2021-07-08 Thread Antonio via Digitalmars-d-learn
In this example (extracted from https://digitalmars.com/articles/b68.html), this works: ``` class C { int a; int foo(int i) { return i + a; } } auto mfp = (C self, int i)=>self.foo(i); void main(){ auto c = new C; assert( c.mfp(20)==20); } ``` but this fails ``` class C { int a;

github copilot and dlang

2021-07-05 Thread Antonio via Digitalmars-d-learn
Has someone tried github copilot (https://copilot.github.com/) with dlang? Access to the preview could be requested and, I think, main dlang team members could bypass the waitlist easily. I suspect that the "low" volume of dlang code (used to train OpenAI) compared to other languages could

Re: Fix gtkD api display

2017-08-11 Thread Antonio via Digitalmars-d-learn
On Thursday, 10 August 2017 at 14:59:52 UTC, Adam D. Ruppe wrote: On Thursday, 10 August 2017 at 14:55:06 UTC, Mike Wey wrote: [...] Oh, I see. My generator lists them on the index, but doesn't recreate it each time, it just links. For example: