Re: Assigning parameter on entry to a function and assigning back on exit
On Friday, 25 November 2022 at 11:01:09 UTC, Victor Porton wrote: Somewhere in my brain memory, it was written: A function argument that is both input and output, may be passed to the function either as reference or do two assignments: on entry of the function it is assigned to the parameter, on exit it is assigned back. Whether it is a reference or two assignments depends on the reference semantics of the type. Now I can't find this in the reference manual. Please help to refresh/correct my memory. Probably, in my memory this was stored regarding Ada and misattributed to D, wasn't it? Does D have or no this kind of feature?
Assigning parameter on entry to a function and assigning back on exit
Somewhere in my brain memory, it was written: A function argument that is both input and output, may be passed to the function either as reference or do two assignments: on entry of the function it is assigned to the parameter, on exit it is assigned back. Whether it is a reference or two assignments depends on the reference semantics of the type. Now I can't find this in the reference manual. Please help to refresh/correct my memory.
Re: `enum x;` - what is it?
On Wednesday, 19 August 2020 at 14:06:16 UTC, Victor Porton wrote: This declaration does compile: enum x; But what is it? Is it an equivalent of enum x { } ? What in the specification allows this looking a nonsense enum x; ? Oh, found: "An empty enum body (For example enum E;) signifies an opaque enum - the enum members are unknown." But what this "unknown" does mean? How "unknown" differs from "none" in this context? The specification is unclear. It does not define the meaning of unknown. I will submit a bug report.
`enum x;` - what is it?
This declaration does compile: enum x; But what is it? Is it an equivalent of enum x { } ? What in the specification allows this looking a nonsense enum x; ?
Why this eponymous template does not compile?
/// template sychronizedMemoize(alias fun) { void sychronizedMemoize() { } } void f() { } void main() { synchronizedMemoize!f(); } /// /tmp/temp_7F3C101460D0.d(9,5): Error: template instance `synchronizedMemoize!f` template `synchronizedMemoize` is not defined, did you mean sychronizedMemoize(alias fun)()? Why the error? Is it compiler bug? DMD v2.084.1
Why this template code does not compile?
Why this template code does not compile? /// module runnable; import std.meta; import std.typecons; template FieldInfo(argT, string argName) { template FieldInfo(Nullable!argT argDefault = Nullable!argT()) { } } alias processFields(T, string name) = AliasSeq!(FieldInfo!(T, name)()); void main(string[] args) { alias Fields = processFields!(int, "x"); } /// /tmp/temp_7F58BE2F0150.d(12,34): Error: template `runnable.FieldInfo!(int, "x").FieldInfo` cannot deduce function from argument types `!()()`, candidates are: /tmp/temp_7F58BE2F0150.d(7,5):`runnable.FieldInfo!(int, "x").FieldInfo(Nullable!int argDefault = Nullable!argT())` /tmp/temp_7F58BE2F0150.d(16,20): Error: template instance `runnable.processFields!(int, "x")` error instantiating LDC - the LLVM D compiler (1.11.0): based on DMD v2.081.2 and LLVM 6.0.1
Re: Why a template with Nullable does not compile?
On Tuesday, 12 March 2019 at 15:26:05 UTC, Victor Porton wrote: template FieldInfo(T, Nullable!T default_) { } On Tuesday, 12 March 2019 at 09:05:36 UTC, Nicholas Wilson wrote: It seems to be getting confused between the two types of Nullable, namely: Nullable(T), and Nullable(T, T defaultVal) I don't understand why exactly it is getting confused. How can it decide that "Nullable!T default_" is a two-arguments template when it is so not "Nullable!(T, default_)"? Please explain the EXACT cause of the error. My question why it is getting confused was not answered.
Re: Why a template with Nullable does not compile?
On Tuesday, 12 March 2019 at 16:20:11 UTC, H. S. Teoh wrote: On Tue, Mar 12, 2019 at 03:26:05PM +, Victor Porton via Digitalmars-d-learn wrote: [...] On Tuesday, 12 March 2019 at 09:05:36 UTC, Nicholas Wilson wrote: [...] > template FieldInfo(T) { > template FieldInfo(Nullable!(T) default_) > { > enum FieldInfo = 0; > } > } > > seems to work, but I can't seem to instantiate one of it. Why you use the same name "FieldInfo" for both the template and its subtemplate? Does it make some sense? This is a D idiom called the "eponymous template". Whenever the template contains a member of the same name as the template, it's an eponymous template, and you can refer directly to the member by the template name, rather than using templateName.memberName. For example, a template function is usually written like this: ReturnType myFunc(TemplateArgs...)(RuntimeArgs args...) { ... // implementation here } This is actually shorthand for the eponymous template: template myFunc(TemplateArgs...) { ReturnType myFunc(RuntimeArgs args...) { ... // implementation here } } Similarly, when you write: enum isInputRange(T) = hasMember!(T, empty) && ... that's actually shorthand for: template isInputRange(T) { enum isInputRange = hasMember!(T, empty) && ... } The eponymonus template idiom allows you to use a single name to refer to both the template and the member. Without this idiom, you'd have to use the very verbose notation: static if (isInputRange!T.isInputRange) ... or auto retval = myFunc!(A, B, C).myFunc(1, 2, 3); I know what is eponymous template. But how it behaves when the eponymous member inside itself is also a template? How to instantiate it? (provide please an example how to instantiate)
Re: Why a template with Nullable does not compile?
template FieldInfo(T, Nullable!T default_) { } On Tuesday, 12 March 2019 at 09:05:36 UTC, Nicholas Wilson wrote: It seems to be getting confused between the two types of Nullable, namely: Nullable(T), and Nullable(T, T defaultVal) I don't understand why exactly it is getting confused. How can it decide that "Nullable!T default_" is a two-arguments template when it is so not "Nullable!(T, default_)"? Please explain the EXACT cause of the error. template FieldInfo(T) { template FieldInfo(Nullable!(T) default_) { enum FieldInfo = 0; } } seems to work, but I can't seem to instantiate one of it. Why you use the same name "FieldInfo" for both the template and its subtemplate? Does it make some sense?
Re: Why a template with Nullable does not compile?
On Tuesday, 12 March 2019 at 05:14:21 UTC, Victor Porton wrote: Why does this not compile? import std.typecons; template FieldInfo(T, Nullable!T default_) { } /usr/lib/ldc/x86_64-linux-gnu/include/d/std/typecons.d(2570,17): Error: `alias T = T;` cannot alias itself, use a qualified name to create an overload set /usr/lib/ldc/x86_64-linux-gnu/include/d/std/typecons.d(3291,17): Error: `alias T = T;` cannot alias itself, use a qualified name to create an overload set LDC - the LLVM D compiler (1.11.0): based on DMD v2.081.2 and LLVM 6.0.1
Why a template with Nullable does not compile?
Why does this not compile? import std.typecons; template FieldInfo(T, Nullable!T default_) { } /usr/lib/ldc/x86_64-linux-gnu/include/d/std/typecons.d(2570,17): Error: `alias T = T;` cannot alias itself, use a qualified name to create an overload set /usr/lib/ldc/x86_64-linux-gnu/include/d/std/typecons.d(3291,17): Error: `alias T = T;` cannot alias itself, use a qualified name to create an overload set
Concatenating compile time sequences
I try to split a compile time sequence of types and names into a sequence consisting of two-element subsequences (each of type and name). That is, I want to transform: (int, "x", float, "y", double, "z") into (AliasSeq!(int, "x"), AliasSeq!(float, "y"), AliasSeq!(double, "z")) I am trying like this: private alias enum processFields() = AliasSeq!(); private alias enum processFields(T, name, Fields...) = AliasSeq!(AliasSeq!(T, name), processFields!(Fields)); But the above would (as I understand) make AliasSeq! returned by the recursively called processFields an element of the parent sequence rather than its tail subsequence as it should. Please help to fix the above code. I want namely a recursive implementation like the above, because I am going to generalize it for some more complex cases.
Re: My template tuple code does not compile
I rewrote it again: https://github.com/vporton/struct-params-dlang/blob/f50f7e5919f90b1d06bf0cc08e3055548aad1797/source/struct_params.d But it does not work :-( What is my error? source/struct_params.d(43,60): Error: function expected before `()`, not `()` of type `()` source/struct_params.d(44,43): Error: undefined identifier `fieldsWithDefaults` source/struct_params.d(56,11): Error: template instance `struct_params.ProviderParamsCode!("S", int, "x", float, "y")` error instantiating source/struct_params.d(82,5): Error: mixin `struct_params.__unittest_L81_C1.ProviderParams!("S", int, "x", float, "y")` error instantiating
Re: How to enumerate a sequence?
On Wednesday, 27 February 2019 at 13:15:06 UTC, Victor Porton wrote: .enumerate does not work for compile-time sequences. Consider for the sake of discussion the following nonsense (I know it cannot be done without enumerate) code: I want namely integer (or size_t) index!
How to enumerate a sequence?
.enumerate does not work for compile-time sequences. Consider for the sake of discussion the following nonsense (I know it cannot be done without enumerate) code: import std.meta; import std.range; string join(Fields...)() { enum f(size_t i) = __traits(identifier, Fields[i]); return staticMap!f(Fields.enumerate).join('\n'); // does not compile because of .enumerate } void main(string[] args) { string r = join!(int, float)(); } How to make it compile?
Re: My template tuple code does not compile
After following your suggestion to rewrite it with Stride it does not work either. I assume the error is somehow related to allSatisfy!. https://github.com/vporton/struct-params-dlang/blob/c1adc86672f46fd6b743903cc270dceef120a8fe/source/struct_params.d Please help. It is important for both D community and world at large.
Re: My template tuple code does not compile
On Tuesday, 26 February 2019 at 22:51:15 UTC, Q. Schroll wrote: On Tuesday, 26 February 2019 at 21:43:31 UTC, Victor Porton wrote: Compilation of unittest at the bottom of this file fails with an error. What is my error? ... You have the line ProviderParams("S", ((int, "x"), (float, "y"))); Why do you even expect it to compile? First, ProviderParams is a mixin template, so you'd need to instantiate it with the ! operator. Then, the expressions (int, "x") and so on do not make sense in D. You could have compile-time tuples (using AliasSeq from std.meta) containing types and other compile-time known stuff, but it wouldn't help directly. You'd use the mixin template like this: mixin ProviderParams!("S", int, "x", float, "y"); ^-- necessary ^-- necessary Grouping arguments could be done, but from my experience, it does not buy you anything; rather it makes it worse. Better just deal with heterogeneous stuff just like std.typecons.Tuple does. You should definitely comment your code. Explain what you intend to do. It helps people helping you. After fixing the error you pointed me, it does not work too: mixin ProviderParams!("S", ((int, "x"), (float, "y"))); Also: Can I nevertheless group arguments?
My template tuple code does not compile
Compilation of unittest at the bottom of this file fails with an error. What is my error? https://github.com/vporton/struct-params-dlang/blob/c32cfde60dbb03cb80a4a8aeb8185f5c86705790/source/struct_params.d It is very important both for this useful little D project and my bigger research project. Please help. source/struct_params.d(74,30): Error: found `,` when expecting `.` following int source/struct_params.d(74,32): Error: found `"x"` when expecting identifier following `int`. source/struct_params.d(74,44): Error: found `,` when expecting `.` following float source/struct_params.d(74,46): Error: found `"y"` when expecting identifier following `float`.
How to pass variables to string mixins?
I want to create a string mixin based on a supplementary variable (name2 below): Let we have something like: mixin template X(string name) { immutable string name2 = '_' ~ name; mixin("struct " ~ name2 ~ "{ int i; }"); } But it would create variable name2 inside X, which should not be created. How to solve this problem?
Simplifying a string mixin
Can string mixing be split into several parts? I have a mixin like this: mixin("struct " ~ name ~ " {\n" ~ " struct Regular {\n" ~ "// ..." ~ " }\n" ~ " struct WithDefaults {\n" ~ "// ..." ~ " }\n" ~ '}'); I would like to split it into several mixins (preferably using regular non-string mixins). Are things like this possible? Also, what is the most proper thing to check that `name` is a proper identified (not say !@#)?
Variadic template parameter (for mixin) does not work
I want to create a mixin with an arbitrary number of parameters. I tried this: mixin template ProviderParams(Types...)(Types t) { } But it does not compile. What's my error?
How to call other variadic function with the same arguments?
Let f be a variadic function: Result f(...); How to implement variadic function g which calls f with the same arguments as one it receives? Result g(...) { // ... }
How to create a class-valued variable?
What is the right way to store in a structure a class (not an instance) derived from a given interface(s)?
Should D file end with newline?
ISO C++ specifies that the C++ file must end with a newline. Should D file end with newline, too?
Re: Why -I flag does not work?
On Saturday, 9 February 2019 at 08:15:36 UTC, Victor Porton wrote: Why does -I flag in DFLAGS does not work? (Ubuntu Linux) https://github.com/dlang/dub/issues/1645
Re: Why -I flag does not work?
On Saturday, 9 February 2019 at 11:40:57 UTC, Victor Porton wrote: On Saturday, 9 February 2019 at 08:35:53 UTC, JN wrote: On Saturday, 9 February 2019 at 08:15:36 UTC, Victor Porton wrote: Why does -I flag in DFLAGS does not work? (Ubuntu Linux) I'm no expert on dub, but it's possible that it's overriding the import path anyway. One of the main points of dub is that you shouldn't have to handle the import paths yourself. I think what could work is adding a librdf in dub.json dependencies section, and then using "dub add-local" command to point to the library. Something like: dub add-local /usr/local/include/d/librdf ~master and then in dependencies section: "dependencies": { "librdf": "~master" } As I understand it would pollute the Git version of dub.json with my local changes. That's no good. I did // dub.json: "dependencies": { "rdf_dlang": "~>1.0.17", "ae": "~>0.0.2331" }, // dub.selections.json: { "fileVersion": 1, "versions": { "ae": "0.0.2331", "rdf_dlang": "1.0.17" } } I also installed /usr/local/include/d/librdf/dub.json with "sourcePaths": ["/usr/local/stow/rendland-bindings/include/d/librdf"] and did $ dub add-local /usr/local/include/d/librdf/ 1.0.17 But nevertheless: $ dub build --compiler=dmd --build=unittest Dynamic libraries are not yet supported as dependencies - building as static library. Performing "unittest" build using dmd for x86_64. xml-boiler-dlang ~master: building configuration "library"... source/xmlboiler/options.d(4,8): Error: module `model` is in file 'rdf/redland/model.d' which cannot be read import path[0] = source/ import path[1] = ../../.dub/packages/ae-0.0.2331/ae/sys import path[2] = ../../.dub/packages/ae-0.0.2331/ae/utils import path[3] = ../../.dub/packages/ae-0.0.2331/ae/net import path[4] = /snap/dmd/40/bin/../import/druntime import path[5] = /snap/dmd/40/bin/../import/phobos dmd failed with exit code 1.
Re: Why -I flag does not work?
On Saturday, 9 February 2019 at 08:35:53 UTC, JN wrote: On Saturday, 9 February 2019 at 08:15:36 UTC, Victor Porton wrote: Why does -I flag in DFLAGS does not work? (Ubuntu Linux) I'm no expert on dub, but it's possible that it's overriding the import path anyway. One of the main points of dub is that you shouldn't have to handle the import paths yourself. I think what could work is adding a librdf in dub.json dependencies section, and then using "dub add-local" command to point to the library. Something like: dub add-local /usr/local/include/d/librdf ~master and then in dependencies section: "dependencies": { "librdf": "~master" } As I understand it would pollute the Git version of dub.json with my local changes. That's no good.
Re: Why -I flag does not work?
On Saturday, 9 February 2019 at 10:32:36 UTC, DanielG wrote: On Saturday, 9 February 2019 at 08:15:36 UTC, Victor Porton wrote: Why does -I flag in DFLAGS does not work? (Ubuntu Linux) Try adding the -i flag as well ("include imported modules in -i in DFLAGS does not help. -I works when passed on the command line but not in DFLAGS :-( the compilation"), or setting both importPaths and sourcePaths in dub.json. This would pollute the Git version of the dub.json with my local changes. I'm not certain that will help, but long ago I was having similar trouble and it was because I was importing, but not compiling, a certain file. It should not be compiled, only imported. It was already compiled to a shared library.
Re: Why -I flag does not work?
On Saturday, 9 February 2019 at 08:15:36 UTC, Victor Porton wrote: $ DFLAGS="-I/usr/local/include/d/librdf -L-L/usr/local/lib" dub build --compiler=dmd --build=unittest ... Why does -I flag in DFLAGS does not work? (Ubuntu Linux) This does not work too: $ DFLAGS="-I/usr/local/include/d/librdf -L-L/usr/local/lib" dmd -unittest source/xmlboiler/options.d Why?!
Why -I flag does not work?
For this project: https://github.com/vporton/xml-boiler-dlang/tree/85b5587f50617ad1b001c035c72a5780a1e69c24 $ DFLAGS="-I/usr/local/include/d/librdf -L-L/usr/local/lib" dub build --compiler=dmd --build=unittest Performing "unittest" build using dmd for x86_64. xml-boiler-dlang ~master: building configuration "library"... source/xmlboiler/options.d(4,8): Error: module `model` is in file 'rdf/redland/model.d' which cannot be read import path[0] = source/ import path[1] = ../../.dub/packages/ae-0.0.2331/ae/sys import path[2] = ../../.dub/packages/ae-0.0.2331/ae/utils import path[3] = ../../.dub/packages/ae-0.0.2331/ae/net import path[4] = /snap/dmd/40/bin/../import/druntime import path[5] = /snap/dmd/40/bin/../import/phobos dmd failed with exit code 1. $ ls /usr/local/include/d/librdf/rdf/redland/model.d /usr/local/include/d/librdf/rdf/redland/model.d $ dmd --version DMD64 D Compiler v2.080.1 Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved written by Walter Bright Why does -I flag in DFLAGS does not work? (Ubuntu Linux)
Ordered set container?
I want "ordered set" container (like list or vector but with the warranty of no duplicate elements). Which type can I use?
Should I prefix package names with the name of my program?
Should I prefix all module names with `xmlboiler.` (where XML Boiler is the name of my program). These packages are expected to be used internally by my program, not as an exported API (however there are some little chances that in the future I will make a public API)
Re: unittest which uses a disk file
This way I would make data duplication (data files distributed with the source and the same data embedding as strings into my D sources). Note that the source is multilingual (I am currently working on a multi-language bindings of a C library).
Re: unittest which uses a disk file
On Wednesday, 16 January 2019 at 21:07:24 UTC, Victor Porton wrote: What is the rule for unittest which uses a file (containing example data for testing) available only in the source distribution, not in binary distribution? I am writing a library. The library has also a file main.d which is compiled only in DUB "application" configuration (I use this configuration solely for testing.) Maybe I should put unittest { } into main.d not in the module which I test? Also, what is the correct way to locate the file in the filesystem? Also if I choose to put any tests in main.d, should these tests be within unittest { }? main.d is anyway meant to be compiled only in unittest mode, so I'm unsure.
unittest which uses a disk file
What is the rule for unittest which uses a file (containing example data for testing) available only in the source distribution, not in binary distribution? I am writing a library. The library has also a file main.d which is compiled only in DUB "application" configuration (I use this configuration solely for testing.) Maybe I should put unittest { } into main.d not in the module which I test? Also, what is the correct way to locate the file in the filesystem?
Subtypes with tighter constraints
In Ada2012 there are "subtypes". Subtypes can have tighter constraints (such as type invariants) than their base types. I have a struct X in D. Is it possible to define a type equivalent to X except that having tighter invariants? As I understand if I derive Y from X, then it is no more X; that is I cannot use X (even provided it matches Y invariants) where I need Y. So in D it is impossible, right? I think in D it's impossible, but want to be sure and so ask.