Re: reading from file
On 12/15/2016 10:47 PM, KaattuPoochi wrote: > On Tuesday, 13 December 2016 at 21:13:26 UTC, Ali wrote: >> >> And extending Ali's solution you can actually get the data in >> to a two dimentional array at compile time and have it in static >> memory with a small adjustment: >> >> static immutable matrix = import("data.txt") >> .split("\n") >> .map!(a => a.split(",").map!(to!int).array) >> .array; >> >> void main() { >> writeln(matrix); >> } > > 1. For any non-trivial matrices (with 500 lines) runs DMD 2.072.1 out of > memory (2GB). Not sure if this is a known bug. Works fine with LDC 1.0.0. Compile time features are awesome but currenty very inefficient. :) > 2. The EOL on the last line results in an empty row in the end. Is there > a way to overcome this? If appropriate, you can filter out all empty lines. Added .map!strip and .filter: import std.stdio; import std.algorithm; import std.array; import std.conv; import std.string; void main() { auto a = File("deneme.txt") .byLine .map!strip .filter!(line => !line.empty) .map!(line => line.splitter(',').map!(to!int).array) .array; writeln(a); } Ali
Re: reading from file
On Tuesday, 13 December 2016 at 21:13:26 UTC, Ali wrote: And extending Ali's solution you can actually get the data in to a two dimentional array at compile time and have it in static memory with a small adjustment: static immutable matrix = import("data.txt") .split("\n") .map!(a => a.split(",").map!(to!int).array) .array; void main() { writeln(matrix); } 1. For any non-trivial matrices (with 500 lines) runs DMD 2.072.1 out of memory (2GB). Not sure if this is a known bug. Works fine with LDC 1.0.0. 2. The EOL on the last line results in an empty row in the end. Is there a way to overcome this?
Re: Accessing members through pointers to structs (also, CTFE associative arrays)
On 12/15/2016 05:30 PM, Stefan Koch wrote: On Thursday, 15 December 2016 at 19:30:08 UTC, Ali Çehreli wrote: Yeah, I think the compiler is confused because the function is called in a non-const context during the initialization of an immutable object. I would open an issue: https://issues.dlang.org/enter_bug.cgi?product=D Ali You cannot Assign Associative Arrays at compile-time. Because those are defined by druntime have no stable ABI. Thanks Stefan but at least there should be a better diagnostic instead of the confusing current situation that Ali experienced. Ali
Re: Accessing members through pointers to structs (also, CTFE associative arrays)
On Thursday, 15 December 2016 at 19:30:08 UTC, Ali Çehreli wrote: Yeah, I think the compiler is confused because the function is called in a non-const context during the initialization of an immutable object. I would open an issue: https://issues.dlang.org/enter_bug.cgi?product=D Ali You cannot Assign Associative Arrays at compile-time. Because those are defined by druntime have no stable ABI.
Re: Simplest way to build a DMD compatible C lib, and how to link using DUB.
On Thursday, 15 December 2016 at 20:34:47 UTC, hardreset wrote: On Thursday, 15 December 2016 at 18:30:14 UTC, hardreset wrote: I have pragma(lib,**fullpath**) in my freetype.d file, is that the correct way? Never mind, figured it out, I needer to add "libs": ["libs/freetype27ST"] to dub.json The pragma alone should have been enough. Did you get the path right? At any rate, if you're adding that line to dub.json, then the pragma is redundant. They both do the same thing.
Re: Allocating a class within another class during object init w/o passing in an allocator
On Thursday, 15 December 2016 at 21:37:34 UTC, David Zhang wrote: So the size of Foo would be the size of SomeClass plus members? ie. Is the size of the array stored too? With these definitions: class SomeClass {} class Foo { this() { import std.conv: emplace; emplace!SomeClass(scStorage); } @property SomeClass sc() { return cast(SomeClass) scStorage.ptr; } void[__traits(classInstanceSize, SomeClass)] scStorage; } the "instance size" of Foo is the size of a) Foo's hidden/implicit fields which all classes have, plus b) the size of scStorage which is Foo's only explicit field. The size of scStorage is the instance size of SomeClass (just the implicit fields here). The size/length of scStorage is known at compile-time. It's not stored in Foo.
Re: Allocating a class within another class during object init w/o passing in an allocator
On Thursday, 15 December 2016 at 21:08:51 UTC, ag0aep6g wrote: On 12/15/2016 09:51 PM, David Zhang wrote: However, it leaves me with another question, how much (if any) space would the static array require from the class? Depends on SomeClass. The array's size is just the value of __traits(classInstanceSize, SomeClass). There's no overhead. You can print such stuff at compile time with pragma(msg, ...): pragma(msg, __traits(classInstanceSize, SomeClass)); pragma(msg, Foo.scStorage.sizeof); /* same */ So the size of Foo would be the size of SomeClass plus members? ie. Is the size of the array stored too?
Re: DRY version of `static if(__traits(compiles, expr)) fun(expr)`
On 15.12.2016 01:38, Basile B. wrote: On Wednesday, 14 December 2016 at 22:06:35 UTC, Ali Çehreli wrote: On 12/14/2016 09:25 AM, Basile B. wrote: > On Tuesday, 13 December 2016 at 23:37:59 UTC, Timon Gehr wrote: >> I usually do >> >> enum code = q{expr}; >> static if(__traits(compiles,mixin(code))) >> fun(mixin(code)); > > Strangely if i put this in a templated enum that doesn't work. > If instead i use a delegate literal it works. > > enum Compiles(string code) = is(typeof((){mixin(code);})); > > enum Compiles2(string code) = __traits(compiles, mixin(code)); When you do that, the code does not match the syntax of __traits(compiles). Putting the code inside a scope works at least in this case: enum Compiles2(string code) = __traits(compiles, mixin('{' ~ code ~ '}')); Ali I see, it makes sense. Anyway the two templates have a common problem (protection attributes: data used in the code must be visible to the outside), so using them as a shortcut is a false good idea. Shortcut that works: enum ifCompiles(string code)=`static if(__traits(compiles,{ `~code~` })){ `~code~` }`; mixin(ifCompiles!q{ ... });
Re: Allocating a class within another class during object init w/o passing in an allocator
Thank you for your responses. Visitor, I don't want any reference to an allocator within the class if I can avoid it. ag0aep6g, thanks! That's what I was looking for. However, it leaves me with another question, how much (if any) space would the static array require from the class? It's not a strict necessity for me, but I'm curious.
Re: Simplest way to build a DMD compatible C lib, and how to link using DUB.
On Thursday, 15 December 2016 at 18:30:14 UTC, hardreset wrote: On Thursday, 15 December 2016 at 03:47:27 UTC, Mike Parker wrote: [1] https://github.com/DerelictOrg/DerelictFT Thanks, I'm trying the "-m32mscoff" method for now, but I get "error LNK2019: unresolved external symbol _FT_Init_FreeType referenced" I have pragma(lib,**fullpath**) in my freetype.d file, is that the correct way? Never mind, figured it out, I needer to add "libs": ["libs/freetype27ST"] to dub.json
Re: Sanitizing forms in vibe.d. How?
On Monday, 12 December 2016 at 11:32:42 UTC, Nicholas Wilson wrote: for strip_tags I would look for an xml library (e.g. arsd.dom) and parse it and then reprint it without the tags. There's probably a better way to do it though. I'm sure Adam Ruppe will be able to help you there. Well, it depends what you are doing with it. If you are just outputting user data, I wouldn't allow any HTML at all... but I'd do it by encoding it all. So if they write
Re: Sanitizing forms in vibe.d. How?
On Monday, 12 December 2016 at 10:25:05 UTC, aberba wrote: How about alternative to php strip_tags(), strip_slash() ? I wouldn't use those functions anyway in most cases: instead of stripping stuff, just encode it properly for the output. So, if it is being output to JSON or javascript, json encode it. If it is going to HTML, html encode it. If a URL, url encode it. If to a database, use a prepared statement. You may need to use multiple layers. A link may be both URL and HTML encoded, because first it is a url, then it is being added to a html document so it needs that too. I don't know the vibe library, but my dom.d has a bunch of options for html encode.
Re: Accessing members through pointers to structs (also, CTFE associative arrays)
On 12/14/2016 04:02 AM, Ali wrote: > On Tuesday, 13 December 2016 at 23:29:31 UTC, Ali Çehreli wrote: >> On 12/13/2016 01:36 PM, Ali wrote: >> >>> Now about that second part of my problem >> >> I'm not entirely sure whether this should work but I think the problem >> is with mutating the 'frequencies' member of an immutable element of >> 'rooms'. The error message means that those non-const expressions >> cannot be shared by a member of an immutable AA. > > I'm not sure I fully follow here. Because the variable in the parse > function is not immutable and the frequencies member is not "created" > yet, so to say. So after I create the frequencies object, I assign it to > the member of a Room object. The following illustrates this more clearly > I think: > > struct A { int x; } > struct B { int[char] x; } > auto f1() { > int x; > x = 3; > return A(x); > } > > auto f2() { > int[char] x; > x['A'] = 2; > return B(x); > } > > static immutable a = f1(); > static immutable b = f2(); > > pragma(msg, a); > pragma(msg, b); > > This fails to compile with "Error: non-constant expression ['A':2]". > But, the pragma(msg) prints out the correct information for both a and b. Yeah, I think the compiler is confused because the function is called in a non-const context during the initialization of an immutable object. I would open an issue: https://issues.dlang.org/enter_bug.cgi?product=D Ali
Re: Allocating a class within another class during object init w/o passing in an allocator
On 12/15/2016 06:44 PM, David Zhang wrote: It is my understanding that a class can have a struct as one of its members, and it will be allocated in-line with the rest of the class' members. Yup. My question is this; how might I be able to do this with another class? I want to be able to allocate Foo using std.experimental.allocator without having to pass in a reference to the actual allocator. Add a fixed-size array with an appropiate size to the class, and use std.conv.emplace to construct the object there: class SomeClass {} class Foo { this() { import std.conv: emplace; sc = emplace!SomeClass(scStorage); } SomeClass sc; void[__traits(classInstanceSize, SomeClass)] scStorage; } void main() { import std.experimental.allocator: make; import std.experimental.allocator.mallocator: Mallocator; auto foo = Mallocator.instance.make!Foo; } I haven't considered alignment here. I'm not sure if you have to. The GC will collect the sc object along with the parent Foo. Be cautious of that when you allow public access to sc. You can also replace the sc field with a method that creates the class reference on the fly (it's just a cast): class Foo { this() { import std.conv: emplace; emplace!SomeClass(scStorage); } @property SomeClass sc() { return cast(SomeClass) scStorage.ptr; } void[__traits(classInstanceSize, SomeClass)] scStorage; }
Re: Allocating a class within another class during object init w/o passing in an allocator
On Thursday, 15 December 2016 at 17:44:23 UTC, David Zhang wrote: would something like this be a solution ? import std.stdio; import std.experimental.allocator; class SomeClass { int someint = 42; static SomeClass opCall(int a) { auto inst = theAllocator.make!SomeClass; inst.someint += a; return inst; } void destruct() { theAllocator.dispose(this); } } class Foo { this() { sc = SomeClass(7); sc.someint -= 42; } ~this() { sc.destruct(); } size_t something; SomeClass sc; } void main(string[] args) { auto foo = theAllocator.make!Foo; assert(foo.sc.someint == 7); theAllocator.dispose(foo); }
Re: Simplest way to build a DMD compatible C lib, and how to link using DUB.
On Thursday, 15 December 2016 at 03:47:27 UTC, Mike Parker wrote: On Wednesday, 14 December 2016 at 23:08:30 UTC, hardreset wrote: As Basile recommended, DerelictFT[1] will save you from the hassle of object formats. It's a dynamic binding, so you don't need to link with FreeType at all during compilation. You simply call DerelictFT.load during initialization and it will load the FreeType DLL for you. However, if your goal is to use DLLs, then you either have to use the MS linker as I described above or get FreeType into the OMF format (either by compiling with DMC or using an object converter). [1] https://github.com/DerelictOrg/DerelictFT Thanks, I'm trying the "-m32mscoff" method for now, but I get "error LNK2019: unresolved external symbol _FT_Init_FreeType referenced" I have pragma(lib,**fullpath**) in my freetype.d file, is that the correct way?
Re: arsd.cgi - maximum length of form post
On Thursday, 15 December 2016 at 16:52:54 UTC, Adam D. Ruppe wrote: On Tuesday, 13 December 2016 at 22:55:55 UTC, bachmeier wrote: Here is a minimal program that can replicate the problem. Compiled and run with OK, try the new git cgi.d version, looks like my popFront was buggy and some data got misplaced over multiple chunks (so if the content was less than one chunk length, it worked, but more than that it broke) That fixed it. Thanks! I use it to capture notes inside a Git repo as I work. My posts are normally short, but in this case I did copy and paste with a lengthy email, and that was apparently enough to trigger the bug.
Allocating a class within another class during object init w/o passing in an allocator
Hello, It is my understanding that a class can have a struct as one of its members, and it will be allocated in-line with the rest of the class' members. My question is this; how might I be able to do this with another class? I want to be able to allocate Foo using std.experimental.allocator without having to pass in a reference to the actual allocator. Thanks. eg: class SomeClass {} class Foo { this(IAllocator alloc) { sc = alloc.make!SomeClass; this.alloc = alloc } ~this() { alloc.dispose(sc); } size_t something; SomeClass sc; IAllocator alloc } auto foo = alloc.make!Foo(alloc); vs. struct SomeStruct {} class Foo { this() { ss = SomeStruct (...); } size_t something; SomeStruct ss; } auto foo = alloc.make!Foo;
Re: arsd.cgi - maximum length of form post
On Tuesday, 13 December 2016 at 22:55:55 UTC, bachmeier wrote: Here is a minimal program that can replicate the problem. Compiled and run with OK, try the new git cgi.d version, looks like my popFront was buggy and some data got misplaced over multiple chunks (so if the content was less than one chunk length, it worked, but more than that it broke)
Re: this is not an lvalue
On Thursday, 15 December 2016 at 15:29:13 UTC, Adam D. Ruppe wrote: General rule of thumb: if you are refing for performance, actually check it before and after first, ref isn't always faster. But D doesn't make this easy, as it disallows rvalues to be passed by ref. It's a very common pitfall for C++ guys. `auto ref` for templates is an insufficient workaround/hack for that. On a side note, wrt. `ref isn't always faster` (due to the indirection): it's almost always faster on Windows 64, as all value types > 64 bit (excluding vectors for the vectorcall convention, but let's keep things simple) need to be passed by reference (ABI requirement). What this means and what keeps on being ignored is that passing a D value type > 64 bits by value (to allow rvalue args, although the parameter is a read-only one) yields the worst of both worlds - a bitcopy of the original argument passed by reference.
Re: arsd.cgi - maximum length of form post
Well, I can reproduce the error now, the buffer it is getting is too long for some reason. Probably a slicing error that doesn't show up with smaller payloads. I should have a fix today though. BTW, interestingly, the more complex codepath for uploads does work fine (add enctype="multipart/form-data" to your HTML form to trigger it, you don't actually have to do an upload, that attr is enough) so you can do that as a work around in the mean time. I prolly just actually tested large uploads and never did large normal submissions with the embedded server.
Re: this is not an lvalue
On Thursday, 15 December 2016 at 14:05:08 UTC, Andrey wrote: In D, probably, I could write something like this: void open(in string fileName) {...} Yes, though remember that `in` does have a specific meaning (even though the compiler rarely enforces it): it means you promise not to modify it nor keep a reference to it. So don't use `in` on anything you want to keep as a member or global variable, it is something you will just look at inside this function then let go.
Re: this is not an lvalue
On Thursday, 15 December 2016 at 13:59:05 UTC, Andrey wrote: Thanks it works, but where should I use the ref? Only when you need it, break the habit of using it everywhere. If it is a value type and you want modifications to the variable itself be seen outside the function, use it: void foo(ref int something) { something++; } void main() { int a = 1; foo(a); assert(a == 2); } Since int is value and you want to see the change to the local outside the function, ref is used. However, notice: void foo(int[] arr) { arr[0] = 5; } void main() { int[2] a = [1, 2]; foo(a[]); assert(a[0] == 5); } No need for ref there, because the slice is already a reference to its contents. However, if I wanted to *append* to the slice and see that length change outside, ref may be appropriate. This brings me to classes: interfaces and class objects in D are already automatically pointers internally, just like slices. void foo(Object o) { /* already like Object* in C++ */ o.modify(); } void main() { Object o = new Object(); foo(o); // o has been modified } No need for ref to see changes to the object's internals outside. However, if you want to rebind the object: void foo(ref Object o) { /* now like Object** in C++ */ o = new Object(); } void main() { Object o; foo(o); // o represents the new Object } ref is needed there - this is changing the variable itself, not what is inside it. With structs, it depends on what you are doing with it - are you changing the variable itself or something inside it? And what is inside. struct Foo { int a; } That Foo acts just like int, so use or don't use ref as if it was an int. struct Foo { int[] a; } That one is just like a slice, no need to `ref` that unless you want to change the outer portion. The contents are implicitly referenced and not copied. struct Foo { int[4] a; } Well, this is a value type again, but bigger. Still, I'd say follow the same rule as plain `int` - only ref that if you need modifications to be seen outside the function. struct Foo { ubyte[1024 * 1024] oneMegabyte; } OK, you might want to ref that one just because the copy to the function may be expensive and the compiler is not necessarily going to optimize that (BUT the compiler MIGHT optimize it! If the function gets inlined and it isn't modified, it may compile to the same thing anyway. Still, I'd probably ref that myself, or wrap it up in pointers or maybe even a class for its interface.) General rule of thumb: if you are refing for performance, actually check it before and after first, ref isn't always faster.
Re: Alias variable from another class
On Tuesday, 13 December 2016 at 19:13:24 UTC, Begah wrote: Any ideas? Closest you can get is wrapping it in a property. If you need to do this often you may be able to generate them, check the recent "Getters/Setters generator" thread in Announce for some inspiration.
Re: this is not an lvalue
On Tuesday, 13 December 2016 at 16:23:16 UTC, Adam D. Ruppe wrote: On Tuesday, 13 December 2016 at 15:09:10 UTC, Andrey wrote: void moveTo(ref Parameter parent) { You probably don't want the `ref` there, nor likely on any other method definitions (especially check removeValue). In D, probably, I could write something like this: void open(in string fileName) {...}
Re: this is not an lvalue
On Tuesday, 13 December 2016 at 16:23:16 UTC, Adam D. Ruppe wrote: On Tuesday, 13 December 2016 at 15:09:10 UTC, Andrey wrote: void moveTo(ref Parameter parent) { You probably don't want the `ref` there, nor likely on any other method definitions (especially check removeValue). Thanks it works, but where should I use the ref? for example in C++ I always used pointers for structs, classes in stack etc.: void open(const std::string ) {...}
Re: Dynamically Loading a D DLL From a D Program
On Wednesday, 14 December 2016 at 21:38:27 UTC, Benjiro wrote: It also seems that the core runtime is incomplete with basic loading but no handling of dlsym, so your still forced to use the basic c conversion casting. int function() fn = cast(int function())dlsym(lib, libFunction); fn(); You can use the derelict-util library to load a dynlib portably. https://github.com/DerelictOrg/DerelictUtil/blob/2de2a9b63f8cdfbd8270afa8a4dc28f4ffaa868c/source/derelict/util/sharedlib.d#L110
Re: Get fils at compile-time
On Thursday, 15 December 2016 at 07:56:40 UTC, Jacob Carlborg wrote: On 2016-12-14 21:47, bauss (wtf happend to my name took some old cached title LOL??) wrote: [...] I would recommend creating a small script that iterates a directory and generates a D file with string imports for all the files, something like: [...] Yeah it was something like that I thought I might had to do. Shame that you can't just do it through D itself. Thanks.
Re: Issue with dmd, linker, visualD on windows 10
On Wednesday, 14 December 2016 at 11:06:10 UTC, aberba wrote: I am trying to get a fellow to try D but just setting up on windows 10 has been headache. He's currently remote. Here's the problem. (Note I'm a Linux user and haven't used windows 10) 1. He installed dmd 2 but the command "dmd" is not recognized. He confirmed and c:\D\dmd2\windows\bin is in system path. Why? 2. He installed visual studio 2015 and visualD(pointed it to dmd location during installation), restarted visual studio. Without creating a visualD project, "compile and run" does nothing when he clicks. We haven't tried creating a project though, just using a D file with simple "Hello, world!" code. Syntax highlighting work though. 3. He navigated to ...dmd2\windows\bin where "dmd" command works. But "dmd -run file.d" says Oplink error, linker exited with code ... I had similar problems with Win7 and the same setup, for some reason dmd's sc.ini wasn't being parsed, making the linker fail to find the required libraries. I solved it by adding the library paths (3 in total) manually to VisualD's options.
Re: Get fils at compile-time
On 2016-12-14 21:47, bauss (wtf happend to my name took some old cached title LOL??) wrote: Is there a way to get all files in a folder at compile-time. To be more specific I want to import the content of all files within a specific folder during compile-time. I know how to do it with a specific file using `import("filename")`, but what if I wanted to do it at compile-time for all files in a folder then loop through those files and handle their content. To give an example. Let's say I have a folder "styles" it has 3 files "global.stylesheet", "main.stylesheet", "shared.stylesheet". Now I don't know about these file's names, so I can't just do it manually as they could be "foo", "bar" and "baz", just like they're "global", "main" and "shared". I would recommend creating a small script that iterates a directory and generates a D file with string imports for all the files, something like: module foo; immutable files = [import("global.stylesheet"), import("main.stylesheet"), import("shared.stylesheet")]; If you need the names of the files as well you could generate an associative array instead. The name of the D module and the variable in the module would always be the same to be able to reference it from other modules. If your using Dub you can run the script automatically before compilation using the "preGenerateCommands" build setting. You can write the script in D and invoke it using "dmd -run generate_files.d". Remember to add -J with the path to the folder to your build script, or use the "importPaths" build setting if you're using Dub. -- /Jacob Carlborg