digitalmars.D - Avoid attribute soup by doing attribute-infering for function(s) of
- An Pham (35/35) Jan 29 Sample below, F.get, get error for class expected but OK for
- Paul Backus (9/13) Jan 29 1. D already does inference of `@safe`, `pure`, `nothrow`, and
- An Pham (4/17) Jan 29 The problem for 'const' is that it is transitive. Without adding
- Paul Backus (8/11) Jan 29 You can make it work for classes by making the return type
- Quirin Schroll (4/17) Feb 04 I know that it’s not what was meant, but you can use [template a
- Kagamin (11/11) Jan 30 You probably want `inout` here, it infers const qualifiers.
Sample below, F.get, get error for class expected but OK for value type (no indirect type). D needs to do attribute inference (const nothrow pure safe) for function(s) within template type module X; safe: struct F(T) { T[] data; T get(size_t i) const nothrow safe { return data[i]; } } struct T1 { int x; } class T2 { int x; } void main() { import std.stdio; F!T1 t1; writeln(t1.get(0)); F!T2 t2; writeln(t2.get(0)); /* onlineapp.d(11): Error: cannot implicitly convert expression `this.data[i]` of type `const(T2)` to `X.T2` onlineapp.d(32): Error: template instance `X.F!(T2)` error instantiating */ }
Jan 29
On Wednesday, 29 January 2025 at 20:43:47 UTC, An Pham wrote:Sample below, F.get, get error for class expected but OK for value type (no indirect type). D needs to do attribute inference (const nothrow pure safe) for function(s) within template type1. D already does inference of ` safe`, `pure`, `nothrow`, and ` nogc` for functions within template types. (`const`, however, is _never_ inferred, under any circumstances.) 2. I don't see anything wrong in your example. Conversion of value types without indirections from `const` to non-`const` is [documented in the language spec.][1] [1]: https://dlang.org/spec/const3.html#implicit_qualifier_conversions
Jan 29
On Wednesday, 29 January 2025 at 21:10:53 UTC, Paul Backus wrote:On Wednesday, 29 January 2025 at 20:43:47 UTC, An Pham wrote:The problem for 'const' is that it is transitive. Without adding it, it has problem using from 'const' caller but adding 'const' will prohibit it used in 'class' typeSample below, F.get, get error for class expected but OK for value type (no indirect type). D needs to do attribute inference (const nothrow pure safe) for function(s) within template type1. D already does inference of ` safe`, `pure`, `nothrow`, and ` nogc` for functions within template types. (`const`, however, is _never_ inferred, under any circumstances.) 2. I don't see anything wrong in your example. Conversion of value types without indirections from `const` to non-`const` is [documented in the language spec.][1] [1]: https://dlang.org/spec/const3.html#implicit_qualifier_conversions
Jan 29
On Wednesday, 29 January 2025 at 22:24:56 UTC, An Pham wrote:The problem for 'const' is that it is transitive. Without adding it, it has problem using from 'const' caller but adding 'const' will prohibit it used in 'class' typeYou can make it work for classes by making the return type `const(T)`: const(T) get(size_t i) const nothrow safe { return data[i]; } This way, the types match, and no conversion is necessary.
Jan 29
On Wednesday, 29 January 2025 at 21:10:53 UTC, Paul Backus wrote:On Wednesday, 29 January 2025 at 20:43:47 UTC, An Pham wrote:I know that it’s not what was meant, but you can use [template a `this` parameter](https://dlang.org/spec/template.html#template_this_parameter) to infer `const` from the calling object.Sample below, F.get, get error for class expected but OK for value type (no indirect type). D needs to do attribute inference (const nothrow pure safe) for function(s) within template type1. D already does inference of ` safe`, `pure`, `nothrow`, and ` nogc` for functions within template types. (`const`, however, is _never_ inferred, under any circumstances.) 2. I don't see anything wrong in your example. Conversion of value types without indirections from `const` to non-`const` is [documented in the language spec.][1] [1]: https://dlang.org/spec/const3.html#implicit_qualifier_conversions
Feb 04
You probably want `inout` here, it infers const qualifiers. ``` struct F(T) { T[] data; inout(T) get(size_t i) inout nothrow safe { return data[i]; } } ```
Jan 30