D1 and D2 differences
How up to date is http://digitalmars.com/d/2.0/features2.html ? If it's old, what differences can people think of off the tops of their heads that are not listed?
Re: multiple return
On 2011-04-20 01:35:46 +0300, %u said: I have function which have more than one return, and the code compile and run but it gives rong result -I guess-, so i use tuple but the compiler can't return tuple. how can I return values? why I can't return tuple? In D, tuple is not built in type, it is defined in stantard library. To use it, you must import module std.typecons Example: import std.typecons; struct Foo {} Tuple!(int, string, Foo) baz() { // some computation here return tuple(13, "inches", Foo()); } void main() { auto bar = baz(); assert(bar == tuple(13, "inches", Foo())); }
sin(float), cos(float)
In the C standard library there are the sqrtf, cosf, sinf functions, that return a 32 bit float. In D std.math.sqrt returns a float if the input argument is float, but sin and cos return double even if their argument is float: import std.math: sqrt, sin, cos; void main() { float x = 1.0f; static assert(is(typeof( sqrt(x) ) == float)); // OK static assert(is(typeof( sin(x) ) == float)); // ERR static assert(is(typeof( cos(x) ) == float)); // ERR } I think this is not correct, and it's worth a Bugzilla entry (if not already present). Do you agree? Bye, bearophile
Re: multiple return
%u: > I have function which have more than one return, and the code compile and run > but it gives rong result -I guess-, so i use tuple but the compiler can't > return tuple. > > how can I return values? > why I can't return tuple? Currently in D there are two ways to return multiple values from a function: - To use a std.typecons.tuple (not a std.typetuple). - To use "out" function arguments. Regarding typetuples, they can't be used to return multiple values from a function because of differences in stack alignment, this means incompatible ABI of D functions and D typetuples. This is not a great thing, but I presume that doing otherwise breaks a "zero overhead" constraint Walter seems to require to D typetuples. Given the presence of typecons tuples (and maybe in future some syntax sugar to use them in a more handy way), this is not a significant limitation. It's mostly strange to have two very different kinds of tuples in a single language. Bye, bearophile
multiple return
I have function which have more than one return, and the code compile and run but it gives rong result -I guess-, so i use tuple but the compiler can't return tuple. how can I return values? why I can't return tuple?
Re: case statement allows for runtime values,
Andrej Mitrovic: > Got it. Bug is reported. Good. > You can compare anything in an if statement, so why is switch more limited? switch has stronger requirements than a series of if statements and its uses such extra information to create assembly code that's more efficient than a series of if statement, like a dense jump table. (And in some situations there are ways to produce something even better, if you need to emulate a state machine or an interpreter. You are able to do it in GCC with computed gotos). Bye, bearophile
Re: case statement allows for runtime values,
Andrej Mitrovic Wrote: > Got it. Bug is reported. > > Btw, is there a specific reason why non-const values are not allowed? > > I mean, doesn't a switch statement like this: > switch(value) > { > case 1: > foo(); break; > case 2: > bar(); break; > default: > doo(); > } > > expand to: > > if (value == 1) > foo(); > else if (value == 2) > bar(); > else > doo(); > > You can compare anything in an if statement, so why is switch more limited? No, it doesn't lower to an if/else statement. I didn't quite understand the details, but I'm actually pretty sure it needs a compile-time value.
Re: case statement allows for runtime values,
Got it. Bug is reported. Btw, is there a specific reason why non-const values are not allowed? I mean, doesn't a switch statement like this: switch(value) { case 1: foo(); break; case 2: bar(); break; default: doo(); } expand to: if (value == 1) foo(); else if (value == 2) bar(); else doo(); You can compare anything in an if statement, so why is switch more limited?
Re: case statement allows for runtime values,
Andrej Mitrovic Wrote: > On 4/19/11, Jesse Phillips wrote: > > Yes bug. Not this part though > > > >> switch (x = foo(y)) > > > > Yeah that I know. > > Do you happen to know if this bug is already filed or should I file it? I would not know. As long as you do a best guess search on what you think it would be in Bugzilla, post the bug. It is best to have a duplicate here and there then to miss something. In fact even when something seems similar it is good to post a new bug when you're not sure (mention possibly related bugs in the report).
Re: case statement allows for runtime values, a case of accepts-invalid?
*I've searched bugzilla and couldn't find an entry for this particular case.
Re: case statement allows for runtime values, a case of accepts-invalid?
On 4/19/11, Jesse Phillips wrote: > Yes bug. Not this part though > >> switch (x = foo(y)) > Yeah that I know. Do you happen to know if this bug is already filed or should I file it?
Re: case statement allows for runtime values, a case of accepts-invalid?
Andrej Mitrovic Wrote: > int foo(ref int y) > { > y = 5; > return y; > } > > void main() > { > int x = 1; > int y = 2; > > switch (x = foo(y)) > { > case y: > writeln("x == y"); > default: > } > > assert(x == 5); > assert(y == 5); > } Yes bug. Not this part though > switch (x = foo(y))
Re: Custom compare function for array.sort on an integer array?
> If your are talking about D2, then use std.algorithm sort > Like taken from docs below: > > bool myComp(int x,int y) {return x> y; } > sort!(myComp)(array); > > > See also: > http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#sort > > -- > Dmitry Olshansky Ah, yes, thanks; that looks like it could be perfect.
Re: Custom compare function for array.sort on an integer array?
On 19.04.2011 16:56, Dmitry Olshansky wrote: If you are talking use std.algorithm sort Should be: "If your are talking about D2, then use std.algorithm sort ", ouch :) -- Dmitry Olshansky
Re: Custom compare function for array.sort on an integer array?
On 19.04.2011 16:57, Sequ wrote: Like the topic says, is it possible to set a custom compare function, for when you are using the 'sort' property of an integer array? I want the integers to be sorted by a different criteria than their natural order. From the documentation (http://d-programming-language.org/arrays.html) I can see how it would be done for structs or objects, but it doesn't seem to be possible for primitive types. If it can't yet be done, then I'm sure that adding the ability to give a comparison function or lazy expression to the 'sort' call would be very useful. Unless there is some reason that that would be beyond the scope of the 'sort' property's purpose? If you are talking use std.algorithm sort Like taken from docs below: bool myComp(int x,int y) {return x> y; } sort!(myComp)(array); See also: http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#sort -- Dmitry Olshansky
Custom compare function for array.sort on an integer array?
Like the topic says, is it possible to set a custom compare function, for when you are using the 'sort' property of an integer array? I want the integers to be sorted by a different criteria than their natural order. From the documentation (http://d-programming-language.org/arrays.html) I can see how it would be done for structs or objects, but it doesn't seem to be possible for primitive types. If it can't yet be done, then I'm sure that adding the ability to give a comparison function or lazy expression to the 'sort' call would be very useful. Unless there is some reason that that would be beyond the scope of the 'sort' property's purpose?
case statement allows for runtime values, a case of accepts-invalid?
int foo(ref int y) { y = 5; return y; } void main() { int x = 1; int y = 2; switch (x = foo(y)) { case y: writeln("x == y"); default: } assert(x == 5); assert(y == 5); } According to the docs: The case expressions must all evaluate to a constant value or array, or a runtime initialized const or immutable variable of integral type. In fact if you try to add a constant, only then will you get an error: switch (x) { case y + 1: } Error: case must be a string or an integral constant, not y + 1 It will also error out if you try to use a field of a struct. Which leads me to believe the first case should not be allowed to compile. Thoughts?