Re: Function Arguments with Multiple Types

2019-09-06 Thread Ali Çehreli via Digitalmars-d-learn
On 09/06/2019 01:46 PM, Bob4 wrote: > Thanks; this works, but I'm not sure why. Where does `T` come from? Well... I assumed this would make you research templates. ;) Here is one resource: http://ddili.org/ders/d.en/templates.html (T) means "this function is for any type; and I call that

Re: Function Arguments with Multiple Types

2019-09-06 Thread Bob4 via Digitalmars-d-learn
On Friday, 6 September 2019 at 20:16:58 UTC, Ali Çehreli wrote: On 09/06/2019 01:02 PM, Bob4 wrote: > I feel like it's wrong to rewrite identical functions over and over. Enter templates. :) auto clamp(T)(T value, T mini, T maxi) { if (value >= maxi) { return maxi; } if (value <=

Re: Function Arguments with Multiple Types

2019-09-06 Thread Ali Çehreli via Digitalmars-d-learn
On 09/06/2019 01:02 PM, Bob4 wrote: > I feel like it's wrong to rewrite identical functions over and over. Enter templates. :) auto clamp(T)(T value, T mini, T maxi) { if (value >= maxi) { return maxi; } if (value <= mini) { return mini; } return value; } unittest {

Function Arguments with Multiple Types

