Emacs AutoComplete Tutorial Video for D Language
I made this video for people asking how to configure Dlang in Emacs environment:) : https://peertube.linuxrocks.online/w/62pWpmw2r4Se1XvmYiWm75
Re: Error with associative array initializer DMD32 D Compiler v2.070.0
On Thursday, 3 March 2016 at 10:35:50 UTC, MGW wrote: The citation from https://dlang.org/spec/hash-map.html Static Initialization of AAs immutable long[string] aa = [ "foo": 5, "bar": 10, "baz": 2000 ]; unittest { assert(aa["foo"] == 5); assert(aa["bar"] == 10); assert(aa["baz"] == 2000); } Judging by the text, it is static initialization, during compilation. You have to put that definition inside body of a function (a declaration). it should not be placed inside module's root level code.
Re: Why we cannot use string in mixins?
Thanks. So the author was plain wrong about using enums instead of strings. The misconception is due to assuming we can use `string` variables at compile time but we cannot (as they are run-time data).
Why we cannot use string in mixins?
I read this criticism about D on Reddit and it claims that you cannot use strings in mixins. Can you please elaborate about this and the reason behind it? QUOTE: Look at strings: they are defined as immutable(char []). "immutable" as in "you could put it in ROM", ... CTFE can't see strings correctly (can't be determined at compile time). So rather than fix the compiler so that strings can be used in mixins in the way that people expect it to work even if that means making them something other than immutable(char []), they decided to start using enums where strings actually go. Seriously: take the same code and replace 'string "foo"' with 'enum "foo"' and it starts to work! END QUOTE
Re: dub: how to reference a compiled package
On Friday, 26 February 2016 at 02:49:20 UTC, Mike Parker wrote: On Thursday, 25 February 2016 at 21:06:59 UTC, mahdi wrote: On Thursday, 25 February 2016 at 16:45:46 UTC, Chris Wright Thanks. Is there a way to use a D library without having access to it's source code? I tried `dmd -lib abcd.d` which creates a static library. But still I need to specify path to library's source files using -I option when compiling the code that uses that library. So if we have just access to the library file, is it possible to use it in the code? The compiler needs to know what symbols are available from any imports you use in your source. .di files exist to allow closed source projects to be distributed as binary. They are analagous to C or C++ header files. You could create them by hand like so: // foo.d struct S { int x, y; } void addTwo(S s) { s.x += 2; s.y += 2; } // foo.di struct S { int x, y; } void addTwo(S s); The compiler needs to know about S and its types, and it needs to know the signature of addTwo. The .di file allows you to provide that while keeping the implementation of addTwo closed. When foo is imported in client code, the compiler will find foo.di and use that instead of foo.d. However, the compiler must have the source for templates, as they are instantiated when they are used, not when the library is compiled. The same is true for any functions you want inlined. In the example above, addTwo can only be inlined when foo.d is used, since the compiler will not have the implementation with foo.di. Great! Thanks. I was looking for a feature like `jar` files in Java or `assemblies` in C# where all compiled code and metadata/symbols are stored together inside a single binary file. I think same can be implemented for D language and it won't break any code because it is not touching the language itself, but the compiler. Anyway, thanks.
Re: dub: how to reference a compiled package
On Thursday, 25 February 2016 at 16:45:46 UTC, Chris Wright wrote: On Thu, 25 Feb 2016 12:15:42 +, mahdi wrote: Hi, Suppose I have a package `mypack` in `~/mypack`. I run `dub` command on this package and have the compiled `mypack` file (OS is Linux). Now I am working on my project. I know how to use the source-code of `mypack` package in the project but what if I only have the compiled binary? How can I reference and use the modules of the compiled `mypack`? (I looked into the DUB homepage and it's Getting Started page but could not find anything). First thing, you need D interface files. See: https://dlang.org/dmd-linux.html#interface-files Note that D interface files still require you to include the full body of anything in a template, so you might have to modify your code somewhat for improved secrecy. Copy the *.di files and binaries into another dub project and add a postBuildCommand to copy that binary to the output location. I think that will work. dub might complain that it doesn't have any source files, in which case you'll have to provide at least one D file for it to compile, but that can be empty. Thanks. Is there a way to use a D library without having access to it's source code? I tried `dmd -lib abcd.d` which creates a static library. But still I need to specify path to library's source files using -I option when compiling the code that uses that library. So if we have just access to the library file, is it possible to use it in the code?
Re: How to detect if an array if dynamic or static
On Thursday, 25 February 2016 at 11:50:02 UTC, sigod wrote: On Thursday, 25 February 2016 at 10:03:08 UTC, mahdi wrote: Thanks. So when we define the function, we MUST specify the array size to be able to accept a static array? Can't we just define a function which can accept any static array with any size? (e.g. a function to calculate average of a static int array of any size)? Static array can be accepted in place of dynamic: void foo() { int[3] arr = [1, 2, 3]; bar(arr); } void bar(scope int[] arr) { import std.stdio : writeln; writeln(arr); // [1, 2, 3] } But be careful not to escape such variables. Demonstration: http://dpaste.dzfl.pl/613e04d4fe3f My question: If in your `bar` function, the code tries to add a new element to the dynamic array, it will be completely ok because array is dynamic. BUT if we pass a static array to this function, can this error be detected at compile time (and prevent a runtime error)? If so, how?
dub: how to reference a compiled package
Hi, Suppose I have a package `mypack` in `~/mypack`. I run `dub` command on this package and have the compiled `mypack` file (OS is Linux). Now I am working on my project. I know how to use the source-code of `mypack` package in the project but what if I only have the compiled binary? How can I reference and use the modules of the compiled `mypack`? (I looked into the DUB homepage and it's Getting Started page but could not find anything).
How to detect if an array if dynamic or static
Thanks. So when we define the function, we MUST specify the array size to be able to accept a static array? Can't we just define a function which can accept any static array with any size? (e.g. a function to calculate average of a static int array of any size)?
Re: How to detect if an array if dynamic or static
On Wednesday, 24 February 2016 at 22:38:04 UTC, Adam D. Ruppe wrote: On Wednesday, 24 February 2016 at 21:48:14 UTC, mahdi wrote: How can we detect is `array` is static (fixed size) or dynamic, inside the function body? `array` there is always dynamic because it is not of a fixed size type. Why do you want to know though? I thought we can simply denote `int[] x` in case we have an array argument in D functions. So according to your answer if a function expects a static array, it has to specify size of array in parameter declaration: void diss(int[3] array) ... //this expects a static array of size 3 void diss(int[] array) ... //this expects a dynamic array is this correct?
How to detect if an array if dynamic or static
Suppose we have a function like this: void diss(int[] array) ... How can we detect is `array` is static (fixed size) or dynamic, inside the function body?
Can D "prevents segfaults, and guarantees thread safety"?
A selling point of Rust language is that it "prevents segfaults, and guarantees thread safety". Is there a library in D language which provides same features?