Re: opEquals nothrow

2017-07-20 Thread w0rp via Digitalmars-d-learn

On Thursday, 20 July 2017 at 15:10:24 UTC, Aldo wrote:
On Thursday, 20 July 2017 at 14:59:50 UTC, Steven Schveighoffer 
wrote:

On 7/20/17 10:38 AM, Aldo wrote:

Hello,

im tring to add nothrow keyword in my code, but compilation 
fails :


function 'object.opEquals' is not nothrow


its a simple comparison between 2 objects. How to make 
opEquals nothrow ?


You can't. Object.opEquals is not nothrow, so object.opEquals 
is not nothrow (note the former is the virtual member 
function, the latter is a global function which is what the 
compiler actually calls).


It is a legacy limitation. Until we get rid of all the Object 
base methods for things like opEquals and toHash, we will not 
be able to fix this.


-Steve


Im using DerelictGLFW3, to process events im doing this :

glfwSetMouseButtonCallback(window, );

onMouseClick function must be nothrow.

But now I can't do anything in this function because I can't 
convert my code to nothrow.


Can I put a try catch in the body ?

extern(C) nothrow
{
void onMouseClick(GLFWwindow* window, int button, int 
action, int d)

{
   try
   {
   // my code
   }
   catch
   {

   }
}
}

it seems its working but what about performances ?

thanks


You could also try assumeWontThrow. 
https://dlang.org/library/std/exception/assume_wont_throw.html


Re: Possible to write a classic fizzbuzz example using a UFCS chain?

2015-04-28 Thread w0rp via Digitalmars-d-learn

On Tuesday, 28 April 2015 at 10:46:54 UTC, Gary Willoughby wrote:

After reading the following thread:

http://forum.dlang.org/thread/nczgumcdfystcjqyb...@forum.dlang.org

I wondered if it was possible to write a classic fizzbuzz[1] 
example using a UFCS chain? I've tried and failed.


[1]: http://en.wikipedia.org/wiki/Fizz_buzz


You can do this.

import std.range : iota;
import std.algorithm : map, each;
import std.typecons : Tuple, tuple;
import std.stdio : writeln;

Tuple!(size_t, string) fizzbuzz(size_t number) {
if (number % 3 == 0) {
if (number % 5 == 0) {
return tuple(number, fizzbuzz);
} else {
return tuple(number, fizz);
}
} else if (number % 5 == 0) {
return tuple(number, buzz);
}

return tuple(number, );
}

void main(string[] argv) {
iota(1, 101)
.map!fizzbuzz
.each!(x = writeln(x[0], : , x[1]));
}