2019-09-06 Thread Bob4 via Digitalmars-d-learn
Hi, I'm coming from a background in Python, without a lot of experience in statically typed languages, and I'm struggling to see how to make a certain function in D. This is what I have in Python: ``` from typing import Union Number = Union[int, float] def clamp(value: Number, mini:

Re: C++ vs D: Default param values and struct to array casting

2019-09-06 Thread Paul Backus via Digitalmars-d-learn
On Friday, 6 September 2019 at 18:39:47 UTC, Andrew Edwards wrote: also probably u can do https://run.dlang.io/is/WMQE93 Ended up using this since it provides for named access and solves the overloading requirement. Thanks, Andrew You can also use std.typecons.Tuple for this, since it

Re: Why are extern(C/C++) definitions and references mangled differently in separately compiled modules?

2019-09-06 Thread Max Samukha via Digitalmars-d-learn
On Friday, 6 September 2019 at 17:54:51 UTC, Adam D. Ruppe wrote: On Friday, 6 September 2019 at 17:42:08 UTC, Max Samukha wrote: That file was silently imported by the compiler (probably, a bug). That's by design - the automatic module import lookups actually always look for .di file first,

Re: C++ vs D: Default param values and struct to array casting

2019-09-06 Thread Andrew Edwards via Digitalmars-d-learn
On Friday, 6 September 2019 at 11:35:59 UTC, a11e99z wrote: https://dlang.org/spec/simd.html This didn't work two well because I wont be able to access the members by name as C++ library expects. Will consider during refactoring. also probably u can do https://run.dlang.io/is/WMQE93

Re: C++ vs D: Default param values and struct to array casting

2019-09-06 Thread Ali Çehreli via Digitalmars-d-learn
On 09/06/2019 02:14 AM, Andrew Edwards wrote: > I'm seeking some pointers on how to define these in D Here is my attempt: struct Demo { // NOTE: The types and number of elements can be templatized and mixed-in like // mixin (makeMembers!T(N)); float a = 0; float b = 0; float c =

Re: Why are extern(C/C++) definitions and references mangled differently in separately compiled modules?

2019-09-06 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 6 September 2019 at 17:42:08 UTC, Max Samukha wrote: That file was silently imported by the compiler (probably, a bug). That's by design - the automatic module import lookups actually always look for .di file first, then .d files.

Re: Why are extern(C/C++) definitions and references mangled differently in separately compiled modules?

2019-09-06 Thread Max Samukha via Digitalmars-d-learn
On Friday, 6 September 2019 at 16:55:31 UTC, Max Samukha wrote: On Friday, 6 September 2019 at 15:52:46 UTC, Stefan Koch wrote: If that is happening you hit a bug. It seems unlikely though. Could you elaborate a bit? How should extern(C/C++) definitions be mangled - fully qualified or not,

Re: Why are extern(C/C++) definitions and references mangled differently in separately compiled modules?

2019-09-06 Thread Max Samukha via Digitalmars-d-learn
On Friday, 6 September 2019 at 15:52:46 UTC, Stefan Koch wrote: On Friday, 6 September 2019 at 15:09:22 UTC, Max Samukha wrote: Consider the following two modules: 1. test.d: module test; import lib.a; void main() { foo(); } 2. lib/a.d: module lib.a; extern(C) void foo() {} When

Re: deep copying a struct

2019-09-06 Thread Paul Backus via Digitalmars-d-learn
On Friday, 6 September 2019 at 10:52:43 UTC, aliak wrote: On Friday, 6 September 2019 at 10:37:16 UTC, aliak wrote: Are there any library APIs that allow this: I just put this together. Any holes other the AA related ones? string a = "hello"; string b = a.dupDeep; // Error: cannot

Re: C++ vs D: Default param values and struct to array casting

2019-09-06 Thread Paul Backus via Digitalmars-d-learn
On Friday, 6 September 2019 at 09:28:57 UTC, Andrew Edwards wrote: This is my thought on how to accomplish op overloading: struct Test { float a, b, c, d; float opIndex(size_t i) in(i >= 0 && i <= 3) { final switch(i) { case 0: return a;

Re: Why are extern(C/C++) definitions and references mangled differently in separately compiled modules?

2019-09-06 Thread Max Samukha via Digitalmars-d-learn
On Friday, 6 September 2019 at 15:32:07 UTC, 0xEAB wrote: On Friday, 6 September 2019 at 15:09:22 UTC, Max Samukha wrote: Consider the following two modules: What compiler version are you using? DMD64 D Compiler v2.088.0-1-g4011382ea, linux

Re: Why are extern(C/C++) definitions and references mangled differently in separately compiled modules?

2019-09-06 Thread Stefan Koch via Digitalmars-d-learn
On Friday, 6 September 2019 at 15:09:22 UTC, Max Samukha wrote: Consider the following two modules: 1. test.d: module test; import lib.a; void main() { foo(); } 2. lib/a.d: module lib.a; extern(C) void foo() {} When compiled separately (dmd -c lib/a.d; dmd test.d a.o), the

Re: Why are extern(C/C++) definitions and references mangled differently in separately compiled modules?

2019-09-06 Thread 0xEAB via Digitalmars-d-learn
On Friday, 6 September 2019 at 15:09:22 UTC, Max Samukha wrote: Consider the following two modules: What compiler version are you using?

Why are extern(C/C++) definitions and references mangled differently in separately compiled modules?

2019-09-06 Thread Max Samukha via Digitalmars-d-learn
Consider the following two modules: 1. test.d: module test; import lib.a; void main() { foo(); } 2. lib/a.d: module lib.a; extern(C) void foo() {} When compiled separately (dmd -c lib/a.d; dmd test.d a.o), the function in 'a.o' is mangled as 'foo', but the reference in 'test.o' is

Why are constructor definitions are preserved in interface files?

2019-09-06 Thread Max Samukha via Digitalmars-d-learn
module lib.a; class C { this() { } void foo() { } ~this() { } } dmd -H -o- a.d: // D import file generated from 'a.d' module lib.a; class C { this() { } void foo(); ~this(); } The destructor and

Re: C++ vs D: Default param values and struct to array casting

2019-09-06 Thread a11e99z via Digitalmars-d-learn
On Friday, 6 September 2019 at 09:14:31 UTC, Andrew Edwards wrote: C++ allows the for following: struct Demo { float a, b, c, d; Demo() { a = b = c = d = 0.0f; } Demo(float _a, float _b, float _c, float _d) { a = _a; b = _b;

Re: deep copying a struct

2019-09-06 Thread aliak via Digitalmars-d-learn
On Friday, 6 September 2019 at 10:37:16 UTC, aliak wrote: Are there any library APIs that allow this: I just put this together. Any holes other the AA related ones? Will it work with classes? auto dupDeep(T)(ref T thing) { import std.range: ElementType; import std.traits:

deep copying a struct

2019-09-06 Thread aliak via Digitalmars-d-learn
Are there any library APIs that allow this: struct S { int[] arr; } void main() { const a = S([1,2,3]); S b = a.copy; // deep dup/copy } I'd also like to avoid implementing a copy constructor for each type.

Re: C++ vs D: Default param values and struct to array casting

2019-09-06 Thread Andrew Edwards via Digitalmars-d-learn
On Friday, 6 September 2019 at 09:49:33 UTC, Johan Engelen wrote: On Friday, 6 September 2019 at 09:14:31 UTC, Andrew Edwards wrote: C++ allows the for following: struct Demo { float a, b, c, d; Demo() { a = b = c = d = 0.0f; } Demo(float _a, float _b, float _c, float

Re: C++ vs D: Default param values and struct to array casting

2019-09-06 Thread Johan Engelen via Digitalmars-d-learn
On Friday, 6 September 2019 at 09:14:31 UTC, Andrew Edwards wrote: C++ allows the for following: struct Demo { float a, b, c, d; Demo() { a = b = c = d = 0.0f; } Demo(float _a, float _b, float _c, float _d) { a = _a; b = _b;

Re: C++ vs D: Default param values and struct to array casting

2019-09-06 Thread Andrew Edwards via Digitalmars-d-learn
On Friday, 6 September 2019 at 09:14:31 UTC, Andrew Edwards wrote: C++ allows the for following: struct Demo { float a, b, c, d; Demo() { a = b = c = d = 0.0f; } Demo(float _a, float _b, float _c, float _d) { a = _a; b = _b;

Re: getting rid of immutable (or const)

2019-09-06 Thread berni via Digitalmars-d-learn
On Friday, 6 September 2019 at 08:47:07 UTC, Kagamin wrote: Physical objects work like reference types. A place on bookshelf is at one coordinate and a book is at another coordinate, you don't copy the book, you fill a place on bookshelf with a reference to the book. So it's more like a

Re: getting rid of immutable (or const)

2019-09-06 Thread berni via Digitalmars-d-learn
On Thursday, 5 September 2019 at 21:22:12 UTC, Ali Çehreli wrote: If it makes for the type to have immutable (or const) members, then fine; with the understanding that objects of that type cannot be assigned or mutated any other way, we can define them like that. What I meant is, because we

C++ vs D: Default param values and struct to array casting

2019-09-06 Thread Andrew Edwards via Digitalmars-d-learn
C++ allows the for following: struct Demo { float a, b, c, d; Demo() { a = b = c = d = 0.0f; } Demo(float _a, float _b, float _c, float _d) { a = _a; b = _b; c = _c; d = _d; } float

Re: getting rid of immutable (or const)

2019-09-06 Thread berni via Digitalmars-d-learn
On Thursday, 5 September 2019 at 20:10:03 UTC, ag0aep6g wrote: You're not putting an immutable int into an AA. You're copying the value of an immutable int to a mutable one. but I can't do that with a struct, having an immutable member. When I remove that immutable inside of the struct it

Re: getting rid of immutable (or const)

2019-09-06 Thread Kagamin via Digitalmars-d-learn
On Thursday, 5 September 2019 at 12:46:06 UTC, berni wrote: OK. This are two solutions and although I'll probably not going to use any of those (due to other reasons), I still don't understand, why the original approach does not work. If I've got a book an put it in a box and later I'll get it

Re: Linking to -framework on MacOS

2019-09-06 Thread Andrew Edwards via Digitalmars-d-learn
On Thursday, 5 September 2019 at 18:26:41 UTC, DanielG wrote: And depending on the version of macOS / which framework you're linking to, you might need to specify a search path as well (-F): lflags "-framework" "SomeFramework" "-framework" "AnotherFramework" "-F/Library/Frameworks" IIRC I

Re: Linking to -framework on MacOS

2019-09-06 Thread Andrew Edwards via Digitalmars-d-learn
On Thursday, 5 September 2019 at 12:30:33 UTC, Jacob Carlborg wrote: On 2019-09-04 17:12, Andrew Edwards wrote: Worked like a charm:  -L/System/Library/Frameworks/Cocoa.framework/Cocoa This probably not a good idea. It relies on how a framework is structured internally. Adam's solution is

Blog Post #68: MVC - Multi-level TreeView

2019-09-06 Thread Ron Tarrant via Digitalmars-d-learn
Today we dig back into the MVC series to look at the multi-level TreeStore and maybe learn a little geography. You can read all about it right here: https://gtkdcoding.com/2019/09/06/0068-multi-level-treestore.html

Why are constructor definitions preserved in interface files?

2019-09-06 Thread Max Samukha via Digitalmars-d-learn
module lib.a; class C { this() { } void foo() { } ~this() { } } dmd -H -o- a.d: // D import file generated from 'a.d' module lib.a; class C { this() { } void foo(); ~this(); } The destructor and

Re: Old code no longer working on any DMD compilers

2019-09-06 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, September 5, 2019 11:59:30 PM MDT Jamie via Digitalmars-d-learn wrote: > On Friday, 6 September 2019 at 00:41:12 UTC, Jonathan M Davis > > wrote: > > On Thursday, September 5, 2019 6:24:07 PM MDT Jamie via > > > > Digitalmars-d-learn wrote: > >>

Re: Old code no longer working on any DMD compilers

2019-09-06 Thread Mike Parker via Digitalmars-d-learn
On Friday, 6 September 2019 at 05:59:30 UTC, Jamie wrote: time and fmod is called so it breaks. In case 3, with default struct arguments, I thought that the constructor I have defined was being called, however the default constructor was being called (this()) so fmod wasn't being called.

Re: Old code no longer working on any DMD compilers

2019-09-06 Thread Jamie via Digitalmars-d-learn
On Friday, 6 September 2019 at 00:41:12 UTC, Jonathan M Davis wrote: On Thursday, September 5, 2019 6:24:07 PM MDT Jamie via Digitalmars-d-learn wrote: /home/jamie/.dvm/compilers/dmd-2.077.0/linux/bin/../../src/phobos/std/math .d(3702): Error: fmodl cannot be interpreted at compile time,