Re: Use class template as a type

2016-11-28 Thread Nicholas Wilson via Digitalmars-d-learn
On Monday, 28 November 2016 at 11:26:41 UTC, dm wrote: Hi. Is it possible to write in D something like this? ``` abstract class MyClass(T) { public: @property const(T) value(){return _value;} @property void value(T val){_value = val;} ... private: T _value; ... } ... class

Re: Use class template as a type

2016-11-28 Thread rikki cattermole via Digitalmars-d-learn
On 29/11/2016 2:56 AM, dm wrote: On Monday, 28 November 2016 at 11:30:23 UTC, rikki cattermole wrote: In your case I'd just swap out ``MyClass[] someArray;`` to ``Object[] someArray;``. But only because there are no members added without the extra typing in MyClass. Remember types in

Use class template as a type

2016-11-28 Thread dm via Digitalmars-d-learn
Hi. Is it possible to write in D something like this? ``` abstract class MyClass(T) { public: @property const(T) value(){return _value;} @property void value(T val){_value = val;} ... private: T _value; ... } ... class MyClassFloat: MyClass!float ... class MyClassInt: MyClass!int

Re: Use class template as a type

2016-11-28 Thread dm via Digitalmars-d-learn
On Monday, 28 November 2016 at 11:30:23 UTC, rikki cattermole wrote: In your case I'd just swap out ``MyClass[] someArray;`` to ``Object[] someArray;``. But only because there are no members added without the extra typing in MyClass. Remember types in meta-programming in D are not erased,

Re: Use class template as a type

2016-11-28 Thread rikki cattermole via Digitalmars-d-learn
In your case I'd just swap out ``MyClass[] someArray;`` to ``Object[] someArray;``. But only because there are no members added without the extra typing in MyClass. Remember types in meta-programming in D are not erased, they exist in the assembly and are unique. Unlike Java who did the

Re: Use class template as a type

2016-11-28 Thread dm via Digitalmars-d-learn
Thats because MyClass is a template class. Templates are note types, instansiations of templates can be types. e.g. Myclass!float[] arr; // note this is not MyClass!(float[]); will work. As Rikki suggested using Object[] instead will allow use to store classes of different types. Maybe

Re: Use class template as a type

2016-11-28 Thread Namespace via Digitalmars-d-learn
We have a handy dandy syntax for this: if (MyClassInt subclass = cast(MyClassInt)value) { writeln(subclass.value); } If it doesn't cast to said type (it will be null) that branch won't execute. Just out of interest: it looks like a dynamic_cast in C++ which is considered as slow

Re: Use class template as a type

2016-11-28 Thread dm via Digitalmars-d-learn
We have a handy dandy syntax for this: if (MyClassInt subclass = cast(MyClassInt)value) { writeln(subclass.value); } If it doesn't cast to said type (it will be null) that branch won't execute. Hell yeah! It's works! Thank you!

Re: non-constant expression ["foo":5, "bar":10, "baz":2000]

2016-11-28 Thread Era Scarecrow via Digitalmars-d-learn
On Monday, 28 November 2016 at 09:06:34 UTC, Paolo Invernizzi wrote: The point is that I was trying to avoid some cycle between modules, detected by 2.072. This bug leads to pollution in the use of static this only to workaround the limitation... Wasn't someone working on a Associative Array

Re: the best language I have ever met(?)

2016-11-28 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, November 26, 2016 00:43:04 Artur Skawina via Digitalmars-d- learn wrote: > IOW you want to improve IFTI, so that `n` is inferred from the > length of the passed argument. That would indeed work for array > literals and CTFE-able expressions. Any improvement to IFTI is a > good thing,

Re: Use class template as a type

2016-11-28 Thread rikki cattermole via Digitalmars-d-learn
On 29/11/2016 3:35 AM, Namespace wrote: We have a handy dandy syntax for this: if (MyClassInt subclass = cast(MyClassInt)value) { writeln(subclass.value); } If it doesn't cast to said type (it will be null) that branch won't execute. Just out of interest: it looks like a dynamic_cast in

Re: How to get hash value of an object?

2016-11-28 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/27/16 2:10 AM, panmengh wrote: How to get hash value of an object? Use hashOf? or typeid(T).getHash()? hashOf is kind of this horrible hacky thing that nobody should be using. It literally takes whatever you pass it and hashes the local bytes. It doesn't care about opHash or if any of

Re: Use class template as a type

2016-11-28 Thread Basile B. via Digitalmars-d-learn
On Monday, 28 November 2016 at 14:35:36 UTC, Namespace wrote: We have a handy dandy syntax for this: if (MyClassInt subclass = cast(MyClassInt)value) { writeln(subclass.value); } If it doesn't cast to said type (it will be null) that branch won't execute. Just out of interest: it

Re: Parsing a string to instantiate classes at runtime

2016-11-28 Thread Marduk via Digitalmars-d-learn
On Sunday, 27 November 2016 at 21:28:52 UTC, ag0aep6g wrote: Ok, that's a hypothetical. It's "if D had a 'dynamic mixin', then we could do fancy things with it." D doesn't have a 'dynamic mixin', so you can't do those fancy things, at least not in the envisioned way. You are right. I

Re: Parsing a string to instantiate classes at runtime

2016-11-28 Thread Marduk via Digitalmars-d-learn
On Monday, 28 November 2016 at 09:33:08 UTC, Jacob Carlborg wrote: It's possible to bypass the constructors [1]. [1] https://github.com/jacob-carlborg/orange/blob/master/orange/util/Reflection.d#L166 Aha! Interesting. Thanks.

Re: Instantiating a class with different types at runtime

2016-11-28 Thread Marduk via Digitalmars-d-learn
On Sunday, 27 November 2016 at 21:06:58 UTC, ag0aep6g wrote: Turn Example into a template, and add a free function for nice construction: class Example(Type_left, Type_right) { /* ... as you had it ... */ } Example!(L, R) makeExample(L, R)(L x, R y) { return new Example!(L, R)(x,

Re: Instantiating a class with different types at runtime

2016-11-28 Thread Marduk via Digitalmars-d-learn
On Sunday, 27 November 2016 at 20:57:28 UTC, Namespace wrote: class Example(L, R) { L _left; R _right; this(L l, R r) { _left = l; _right = r; } } That was fast! But I needed the second reply in order to understand yours. Thanks anyway.

Re: Parsing a string to instantiate classes at runtime

2016-11-28 Thread Jacob Carlborg via Digitalmars-d-learn
On 2016-11-29 07:46, Marduk wrote: Aha! Interesting. Thanks. Then you can call a custom method that acts as a constructor when the instance is created this way, if there's a need for it. -- /Jacob Carlborg

Re: non-constant expression ["foo":5, "bar":10, "baz":2000]

2016-11-28 Thread Paolo Invernizzi via Digitalmars-d-learn
On Sunday, 27 November 2016 at 22:25:51 UTC, John Colvin wrote: On Saturday, 26 November 2016 at 17:37:57 UTC, Paolo Invernizzi wrote: This is stated in documentation [1]: immutable long[string] aa = [ "foo": 5, "bar": 10, "baz": 2000 ]; unittest { assert(aa["foo"] == 5);

Re: Parsing a string to instantiate classes at runtime

2016-11-28 Thread Jacob Carlborg via Digitalmars-d-learn
On 2016-11-27 22:19, Marduk wrote: Sure, it's here: http://forum.dlang.org/post/xmnnsdiuwyjrhkasy...@forum.dlang.org In that thread they also mention Object.factory, but the documentation says that the class must have either no constructor or the default constructor, which is not my case.