The desired output may vary, depending on variations of FizzBuzz. 
(Some want the number always in the output, some don't.) You 
could maybe do crazy ternary expressions instead of writing a 
function, or put it directly in there as a lambda, but both just 
look ugly, and you might as well just write a function for it.


Re: Static if to compare two types are the exact same

2015-04-07 Thread w0rp via Digitalmars-d-learn

On Tuesday, 7 April 2015 at 06:37:50 UTC, Jonathan wrote:

static if (is(T == V))


Are static ifs always checked outside of runtime? Is it 
possible for a static if condition to be undeterminable outside 
of runtime, or would such a condition throw a compiler error?


'static if' is always run at compile time, so it needs access to 
compile time information. Fortunately, you can access quite a lot 
at compile time in D.


Re: Binary search in structs

2015-04-05 Thread w0rp via Digitalmars-d-learn

On Sunday, 5 April 2015 at 23:06:27 UTC, FreeSlave wrote:
I have array of structs sorted by specific field. How can I 
perform binary search using this field as a key?


Currently I ended up with this, but it gives error:

struct S
{
int i;
string s;
}

import std.range;

void main(string [] args)
{
S[] structs = [{1,hello}, {2,world}, {3, !}]; 
//sorted by i


auto sortedRange = assumeSorted!(function bool(ref const S 
item, int needle) {

return item.i  needle;
})(structs);

sortedRange.trisect(2); //compilation error
}


I believe you have to pass trisect a value of S. So S(2, ) 
would do here, I suppose.


Re: @trusted and return ref

2015-03-03 Thread w0rp via Digitalmars-d-learn
On Wednesday, 25 February 2015 at 06:48:17 UTC, Ola Fosheim 
Grøstad wrote:

On Tuesday, 24 February 2015 at 22:49:17 UTC, w0rp wrote:
In general, @trusted means I have proven myself that this 
code is actually safe, eeven though it uses unsafe features. 
The compiler has to be pessimistic and assume that everything 
which can be used unsafely will be used unsafely. @trusted, as 
it is used here, is used to say, I assure you I have used 
this in a safe manner.


From http://dlang.org/function.html#trusted-functions :

«Trusted functions are guaranteed by the programmer to not 
exhibit any undefined behavior if called by a safe function.»


I take this to mean that anything that is wrapped up in 
@trusted should not violate memory safety when in injected into 
any arbitrary context marked as @safe.


The key phrase is guaranteed by the programmer. Which means 
that the programmer, not the compiler, is providing a guarantee 
that calling a @trusted function will not violate memory safety. 
If the programmer cannot make that guarantee, the function should 
be marked as @system instead. It's a mechanism which allows 
humans to achieve something the compiler isn't capable of 
achieving, at least at this point in time.


Much in the same way that a compiler cannot prove in general that 
programs will terminate, it can be very difficult for a compiler 
to prove that your program will not violate memory safety when 
the language is capable of calling into C code, etc. If you don't 
have an annotation like @trusted, the amount of code which could 
be run from @safe functions would be very small indeed.


Re: @trusted and return ref

2015-02-24 Thread w0rp via Digitalmars-d-learn
On Tuesday, 24 February 2015 at 22:37:58 UTC, Ola Fosheim Grøstad 
wrote:
If this is careful use of @trusted, then I don't see the point 
of having @trusted at all. What is the purpose? What is it 
meant to cover? In order for @trusted to make sense in this 
code segment ( http://dpaste.dzfl.pl/f3d854feede9 ) I would say 
that the whole class will have to be marked @trusted. Is that 
possible?


In general, @trusted means I have proven myself that this code 
is actually safe, eeven though it uses unsafe features. The 
compiler has to be pessimistic and assume that everything which 
can be used unsafely will be used unsafely. @trusted, as it is 
used here, is used to say, I assure you I have used this in a 
safe manner.


I would like to see @trusted blocks, although I can see Andrei's 
argument of not making it easy to do intentionally.


Re: dmd dub from git master

2014-07-06 Thread w0rp via Digitalmars-d-learn
I hit this myself also. I was trying to use the master branch 
DMD, druntime, and phobos with a recent vibe.d for building the 
documentation with ddox, and I ran into the reliance on 
std.metastrings.


Re: '!' and naming conventions

2014-06-18 Thread w0rp via Digitalmars-d-learn

On Wednesday, 18 June 2014 at 20:55:36 UTC, cym13 wrote:

Hello,

I see a lot of functions and other stuff with a '!' in the name
such as 'bitfields!' or 'ctRegex!'. What does it mean exactly? 
In

scheme, we use such a convention to warn that a function is not
pure, but I don't see the point of using it that way in D as
there are other way to express it.

Moreover, I'm looking for a style guide of D, something like the
PEP8 for python. Is there anything like that?

Thanks!


! is actually the way in D of passing compile time arguments. So 
take for example the 'to' function.


double foo = 3.14;
// Convert a double to a string.
string bar = foo.to!string;
// Same as the above, written a little differently.
bar = to!(string)(foo);
// How C++ and friends would write it.
// bar = tostring(foo);


Creating ranges over mutable, const, or immutable data structures.

2014-05-24 Thread w0rp via Digitalmars-d-learn
I have been writing my own hashmap which can provide forward 
ranges usable in @safe pure nothrow functions, because it's going 
to be useful for creating graph data structures with the same. I 
came to writing my ranges and I figured out how to do everything 
right for just mutable hashmaps, but I have no idea how to manage 
the tail-const nature of ranges in a variety of combinations. I 
figured out that I need to satisfy the following constraints.


Create a mutable KeyRange over a map which forwards on the right 
constness for the key type, so the following must be true.


HashMap!(K, V).keys.front - K
const(HashMap!(K, V)).keys.front - const(K)
immutable(HashMap!(K, V)).keys.front - immutable(K)

I have encounted some difficulty in trying to write a range which 
does this.


Re: Creating ranges over mutable, const, or immutable data structures.

2014-05-24 Thread w0rp via Digitalmars-d-learn

On Saturday, 24 May 2014 at 18:01:43 UTC, Ali Çehreli wrote:

On 05/24/2014 10:02 AM, w0rp wrote:

 I have been writing my own hashmap which can provide forward
ranges
 usable in @safe pure nothrow functions, because it's going to
be useful
 for creating graph data structures with the same. I came to
writing my
 ranges and I figured out how to do everything right for just
mutable
 hashmaps, but I have no idea how to manage the tail-const
nature of
 ranges in a variety of combinations. I figured out that I
need to
 satisfy the following constraints.

 Create a mutable KeyRange over a map which forwards on the
right
 constness for the key type, so the following must be true.

 HashMap!(K, V).keys.front - K
 const(HashMap!(K, V)).keys.front - const(K)
 immutable(HashMap!(K, V)).keys.front - immutable(K)

 I have encounted some difficulty in trying to write a range
which does
 this.

How timely! :) Jonathan Crapuchettes talked about facing the 
same issue in his talk at DConf. Perhaps he will post his 
WrapMutability template here or you can wait for his talk on 
YouTube.


Ali


I thought someone would say something like that! I'll wait for 
the talk I suppose.


Re: Array!T and find are slow

2014-05-17 Thread w0rp via Digitalmars-d-learn
On Saturday, 17 May 2014 at 20:06:03 UTC, Jonathan M Davis via 
Digitalmars-d-learn wrote:
But I think that the correct solution is to improve the 
compiler with regards
to lazy. The fact that lazy is so slow is a serious problem, 
and enforce is
just one manifestation of it (albeit the worst because of how 
often it's

used).

- Jonathan M Davis


I haven't been bitten by the performance of lazy myself, but I'd 
+1 that, because of how easy it can make writing code.


Re: What does it mean for opCmp and opEquals to be consistent?

2014-04-03 Thread w0rp

On Thursday, 3 April 2014 at 10:42:33 UTC, monarch_dodra wrote:
A correctly implemented AA would use opCmp to store objects in 
each bucket in cases of hash collisions, but still use opEqual 
in case of equivalence.


I would add to that, try to use opCmp if it is available. It 
should be possible to hash something if it has toHash and 
opEquals, and not require opCmp for hashing.


Re: How to repeat a function call?

2014-04-02 Thread w0rp
tuples are definitely a compile-time job. You could do something 
like this to build an N tuple by calling a function N many times.


---
import std.typecons;

int foo() {
return 3;
}

auto initTuple(size_t N, alias func)() {
string magic() {
string result = return tuple(;

foreach(i; 0..N) {
result ~= func(),;
}

result ~= );;

return result;
}

mixin(magic());
}

void main(string[] argv) {
enum Tuple!(int, int, int) tup = initTuple!(3, foo);
}

---

This just builds the tuple by building up a mixin string which 
calls the function so many times, with no guarantees about the 
order of arguments. If you have side-effects in your function 
though, you probably want to move it runtime anyway and use a 
range with the 'repeat' function, etc.


Re: Getting a class' name, but not fully qualified?

2014-03-28 Thread w0rp

You could do the following.

class Klass {
static class SubKlass {
}
}

string baseName(ClassInfo classinfo) {
import std.array;
import std.algorithm : countUntil;
import std.range : retro;

string qualName = classinfo.name;

size_t dotIndex = qualName.retro.countUntil('.');

if (dotIndex  0) {
return qualName;
}

return qualName[$ - dotIndex .. $];
}

string classBaseName(Object instance) {
if (instance is null) {
return null;
}

return instance.classinfo.baseName;
}

void main(string[] argv) {
import std.stdio;

writeln(Klass.classinfo.baseName);
writeln(Klass.SubKlass.classinfo.baseName);

Object x = new Klass();
Object y = new Klass.SubKlass();

writeln(x.classBaseName);
writeln(y.classBaseName);
}

The above won't result in any extra allocation and there are two 
functions so you could find a class name both at compile time and 
a runtime without too much trouble.


Re: Getting a class' name, but not fully qualified?

2014-03-28 Thread w0rp

On Friday, 28 March 2014 at 20:47:39 UTC, JR wrote:

On Friday, 28 March 2014 at 13:42:43 UTC, w0rp wrote:

   size_t dotIndex = qualName.retro.countUntil('.');

   if (dotIndex  0) {


size_t is unsigned. :3

(So ptrdiff_t -- or simply auto.)


Oh yes, that is a bug. There's always at least one bug in an 
example.


Re: enum VS static immutable

2014-03-13 Thread w0rp

On Thursday, 13 March 2014 at 14:38:27 UTC, ref2401 wrote:

Hi.
I have this structure:

struct MyStruct {
enum MyStruct VALUE = MyStruct(5f);

static immutable MyStruct value = MyStruct(5f);


float data;

this(float v) { data = v; }
}

What's the difference between MyStruct.VALUE and 
MyStruct.value? How should I decide what to use?


enum is a compile-time constant. 'static immutable' is an 
immutable (and therefore implicitly shared between threads...) 
runtime constant that isn't part of the struct itself. I'd stick 
with enum and switch to static when what you're writing becomes 
impossible to express at compile-time. A surprising number of 
things can actually be expressed at compile-time.


Re: Does string.isNumeric mean that parse!int will not throw?

2014-02-20 Thread w0rp

On Thursday, 20 February 2014 at 19:23:28 UTC, Cooler wrote:

On Thursday, 20 February 2014 at 19:18:15 UTC, Stanislav Blinov
wrote:

On Thursday, 20 February 2014 at 19:11:55 UTC, Cooler wrote:

The code:

string s = ...;
if(s.isNumeric){
  auto x = parse!int(s); // Can I be sure here that parse will
not throw?
}


No. s may still contain data that is not convertible to int. 
For

example, nan.


Is there any way to know that a string is convertible to a 
number

without throwing?


You could do this.

import std.stdio;
import std.algorithm: all;
import std.ascii: isDigit;

void main(string[] args) {
 writeln(123.all!isDigit); // true
 writeln(12x.all!isDigit); // false
}

Combine the all algorithm which returns true if everything in a
range matches a predicate and use isDigit as the predicate
which returns true if the character is an ascii digit.

if (s.length  0  s.all!isDigit) {
 // Never throws now.
 auto x = parse!int(s);
}


Re: Does string.isNumeric mean that parse!int will not throw?

2014-02-20 Thread w0rp

On Thursday, 20 February 2014 at 19:58:10 UTC, Stanislav Blinov
wrote:

On Thursday, 20 February 2014 at 19:46:35 UTC, w0rp wrote:


if (s.length  0  s.all!isDigit) {
// Never throws now.
auto x = parse!int(s);
}


And what about +/- and U/L suffixes? Or, say, different base 
(i.e. hexadecimal)?


It would be way more beneficial if Phobos' parse (or some 
additional function) returned some kind of Optional. Maybe a D 
implementation of Andrei's ExpectedT?


True, this doesn't cover those. Returning a
Maybe/Option/Nullable/Whatever type from another function may be
better.


Re: C++ library says to delete data that it allocates

2013-09-22 Thread w0rp

On Sunday, 22 September 2013 at 23:09:52 UTC, Charles Hixson
wrote:
I'm trying to use a C++ library that allocates data and returns 
a pointer to it.  It tells me that I should delete the data 
when I'm through with it.


Can I do this from within D?  Or do I need to write some C++ 
code to manage the delete, and pass the pointer on to it?


You can't use delete straight from D, so I'd write the delete in
C++ and pass the pointer back to C++, like you say.


Re: Can call static method with null reference

2013-06-20 Thread w0rp
You are invoking a function effectively stored statically in a 
class namespace. So you never actually dereference the null 
reference. You're just calling a function that doesn't really 
have anything to do with the reference. I prefer to tell it like 
it is and call the static method with the class name, so in this 
case that would be Foo.test1.


Re: Static member inside a class.

2013-06-13 Thread w0rp

On Thursday, 13 June 2013 at 19:47:23 UTC, Agustin wrote:
I would like to know if static members are shared between 2 
library. For example:


Class A
{
 static uint var;
}

From Library A:

A::var = 3;

From Library B:

if( A::var == 3 )
  ...

Its this possible? if not, its any way to make it happend?


The members are shared between different modules, yes. You use . 
instead of :: for scope resolution. You can also initialise a few 
things in a static constructors at class scope...


class A {
static uint var;

static this() {
var = 3;
}
}

Or at module scope...

class A {
static uint var;
}

static this() {
A.var = 3;
}

You should note that the data isn't shared between threads by 
default, but that's another detail. You can also usually produce 
a design that uses non-static data instead of static data.


Re: opEquals does not work?

2013-06-07 Thread w0rp

On Friday, 7 June 2013 at 20:51:13 UTC, Namespace wrote:
I got it. I must use Object instead of A... How ridiculous. I 
thought this was already fixed...


The class opEquals is quite like .equals in Java. You need to 
match the signature of Object, which is implicitly the base class 
of all classes. I think I recommend something like this.


override bool opEquals(Object o) const {
auto other = cast(A) o;

// It's probably easy to miss out !is null here, which 
matters.

return other !is null  this.id == other.id;
}


Re: opEquals does not work?

2013-06-07 Thread w0rp

On Friday, 7 June 2013 at 21:02:15 UTC, Namespace wrote:


Yes, I remember. As I heard it the first time I thought it's 
because of the limited compiler and that it will fixed some day.


opEquals for classes is interesting because you actually do need 
to write 'Object' there because of type polymorphism.


Re: opEquals does not work?

2013-06-07 Thread w0rp

On Friday, 7 June 2013 at 21:18:12 UTC, Jonathan M Davis wrote:


I have an open pull request as part of the move to getting rid 
of opEquals,
opCmp, toHash, and toString from Object, and it would make it 
so that you

could use something other than Object


Oh, that's cool. I didn't know about that.