Re: Map one tuple to another Tuple of different type
On Tue, Jul 22, 2014 at 02:47:51AM +, Vlad Levenfeld via Digitalmars-d-learn wrote: > On Monday, 21 July 2014 at 19:02:59 UTC, H. S. Teoh via Digitalmars-d-learn > wrote: > >functionality is desirable. Maybe we should rouse a racket on the > >main D forum to either make staticIota public, or implement static > >foreach. ;-) > > static switch would be so sick. I frequently find myself doing some > compile-time branching with more than 2 branches or with an enum (like > for policies/strategies/whatever). Compile-time case labels would > clean that code up, and final switch would be a maintenance > improvement as well. > > static while sounds cool, but how would it work? (as in use case, not > implementation). The condition would have to be immutable, wouldn't > it? I don't know about use cases in general, but one place where it comes in handy is in iterating over template argument lists ("type tuples"). Currently, I have to resort to: Tuple!(int,int,int,int,int) fields; foreach (i; staticIota!(0, n)) { fields[i]++; // for example } Which is not bad for simple operations, but would be cleaner if we had static while / static foreach. T -- We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true. -- Robert Wilensk
Re: Map one tuple to another Tuple of different type
On Monday, 21 July 2014 at 19:02:59 UTC, H. S. Teoh via Digitalmars-d-learn wrote: functionality is desirable. Maybe we should rouse a racket on the main D forum to either make staticIota public, or implement static foreach. ;-) static switch would be so sick. I frequently find myself doing some compile-time branching with more than 2 branches or with an enum (like for policies/strategies/whatever). Compile-time case labels would clean that code up, and final switch would be a maintenance improvement as well. static while sounds cool, but how would it work? (as in use case, not implementation). The condition would have to be immutable, wouldn't it?
Re: Code spliting in module and packages
On Monday, 21 July 2014 at 18:02:33 UTC, bearophile wrote: Dicebot: Probably most idiomatic D way is to use files _instead_ of classes :) It is a bit idealistic though and is not yet 100% feasible in practice. What's stopping it from being feasible? Bye, bearophile Stuff like this : http://forum.dlang.org/post/mailman.57.1405963972.32463.digitalmar...@puremagic.com Also infamous private name clash issue.
Re: Map one tuple to another Tuple of different type
On Monday, 21 July 2014 at 18:10:14 UTC, H. S. Teoh via Digitalmars-d-learn wrote: On Mon, Jul 21, 2014 at 12:55:34AM +0200, Daniel Gibson via Digitalmars-d-learn wrote: Hi, I have a variadic templated function and want to call a C varargs function. I want to be able to pass static arrays, which D2 passes by value and C by reference, so I'd like to automagically translate those arguments. My idea was something like this: extern (C) origFun(int x, ...); T transTupleElem(T)(T arg) { return arg; } float* transTupleElem(T : float[3])(T arg) { return arg.ptr; } void fun(T...)(int x, T argTuple) { // create a new tuple type that replaces all static float[3] // arrays with float* to emulate C call-by-reference behavior alias ReplaceAll!(float[3], float*, T) ModifiedTuple; ModifiedTuple modTuple; foreach(size_t i ; 0 .. T.length) modTuple[i] = transTupleElem(argTuple[i]); // BOOM! origFun(modTuple); // or is it modTuple.expand ? } However, this doesn't work (dmd 2.065 linux64), because: "Error: variable i cannot be read at compile time" [...] Try this: import std.typecons : staticIota; foreach (i; staticIota!(0, T.length)) modTuple[i] = transTupleElem(argTuple[i]); T staticIota is marked package in std.typecons
Re: Map one tuple to another Tuple of different type
On Mon, Jul 21, 2014 at 06:48:45PM +, bearophile via Digitalmars-d-learn wrote: > H. S. Teoh: > > >It's already in std.typecons. > > But it is not online yet? [...] Hmph. Apparently it is undocumented. :-/ It has been in Phobos since last April, but was private until November when it became 'package'. Hold on a sec... so how is it that my code compiles with it?! Apparently some package protection bug? Sigh... Recently there was a pull request that implements static foreach / static while, but I can't seem to find it anymore. It's clear that this functionality is desirable. Maybe we should rouse a racket on the main D forum to either make staticIota public, or implement static foreach. ;-) T -- People tell me I'm stubborn, but I refuse to accept it!
Re: Map one tuple to another Tuple of different type
Am 21.07.2014 20:09, schrieb H. S. Teoh via Digitalmars-d-learn: On Mon, Jul 21, 2014 at 06:36:04PM +0200, Daniel Gibson via Digitalmars-d-learn wrote: [...] However, having something like staticIota in the stdlib would probably make sense. [...] It's already in std.typecons. (Admittedly, that's not exactly the most obvious place to look for it...) T static.typecons is actually where I would have expected it, as it constructs a tuple.. but it isn't mentioned on http://dlang.org/library/std/typecons.html or http://dlang.org/phobos/std_typecons.html and at least in my /usr/include/dmd/phobos/std/typecons.d (2.065) it's private: private template staticIota(int beg, int end) { ... } And it seems like I can't use it. Anyway, good to know that it exists and hopefully future versions of D2 make that function public, so thanks for showing up another alternative to solve my problem :-) BTW: The name "Iota" is horrible.. it doesn't describe at all what the function does. And "But C++11 STL has a function of the same name that does the same thing" or "some obscure programming language from the 60ies (APL) used the Greek iota letter to do this" is no excuse, one shouldn't expect potential D users to know about that (even after using C++ for years I never encountered std::iota..) Maybe "Numerate" or something like that would be more descriptive.. Cheers, Daniel
Re: Map one tuple to another Tuple of different type
H. S. Teoh: It's already in std.typecons. But it is not online yet? Bye, bearophile
Re: Map one tuple to another Tuple of different type
On Mon, Jul 21, 2014 at 06:36:04PM +0200, Daniel Gibson via Digitalmars-d-learn wrote: [...] > However, having something like staticIota in the stdlib would probably > make sense. [...] It's already in std.typecons. (Admittedly, that's not exactly the most obvious place to look for it...) T -- There are two ways to write error-free programs; only the third one works.
Re: Map one tuple to another Tuple of different type
On Mon, Jul 21, 2014 at 12:55:34AM +0200, Daniel Gibson via Digitalmars-d-learn wrote: > Hi, > I have a variadic templated function and want to call a C varargs function. > I want to be able to pass static arrays, which D2 passes by value and C by > reference, so I'd like to automagically translate those arguments. > > My idea was something like this: > > extern (C) origFun(int x, ...); > > T transTupleElem(T)(T arg) { return arg; } > > float* transTupleElem(T : float[3])(T arg) { > return arg.ptr; > } > > void fun(T...)(int x, T argTuple) { > // create a new tuple type that replaces all static float[3] > // arrays with float* to emulate C call-by-reference behavior > alias ReplaceAll!(float[3], float*, T) ModifiedTuple; > ModifiedTuple modTuple; > > foreach(size_t i ; 0 .. T.length) > modTuple[i] = transTupleElem(argTuple[i]); // BOOM! > > origFun(modTuple); // or is it modTuple.expand ? > } > > However, this doesn't work (dmd 2.065 linux64), because: > "Error: variable i cannot be read at compile time" [...] Try this: import std.typecons : staticIota; foreach (i; staticIota!(0, T.length)) modTuple[i] = transTupleElem(argTuple[i]); T -- The volume of a pizza of thickness a and radius z can be described by the following formula: pi zz a. -- Wouter Verhelst
Re: Code spliting in module and packages
Dicebot: Probably most idiomatic D way is to use files _instead_ of classes :) It is a bit idealistic though and is not yet 100% feasible in practice. What's stopping it from being feasible? Bye, bearophile
Re: Code spliting in module and packages
Hi, Thanks for your answers. I'll continue to code as I ussed to do, and try to improve my style to have a better usage of the modules (today i'm most one class per file, so I don't use the modules at all). Bye, Matthieu
Re: Map one tuple to another Tuple of different type
Am 21.07.2014 17:04, schrieb TheFlyingFiddle: On Monday, 21 July 2014 at 01:42:58 UTC, Daniel Gibson wrote: Am 21.07.2014 03:34, schrieb Vlad Levenfeld: To get a foreach to run at compile-time, you have to give it something whose value is known to the compiler (so, T and typeof(argTuple) would suffice, and 0..T.length really should as well). Yup I use this when i want a compile time foreach(from a constant number). It's slightly longer but has worked great for me thus far. template staticIota(size_t s, size_t e, size_t step = 1) { import std.typetuple : TypeTuple; static if(s < e) alias staticIota = TypeTuple!(s, staticIota!(s + step, e)); else alias staticIota = TypeTuple!(); } Yeah, I had a similar workaround: template TupleIndicesImpl(alias len, I...) { static if(len == I.length) // also handles len == 0 alias TupleIndicesImpl = I; else static if(I.length == 0) alias TupleIndicesImpl = TupleIndicesImpl!(len, 0); else // I contains 0 ... I.length - 1, so add I.length alias TupleIndicesImpl = TupleIndicesImpl!(len, I, I.length); } template TupleIndices(alias len) { alias TupleIndices = TupleIndicesImpl!(len); } foreach(i; TupleIndices!(myTuple.length) { ... } At least for iterating over a tuple Vlad's way suggestion ("foreach(i, U; TupleType)") is nicer and more concise. However, having something like staticIota in the stdlib would probably make sense. Cheers, Daniel
Re: Code spliting in module and packages
Probably most idiomatic D way is to use files _instead_ of classes :) It is a bit idealistic though and is not yet 100% feasible in practice.
Re: Code spliting in module and packages
Matthieu: Thanks for your answers. I'll continue to code as I ussed to do, and try to improve my style to have a better usage of the modules (today i'm most one class per file, so I don't use the modules at all). Also, try to use less classes and more free (pure) functions :-) This means a little more functional style. Bye, bearophile
Re: Map one tuple to another Tuple of different type
On Monday, 21 July 2014 at 01:42:58 UTC, Daniel Gibson wrote: Am 21.07.2014 03:34, schrieb Vlad Levenfeld: To get a foreach to run at compile-time, you have to give it something whose value is known to the compiler (so, T and typeof(argTuple) would suffice, and 0..T.length really should as well). Yup I use this when i want a compile time foreach(from a constant number). It's slightly longer but has worked great for me thus far. template staticIota(size_t s, size_t e, size_t step = 1) { import std.typetuple : TypeTuple; static if(s < e) alias staticIota = TypeTuple!(s, staticIota!(s + step, e)); else alias staticIota = TypeTuple!(); } usage: unittest { foreach(i; staticIota!(0, 10)) { pragma(msg, i); } } //Outputs 1 to 10 at compile-time.
Re: Map one tuple to another Tuple of different type
On Monday, 21 July 2014 at 15:04:14 UTC, TheFlyingFiddle wrote: //Outputs 1 to 10 at compile-time. Edit: 0 to 9
Re: Really nooB question - @property
On Sunday, 20 July 2014 at 16:35:52 UTC, Eric wrote: There are a lot of discussions in the forums about how @property should or could be implemented. But I can't seem to find anything that explains why or when I should use @property with the current compiler. Can anyone explain why and when I should use the @property tag? Consider the following struct: struct Vector4 { @property float x() { return vec[0]; } . . @property void x(float _x) { vec[0] = _x } . . private: float[4] vec; } Here I like to use static array for storing components of vector because I want to use special operations on arrays while taking advantage of auto-vectorization if possible. In this case, it is very convenient to use properties.
Re: Really nooB question - @property
On Sunday, 20 July 2014 at 18:14:29 UTC, Eric wrote: Use @property when you want a pseudo-variable or something that might be conceptually considered a "property" of the object, i.e. to do this: auto blah = thing.someProperty; thing.someProperty = blahblah; This is basically what I suspected. But why write: @property int getValue() { return(value); } You should use nouns to name properties and verbs to name methods. D has optional parentheses, so you can write x=getValue; So @property is generally not needed except for the case when the method returns a delegate, which in its turn can be implicitly called, so there's an ambiguity, which @property was meant to solve (but AFAIK in the end it didn't). It also serves as a documentation, that the method should be viewed as a property. Initially optional parentheses were meant as an easy implementation of properties, and @property wasn't meant to exist until the ambiguity with the delegate was understood.
Re: Map one tuple to another Tuple of different type
On Monday, 21 July 2014 at 15:04:14 UTC, TheFlyingFiddle wrote: template staticIota(size_t s, size_t e, size_t step = 1) { import std.typetuple : TypeTuple; static if(s < e) alias staticIota = TypeTuple!(s, staticIota!(s + step, e, step)); else alias staticIota = TypeTuple!(); } Edit: Missed the second step.
Re: Really nooB question - @property
On Sunday, 20 July 2014 at 18:14:29 UTC, Eric wrote: Use @property when you want a pseudo-variable or something that might be conceptually considered a "property" of the object, i.e. to do this: auto blah = thing.someProperty; thing.someProperty = blahblah; This is basically what I suspected. But why write: @property int getValue() { return(value); } When you could just have a public field: int value; That lets you set and get the value without the parens anyways? thanks, Eric It allows you to add extra logic on read/write, without breaking any user code.
Re: mixin assembler does not work?
On Sunday, 20 July 2014 at 17:50:10 UTC, Nicolas Sicard wrote: On Sunday, 20 July 2014 at 15:02:58 UTC, Foo wrote: On Sunday, 20 July 2014 at 14:55:00 UTC, Foo wrote: For clarification: how would that work without mixin + string? I tried this: mixin template Vala2(uint count, alias arr) { asm { sub ESP, count; mov arr, count; mov arr + 4, ESP; } } but I get several errors. Unfortunately it seems that asm cannot be used in mixin templates?! The reason may be that mixin templates are just for inserting declarations, which asm blocks aren't. This limitation isn't specific to asm. But what's the reason for that?
Re: Map one tuple to another Tuple of different type
Am 21.07.2014 03:05, schrieb Vlad Levenfeld: Thats real weird that it would reject your "i" variable, given that T.length is known at compile time. I think this is a bug. I can get your code to compile if I change your foreach loop to this: foreach(i, U; T) modTuple[i] = transTupleElem(argTuple[i]); // ok That works indeeed. I also tried "foreach(int i, x; argTuple)" which also with the same error as "foreach(i ; 0 .. T.length)". As a workaround I created a TupleIndices template, that would return a tuple with 0 .. len and did "foreach(i; TupleIndices!(T.length)" but that was kinda messy and reminded me of the loops I had to jump through in C++ to do anything useful with variadic templates.. I agree that this is a bug, but at least your workaround is much nicer, thanks a lot! :-) Cheers, Daniel (@Vlad: Originally I answered you directly because the Thunderbird developers thought it was a clever idea to put an "answer" button that answers to the author instead of to the newsgroup prominently into the GUI)
Re: Really nooB question - @property
On 7/21/2014 3:14 AM, Eric wrote: Use @property when you want a pseudo-variable or something that might be conceptually considered a "property" of the object, i.e. to do this: auto blah = thing.someProperty; thing.someProperty = blahblah; This is basically what I suspected. But why write: @property int getValue() { return(value); } When you could just have a public field: int value; That lets you set and get the value without the parens anyways? thanks, Eric In addition to what others have pointed out, it's useful for read-only values. You don't always want to allow clients to be able to set an objects members. --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com
Re: Code spliting in module and packages
On 7/21/2014 7:13 AM, Matthieu wrote: Hi! I can't find any good tutorial about how to split my source code in modules and packages. I don't want to use a Java splitting style (one class per file) or something like that, I want to use the D way, but it's so hard to find it! Can someone explain to me how to do it well, please? I don't think there is a "D way." It's a matter of personal preference. I don't like to see huge modules, so I prefer to keep them small. But what is a reasonable level of separation for me may not be the same for you. Just go with what feels natural to you. --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com
Re: Map one tuple to another Tuple of different type
Am 21.07.2014 03:34, schrieb Vlad Levenfeld: You're very welcome. The reason foreach(int i, x; argTuple) failed is because argTuple is a value (of type T), and so known only at run-time. Hmm but the only thing the compiler would need to know at compile-time is still i, which only depends on argTuple.length which is known at compile-time. But ok, I can kinda understand that this doesn't work, probably foreach either operates completely on compile-time stuff (and does so statically) or completely on runtime-stuff, done dynamically. To get a foreach to run at compile-time, you have to give it something whose value is known to the compiler (so, T and typeof(argTuple) would suffice, and 0..T.length really should as well). Yup A nice way to test is "pragma(msg, Foo)" where Foo is your argument... if its knowable at compile-time, then your compiler should output "Foo" (or the name of whatever its aliasing) to the console. Thanks for the advice! Cheers, Daniel
Re: Map one tuple to another Tuple of different type
On Monday, 21 July 2014 at 01:29:40 UTC, Daniel Gibson wrote: Am 21.07.2014 03:05, schrieb Vlad Levenfeld: Thats real weird that it would reject your "i" variable, given that T.length is known at compile time. I think this is a bug. I can get your code to compile if I change your foreach loop to this: foreach(i, U; T) modTuple[i] = transTupleElem(argTuple[i]); // ok That works indeeed. I also tried "foreach(int i, x; argTuple)" which also with the same error as "foreach(i ; 0 .. T.length)". As a workaround I created a TupleIndices template, that would return a tuple with 0 .. len and did "foreach(i; TupleIndices!(T.length)" but that was kinda messy and reminded me of the loops I had to jump through in C++ to do anything useful with variadic templates.. I agree that this is a bug, but at least your workaround is much nicer, thanks a lot! :-) Cheers, Daniel (@Vlad: Originally I answered you directly because the Thunderbird developers thought it was a clever idea to put an "answer" button that answers to the author instead of to the newsgroup prominently into the GUI) You're very welcome. The reason foreach(int i, x; argTuple) failed is because argTuple is a value (of type T), and so known only at run-time. To get a foreach to run at compile-time, you have to give it something whose value is known to the compiler (so, T and typeof(argTuple) would suffice, and 0..T.length really should as well). A nice way to test is "pragma(msg, Foo)" where Foo is your argument... if its knowable at compile-time, then your compiler should output "Foo" (or the name of whatever its aliasing) to the console.
Re: Map one tuple to another Tuple of different type
Thats real weird that it would reject your "i" variable, given that T.length is known at compile time. I think this is a bug. I can get your code to compile if I change your foreach loop to this: foreach(i, U; T) modTuple[i] = transTupleElem(argTuple[i]); // ok
Re: Compile-Time Interfaces (Concepts)
On Sunday, 20 July 2014 at 15:45:37 UTC, Atila Neves wrote: On Thursday, 17 July 2014 at 22:52:37 UTC, Justin Whear wrote: On Thu, 17 Jul 2014 22:49:30 +, Nordlöw wrote: AFAIK there is no compile-time variant of interfaces right? Why is that? Wouldn't it be nice to say something like struct SomeRange realize InputRange { /* implement members of InputRange */ } and then the compiler will statically check that that all members are implemented correctly. I guess this requires some new syntax to describe what an InputRange is. Kind of like C++ Concepts. What benefits would accrue from adding this? Static verification that a structure implements the specified concepts? If so, you can simply do this instead: static assert(isInputRange!SomeRange); This is sufficient, but not adequate. Just as the built-in unittest blocks with assertions, it's great when the assertion is true but good luck finding out where the bug is when it's not. The D Cookbook has an idiom to handle this by checking for __ctfe but it's super hacky and there should be a better way. I have lost count of how many times I wish the compiler would help me with compile time interfaces as it does with runtime code. static override and static interface? Yes please. Atila +1, failing template constraints just gives a vague "couldn't match overload" type of error, and sometimes static assertions get suppressed. I've noticed that opDispatch is particularly bad about this. Even syntactic errors won't trigger compiler messages, and instead seems to behave like SFINAE which I was assured doesn't exist in D. I have to use pragma (msg, ...) to get meaningful errors. Its so bad I generally avoid opDispatch despite its awesome potential and just generate template functions with mixins instead, because they are marginally easier to debug. I wind up doing things like this to get the functionality I want: static string assert_processing_stage_defined (string stage)() { static immutable error_msg = `"Model must define processing stage: ` ~stage~ ` ()"`; return q{ static assert (hasMember!(This, } `"`~stage~`"` q{), } ~error_msg~ q{); static assert (isSomeFunction!(__traits(getMember, This, } `"`~stage~`"` q{)), } ~error_msg~ q{); static assert (ParameterTypeTuple!(__traits(getMember, This, } `"`~stage~`"` q{)).length == 0, } ~error_msg~ q{); static assert (is (ReturnType!(__traits(getMember, This, } `"`~stage~`"` q{)) == void), } ~error_msg~ q{); }; } mixin(`` ~assert_processing_stage_defined!`initialize` ~assert_processing_stage_defined!`update` ); I'm sure theres worse ways to do it but I still find this ugly and overly specific. I would much rather use something like what Nordlöw suggested. Something that is standardized across the language and generates meaningful error messages.
Map one tuple to another Tuple of different type
Hi, I have a variadic templated function and want to call a C varargs function. I want to be able to pass static arrays, which D2 passes by value and C by reference, so I'd like to automagically translate those arguments. My idea was something like this: extern (C) origFun(int x, ...); T transTupleElem(T)(T arg) { return arg; } float* transTupleElem(T : float[3])(T arg) { return arg.ptr; } void fun(T...)(int x, T argTuple) { // create a new tuple type that replaces all static float[3] // arrays with float* to emulate C call-by-reference behavior alias ReplaceAll!(float[3], float*, T) ModifiedTuple; ModifiedTuple modTuple; foreach(size_t i ; 0 .. T.length) modTuple[i] = transTupleElem(argTuple[i]); // BOOM! origFun(modTuple); // or is it modTuple.expand ? } However, this doesn't work (dmd 2.065 linux64), because: "Error: variable i cannot be read at compile time" In C++11 (where almost everything else about variadic templates is pretty painful), this could probably be done like: template void fun(T... args) { origFun( transTupleElem(args)... ); } so I guess it shouldn't be too hard to do something equivalent in D? I looked for some kind of staticMap() function that could map values from one tuple to another (of the same type), but only found std.typetuple.staticMap() which only seems to modify types in TypeTuples, not values in tuple instances. Cheers, Daniel
Re: Code spliting in module and packages
Matthieu: Can someone explain to me how to do it well, please? Put related stuff in a module, and unrelated stuff in other modules. If the module grows too much, split it up in two or more. It's about the same as in Python. Just remember that classes can see each other private members only if they are in the same module. So if you move things to another module, sometimes some stuff (like unittests) breaks. In the same time, is there someone who knows where to find a good style guide for D, something like the PEP for python? A starting point: http://dlang.org/dstyle.html Bye, bearophile
Code spliting in module and packages
Hi! I can't find any good tutorial about how to split my source code in modules and packages. I don't want to use a Java splitting style (one class per file) or something like that, I want to use the D way, but it's so hard to find it! Can someone explain to me how to do it well, please? In the same time, is there someone who knows where to find a good style guide for D, something like the PEP for python? Thx. Matthieu
Re: Question about iteger literals
I see these rules but when I compile following code and it fails with error it looks VERY stupid. import std.stdio; void main() { ubyte a = 15; ubyte b = 10; ubyte c = a + b; //What is happening there?! ARGH! Are you joking?! } Compilation output: /d837/f382.d(9): Error: cannot implicitly convert expression (cast(int)a + cast(int)b) of type int to ubyte I'm just crazy about it! How could it happen?! I just should forget about all integer type except *int*, because it make my head just explode!!!
Re: Really nooB question - @property
On Sunday, 20 July 2014 at 16:35:52 UTC, Eric wrote: There are a lot of discussions in the forums about how @property should or could be implemented. But I can't seem to find anything that explains why or when I should use @property with the current compiler. Can anyone explain why and when I should use the @property tag? Thx. Eric I wondered the same: http://forum.dlang.org/thread/uskutitmqgdfjeusr...@forum.dlang.org
Re: Learning to use ranges instead of arrays
Also, there is std.array.array for the ranges you want to convert to arrays. For example, if "a" is an array, "a.map!(x => x * 2).array" produces an new array of doubled values (as opposed to a lazy range produced by std.algorithm.map).
Re: Really nooB question - @property
On 07/20/2014 11:14 AM, Eric wrote: > >> Use @property when you want a pseudo-variable or something that might >> be conceptually considered a "property" of the object, i.e. to do this: >> >> auto blah = thing.someProperty; >> thing.someProperty = blahblah; > > This is basically what I suspected. But why > write: > > @property int getValue() { return(value); } > > When you could just have a public field: > > int value; > > That lets you set and get the value without the parens anyways? Freely setting a member makes sense only in limited cases where that member does not take any part in any invariant of the object. For example, if a Rectangle class has .length, .width, and .area, it would be an error to set either of them. Additionally, properties enable one to make it look like a type has such a member: struct Rectangle { // ... @property int area() { return length * width; } } Ali
Re: mixin assembler does not work?
On Sunday, 20 July 2014 at 15:54:15 UTC, bearophile wrote: mixin template Vala2(uint count, alias arr) { What about disallowing "mixin templatename" unless you add "mixin" before the "template" keyword? Bye, bearophile I thought it's disallowed.
Re: mixin assembler does not work?
On Sunday, 20 July 2014 at 15:02:58 UTC, Foo wrote: On Sunday, 20 July 2014 at 14:55:00 UTC, Foo wrote: For clarification: how would that work without mixin + string? I tried this: mixin template Vala2(uint count, alias arr) { asm { sub ESP, count; mov arr, count; mov arr + 4, ESP; } } but I get several errors. Unfortunately it seems that asm cannot be used in mixin templates?! The reason may be that mixin templates are just for inserting declarations, which asm blocks aren't. This limitation isn't specific to asm.
Re: Question about iteger literals
Uranuz: ubyte a = 15; ubyte b = 10; ubyte c = a + b; //What is happening there?! ARGH! Are you joking?! In C/C++/D if you sum a types that are smaller than int, you obtain an int. D has copied C for backwards compatibility with C code. Bye, bearophile
Re: Really nooB question - @property
On Sunday, 20 July 2014 at 16:35:52 UTC, Eric wrote: There are a lot of discussions in the forums about how @property should or could be implemented. But I can't seem to find anything that explains why or when I should use @property with the current compiler. Can anyone explain why and when I should use the @property tag? Thx. Eric Use @property when you want a pseudo-variable or something that might be conceptually considered a "property" of the object, i.e. to do this: auto blah = thing.someProperty; thing.someProperty = blahblah; or struct myArray(T) { T[] arr; @property size_t memSize() { return arr.length * T.sizeof; } } Other than that, don't use it. Ordinary functions can be called without parenthesis anyway. Using those ideas you shouldn't run in to any surprises.
Re: Question about iteger literals
In C/C++/D if you sum a types that are smaller than int, you obtain an int. D has copied C for backwards compatibility with C code. Bye, bearophile Is there any reasoning why this should remain unchainged? How could it break interface between languages? And also this code succesfully compiles and runs in C++. #include using namespace std; int main() { unsigned short a = 15; unsigned short b = 10; unsigned short c = a + b; //There is no problem cout << c << endl; return 0; } As D compiler doesn't need to compile C programme and have compatible operations with types. Why we still should keep this garbage?! I don't know the right solution but I believe that previous example illustrates some contradiction in integer types system design or implementation. Why we dont promote *ulong* and *long* to int? Let's also promote string into array of ints?! Can't believe it!
Re: How to say to compiler that I want to inherit final template bethod of base interface into derived class
On Sunday, 20 July 2014 at 16:47:30 UTC, Uranuz wrote: Where did you get it? Or you compiled it yourself? I'm building it myself. It's not difficult, when you know basic git. And it doesn't take long. You can find instructions here: http://wiki.dlang.org/Building_DMD Because I tried beta4 and it doesn't work there too. If it doesn't work in the 2.066 beta, I guess that means you'd have to wait for 2.067 to get the feature in a release.
Re: Really nooB question - @property
On Sunday, 20 July 2014 at 17:59:07 UTC, John Colvin wrote: On Sunday, 20 July 2014 at 16:35:52 UTC, Eric wrote: There are a lot of discussions in the forums about how @property should or could be implemented. But I can't seem to find anything that explains why or when I should use @property with the current compiler. Can anyone explain why and when I should use the @property tag? Thx. Eric Use @property when you want a pseudo-variable or something that might be conceptually considered a "property" of the object, i.e. to do this: auto blah = thing.someProperty; thing.someProperty = blahblah; or struct myArray(T) { T[] arr; @property size_t memSize() { return arr.length * T.sizeof; } } Other than that, don't use it. Ordinary functions can be called without parenthesis anyway. Using those ideas you shouldn't run in to any surprises. Oh, but don't expect the compiler to enforce this and please don't use the -property flag, it will only cause you pain.
Re: Really nooB question - @property
Use @property when you want a pseudo-variable or something that might be conceptually considered a "property" of the object, i.e. to do this: auto blah = thing.someProperty; thing.someProperty = blahblah; This is basically what I suspected. But why write: @property int getValue() { return(value); } When you could just have a public field: int value; That lets you set and get the value without the parens anyways? thanks, Eric
Re: Question about iteger literals
On Monday, 23 June 2014 at 18:32:38 UTC, Steven Schveighoffer wrote: On Sun, 22 Jun 2014 08:23:45 -0400, Uranuz wrote: If these rules are not so clear and have some exceptions (but I don't understand why they are needed) then some documentation needed about this. See integer promotion rules: http://dlang.org/type.html#Integer%20Promotions And the section below it. -Steve I see these rules but when I compile following code and it fails with error it looks VERY stupid. import std.stdio; void main() { ubyte a = 15; ubyte b = 10; ubyte c = a + b; //What is happening there?! ARGH! Are you joking?! } Compilation output: /d837/f382.d(9): Error: cannot implicitly convert expression (cast(int)a + cast(int)b) of type int to ubyte I'm just crazy about it! How could it happen?!
Re: mixin assembler does not work?
Foo: I do not understand? Sorry, mine was a language design question (to catch your bug). Bye, bearophile
Really nooB question - @property
There are a lot of discussions in the forums about how @property should or could be implemented. But I can't seem to find anything that explains why or when I should use @property with the current compiler. Can anyone explain why and when I should use the @property tag? Thx. Eric
Re: How to say to compiler that I want to inherit final template bethod of base interface into derived class
On Sunday, 20 July 2014 at 16:12:20 UTC, anonymous wrote: On Sunday, 20 July 2014 at 15:48:19 UTC, Uranuz wrote: Sorry, but this example doesn't work too. Ugh, 2.065 doesn't like it, but it works for me with git head (v2.066-devel-82b031c). Where did you get it? Or you compiled it yourself? Because I tried beta4 and it doesn't work there too.
Learning to use ranges instead of arrays
I find myself writing this code too much and i'm curious what D idiom I am missing. given a list of files (or any strings) and then maybe I want to sort them and maybe I don't. string [] fileList; ... fill list if (sort) { string [] tempList; foreach(filename; sort(fileList)) { tempList ~= filename; } fileList = tempList; } foreach(filename, fileList) { ... do something; } but I know this is ugly code, so I'm curious as to how to make it better. I suspect it is something very simple, about switching everything I do to 'ranges', but I can't see it yet. Thanks for any input, Bob
Re: How to say to compiler that I want to inherit final template bethod of base interface into derived class
On Sunday, 20 July 2014 at 15:48:19 UTC, Uranuz wrote: Sorry, but this example doesn't work too. Ugh, 2.065 doesn't like it, but it works for me with git head (v2.066-devel-82b031c).
Re: Learning to use ranges instead of arrays
Sorry, somehow this submitted in the middle, even without the captcha. Please see the later version.
Re: Learning to use ranges instead of arrays
On Sunday, 20 July 2014 at 16:11:03 UTC, John Colvin wrote: Even without ranges, you can do this: string [] fileList; ... fill list if (sort) sort(fileList); foreach(filename, fileList) { ... do something; } because sort works in-place. Oh, I see that now. Guess when I saw it returned a SortedRange, I stopped reading and missed the bit about the underlying array also being sorted. Thanks for the quick help, Bob
Re: mixin assembler does not work?
On Sunday, 20 July 2014 at 15:54:15 UTC, bearophile wrote: mixin template Vala2(uint count, alias arr) { What about disallowing "mixin templatename" unless you add "mixin" before the "template" keyword? Bye, bearophile I do not understand?
Re: Learning to use ranges instead of arrays
On Sunday, 20 July 2014 at 16:02:05 UTC, Bob Tolbert wrote: I find myself writing this code too much and i'm curious what D idiom I am missing. given a list of files (or any strings) and then maybe I want to sort them and maybe I don't. string [] fileList; ... fill list if (sort) { string [] tempList; foreach(filename; sort(fileList)) { tempList ~= filename; } fileList = tempList; } foreach(filename, fileList) { ... do something; } but I know this is ugly code, so I'm curious as to how to make it better. I suspect it is something very simple, about switching everything I do to 'ranges', but I can't see it yet. Thanks for any input, Bob Even without ranges, you can do this: string [] fileList; ... fill list if (sort) sort(fileList); foreach(filename, fileList) { ... do something; } because sort works in-place.
Re: How to say to compiler that I want to inherit final template bethod of base interface into derived class
On Sunday, 20 July 2014 at 12:48:09 UTC, anonymous wrote: import std.stdio; interface IBase { template getStr(string fieldName) { final string getStr() { return "George"; } } string getStr(string fieldName); } class Derived: IBase { alias getStr = IBase.getStr; /* order matters, see below */ override string getStr(string fieldName) { return "Sam"; } /* alias getStr = IBase.getStr; /* doesn't work here, I guess that's a compiler bug */ } void main() { auto obj = new Derived; assert( obj.getStr!("aaa")() == "George" ); assert( obj.getStr("aaa") == "Sam" ); } Sorry, but this example doesn't work too.
Learning to use ranges instead of arrays
I find myself writing this code too much and i'm curious what D idiom I am missing. given a list of files (or any string) and then maybe I want to sort them and maybe I don't. string [] fileList; ... fill list if (sort) {
Re: mixin assembler does not work?
mixin template Vala2(uint count, alias arr) { What about disallowing "mixin templatename" unless you add "mixin" before the "template" keyword? Bye, bearophile
Re: Compile-Time Interfaces (Concepts)
On Thursday, 17 July 2014 at 22:52:37 UTC, Justin Whear wrote: On Thu, 17 Jul 2014 22:49:30 +, Nordlöw wrote: AFAIK there is no compile-time variant of interfaces right? Why is that? Wouldn't it be nice to say something like struct SomeRange realize InputRange { /* implement members of InputRange */ } and then the compiler will statically check that that all members are implemented correctly. I guess this requires some new syntax to describe what an InputRange is. Kind of like C++ Concepts. What benefits would accrue from adding this? Static verification that a structure implements the specified concepts? If so, you can simply do this instead: static assert(isInputRange!SomeRange); This is sufficient, but not adequate. Just as the built-in unittest blocks with assertions, it's great when the assertion is true but good luck finding out where the bug is when it's not. The D Cookbook has an idiom to handle this by checking for __ctfe but it's super hacky and there should be a better way. I have lost count of how many times I wish the compiler would help me with compile time interfaces as it does with runtime code. static override and static interface? Yes please. Atila
Re: mixin assembler does not work?
Foo: Hey guys. Can someone explain me, why this code does only works with the inline assembler version but not with the mixin? You need mixin(), it's unfortunate this gives no error messages: import std.string: format; enum Vala(uint count, alias arr) = format(" asm { sub ESP, %d; // Reserve 'count' bytes mov %s, %d; // Set a.length = 'count' mov %s + 4, ESP; // Set &a[0] to reserved bytes }", count, arr.stringof, count, arr.stringof); void main() { import std.stdio: writeln; ubyte[] a; a.length.writeln; static if (false) { asm { sub ESP, 1000; // Reserve 1000 bytes mov a, 1000;// Set a.length = 1000 mov a + 4, ESP; // Set &a[0] to reserved bytes } } else { mixin(Vala!(1000, a)); } a.length.writeln; } Bye, bearophile
Re: mixin assembler does not work?
Foo: It seems that your code doesn't work with 2.065. Right. For 2.065 you have to replace the enum expression with a template. If you can I suggest to update your compiler to the latest beta. There are tons of bugfixes and changes. Bye, bearophile
Re: mixin assembler does not work?
On Sunday, 20 July 2014 at 14:18:58 UTC, Foo wrote: template Vala(uint count, alias arr) { immutable string c = to!string(count); enum Vala = "asm { sub ESP, " ~ c ~ "; mov " ~ arr.stringof ~ ", " ~ c ~ "; mov " ~ arr.stringof ~ " + 4, ESP; }"; } ... mixin Vala!(1000, a); I'm not sure how to do it, but I see few mistakes in your code: 1. You declaring it as a string. (Or your intend to use `mixin()`?) 2. You trying to use `template` as a [`mixin template`][0]. [0]: http://dlang.org/template-mixin
Re: mixin assembler does not work?
On Sunday, 20 July 2014 at 14:55:00 UTC, Foo wrote: For clarification: how would that work without mixin + string? I tried this: mixin template Vala2(uint count, alias arr) { asm { sub ESP, count; mov arr, count; mov arr + 4, ESP; } } but I get several errors. Unfortunately it seems that asm cannot be used in mixin templates?!
Re: mixin assembler does not work?
On Sunday, 20 July 2014 at 14:46:32 UTC, bearophile wrote: enum Vala(uint count, alias arr) = format(" asm { sub ESP, %d; // Reserve 'count' bytes mov %s, %d; // Set a.length = 'count' mov %s + 4, ESP; // Set &a[0] to reserved bytes }", count, arr.stringof, count, arr.stringof); I'd like to write that more like this: enum Vala(uint count, alias arr) = format(" asm { sub ESP, %%(count); // Reserve 'count' bytes mov %%(arr.stringof), %%(count); // Set a.length = 'count' mov %%(arr.stringof) + 4, ESP; // Set &a[0] to reserved bytes }"); See: http://fslang.uservoice.com/forums/245727-f-language/suggestions/6002107-steal-nice-println-syntax-from-swift Bye, bearophile It seems that your code doesn't work with 2.065.
Re: mixin assembler does not work?
On Sunday, 20 July 2014 at 14:44:07 UTC, sigod wrote: On Sunday, 20 July 2014 at 14:18:58 UTC, Foo wrote: template Vala(uint count, alias arr) { immutable string c = to!string(count); enum Vala = "asm { sub ESP, " ~ c ~ "; mov " ~ arr.stringof ~ ", " ~ c ~ "; mov " ~ arr.stringof ~ " + 4, ESP; }"; } ... mixin Vala!(1000, a); I'm not sure how to do it, but I see few mistakes in your code: 1. You declaring it as a string. (Or your intend to use `mixin()`?) 2. You trying to use `template` as a [`mixin template`][0]. [0]: http://dlang.org/template-mixin Yeah, it now works with mixin(Vala!(1000, a)); I thought that both are the same.
Re: mixin assembler does not work?
sigod: , but I see few mistakes in your code: What's unfortunate is the silence of the compiler about that programmer mistake :-) Bye, bearophile
Re: mixin assembler does not work?
enum Vala(uint count, alias arr) = format(" asm { sub ESP, %d; // Reserve 'count' bytes mov %s, %d; // Set a.length = 'count' mov %s + 4, ESP; // Set &a[0] to reserved bytes }", count, arr.stringof, count, arr.stringof); I'd like to write that more like this: enum Vala(uint count, alias arr) = format(" asm { sub ESP, %%(count); // Reserve 'count' bytes mov %%(arr.stringof), %%(count); // Set a.length = 'count' mov %%(arr.stringof) + 4, ESP; // Set &a[0] to reserved bytes }"); See: http://fslang.uservoice.com/forums/245727-f-language/suggestions/6002107-steal-nice-println-syntax-from-swift Bye, bearophile
Re: mixin assembler does not work?
For clarification: how would that work without mixin + string?
mixin assembler does not work?
Hey guys. Can someone explain me, why this code does only works with the inline assembler version but not with the mixin? Thanks in advance! Code: import std.stdio : writeln; import std.conv : to; template Vala(uint count, alias arr) { immutable string c = to!string(count); enum Vala = "asm { sub ESP, " ~ c ~ "; mov " ~ arr.stringof ~ ", " ~ c ~ "; mov " ~ arr.stringof ~ " + 4, ESP; }"; } void main() { ubyte[] a; writeln(a.length); static if (false) { asm { sub ESP, 1000; // Reserve 1000 bytes mov a, 1000;// Set a.length = 1000 mov a + 4, ESP; // Set &a[0] to reserved bytes } } else { mixin Vala!(1000, a); } writeln(a.length); }