Throw exception on segmentation fault in GNU/Linux

2014-04-22 Thread ilya-stromberg via Digitalmars-d-learn
What should I add in the D program in GNU/Linux to throw 
exception if I have segmentation fault error? I read somewhere 
that it's possible, but I don't know how to do it.


Re: Throw exception on segmentation fault in GNU/Linux

2014-04-22 Thread ilya-stromberg via Digitalmars-d-learn

On Tuesday, 22 April 2014 at 14:49:58 UTC, Dicebot wrote:

On Tuesday, 22 April 2014 at 09:58:45 UTC, ilya-stromberg wrote:
What should I add in the D program in GNU/Linux to throw 
exception if I have segmentation fault error? I read somewhere 
that it's possible, but I don't know how to do it.


etc.linux.memoryerror

Just remember that it is more of hack than reliable 
production-ready solution ;)


Thanks. How shall I use the module? Should I just import it or 
call any init function?


Re: Any library with OAuth support?

2014-01-29 Thread ilya-stromberg
On Wednesday, 22 January 2014 at 11:58:17 UTC, Rikki Cattermole 
wrote:
On Wednesday, 22 January 2014 at 11:14:22 UTC, ilya-stromberg 
wrote:

Do you know any library with OAuth support?


Not currently.
But I can add it to my todo list for Cmsed[0].

[0] https://github.com/rikkimax/Cmsed


Yes, it will be great.


Any library with OAuth support?

2014-01-22 Thread ilya-stromberg

Do you know any library with OAuth support?


Re: Any library with OAuth support?

2014-01-22 Thread ilya-stromberg
On Wednesday, 22 January 2014 at 14:54:00 UTC, Adam D. Ruppe 
wrote:
On Wednesday, 22 January 2014 at 11:14:22 UTC, ilya-stromberg 
wrote:

Do you know any library with OAuth support?


I did one extremely biased toward what I needed to do:


Do you have OAuth server implementation?


Any library with string encoding/decoding support?

2014-01-20 Thread ilya-stromberg
Do you know any library with string encoding/decoding support? I 
need more encodings than provides `std.encoding`.


Re: Compiling an app to a single binary - possible?

2013-11-17 Thread ilya-stromberg
On Sunday, 17 November 2013 at 18:40:34 UTC, Jacek Furmankiewicz 
wrote:
It is possible to import an entire folder and subfolders of 
assets? (e.g. static HTML with all of its pieces, i.e. CSS, 
JSS. etc)


Are we talking about Vibe.d? Yes, it's possible:
http://vibed.org/docs

shared static this()
{
auto router = new URLRouter;
router.get(*, serveStaticFiles(./public/));

listenHTTP(new HTTPServerSettings, router);
}


Re: Compiling an app to a single binary - possible?

2013-11-17 Thread ilya-stromberg
On Monday, 18 November 2013 at 00:12:57 UTC, Jacek Furmankiewicz 
wrote:
In this case is the content of ./public compiled directly 
into the executable (let's say the way all web resources are 
compiled into a Java WAR file), or does it have to be a 
separate folder on the filesystem, deployed alongside the main 
executable?


The ./public folder have to be a separate folder. All Diet 
templates in ./views folder compile directly into the 
executable.
See also The recommended structure for a project is about as 
follows: section.


Re: Efficient string concatenation?

2013-11-16 Thread ilya-stromberg
On Friday, 15 November 2013 at 23:51:42 UTC, Jacek Furmankiewicz 
wrote:
Thanks for the book! I printed it, all 673 pages of it. Immense 
work you have there.


I think it's good to listen a little critics from newcomers. I 
belive that it helps Ali Cehreli to improve the book.


Also, you can use `text` function from `std.conv`. It can convert 
any data to the `string`.


string newString = text(string1, 1, string2, 2, string3, 3);

Note that it use `~` operator, so `appender` can be faster.

P.S. We also have great book for D templates:
https://github.com/PhilippeSigaud/D-templates-tutorial


Re: What is the closest to ConcurrentHashMap and NavigableMap in Java?

2013-11-15 Thread ilya-stromberg
On Friday, 15 November 2013 at 15:21:59 UTC, Jacek Furmankiewicz 
wrote:

So what happens when the write operation is doing

 map[1] = map[1].editData(5);

and at the same time 50 threads are simultaneously reading the 
value in map[1]?.


Is that reassignment operation thread safe?
Or would I get corrupted reads with potentially a partially 
overriden value?


Jacek


Yes, this is thread safe.
Put attention to the `MyMap` class definition, it's marked as 
`synchronized`. It means that all class functions use the same 
Mutex. So, write operation will block map and all 50 threads 
will wait.


Re: What is the closest to ConcurrentHashMap and NavigableMap in Java?

2013-11-15 Thread ilya-stromberg
On Friday, 15 November 2013 at 16:36:56 UTC, Jacek Furmankiewicz 
wrote:
How can you achieve lock-free reads with the synchronized MyMap 
approach?


In this case you can use Readers-writer lock
http://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock

It allows multiple reads and single write. I think that the 
easiest way is use OS spesific function, for example 
`pthread_rwlock_t` for POSX. Note that D supports C ABI, so you 
can call any C function from D.


I don't know any D implementation of Readers-writer lock, but you 
can ask this question - maybe it already exist.


Re: What is the closest to ConcurrentHashMap and NavigableMap in Java?

2013-11-15 Thread ilya-stromberg

On Friday, 15 November 2013 at 17:09:54 UTC, Dicebot wrote:
On Friday, 15 November 2013 at 17:03:15 UTC, ilya-stromberg 
wrote:
I don't know any D implementation of Readers-writer lock, but 
you can ask this question - maybe it already exist.


http://dlang.org/phobos/core_sync_rwmutex.html


Thank you. I just never use it.


Re: What is the closest to ConcurrentHashMap and NavigableMap in Java?

2013-11-15 Thread ilya-stromberg

On Friday, 15 November 2013 at 17:46:41 UTC, Russel Winder wrote:
If D programmers are being told to use locks in applications 
code, then
the D programming model and library are failing. Or the advice 
is

wrong ;-)


It's possible to implement lock-free data structures in D, you 
can use core.atomic

http://dlang.org/phobos/core_atomic.html

But it's REALLY difficult to implement and it can be SLOWER than 
Mutex version (not only in D, it depends from usage situation).


Re: What is the closest to ConcurrentHashMap and NavigableMap in Java?

2013-11-15 Thread ilya-stromberg
On Friday, 15 November 2013 at 18:16:17 UTC, Jacek Furmankiewicz 
wrote:
taskPool looks like the closest equivalent in D that I could 
find.


Yes, that's sad truth: if you want to use D, be ready make 
something yourself.


BTW, why did you decide to migrate to D? Any problems with Java?


Re: What is the closest to ConcurrentHashMap and NavigableMap in Java?

2013-11-14 Thread ilya-stromberg
On Thursday, 14 November 2013 at 17:36:09 UTC, Jacek 
Furmankiewicz wrote:
In our Java code, we make heavy use of ConcurrentHashMap for 
in-memory caches:


Try to look dcollections:
http://www.dsource.org/projects/dcollections

Also, Vibe.d has own hashmap:
https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/utils/hashmap.d


Re: What is the closest to ConcurrentHashMap and NavigableMap in Java?

2013-11-14 Thread ilya-stromberg
On Thursday, 14 November 2013 at 20:00:10 UTC, Jacek 
Furmankiewicz wrote:
I looked at the dcollections docs, but none of their 
collections seem thread safe. The vibe.d I guess is because it 
is meant to be used from async I/O in a single thread...but 
once you add multi-threading to an app I am guessing it would 
not be usable.


No, you can:
1) Use different hashmap per tread. I don't know your situation, 
but it can be possible fo read-only cache like this:


import vibe.utils.hashmap;

HashMap!(int, int) map;

void foo()
{
   //use map
   map[1] = 1;
}

2) Use `shared` storage class and mutex like this:

import vibe.utils.hashmap;

shared HashMap!(int, int) map;

void foo()
{
   synchronized
   {
  //use map
  map[1] = 1;
   }
}


Re: What is the closest to ConcurrentHashMap and NavigableMap in Java?

2013-11-14 Thread ilya-stromberg
On Thursday, 14 November 2013 at 21:16:15 UTC, TheFlyingFiddle 
wrote:


If that is the case are you not limited in the way you can 
update the map eg only in a single block?


Yes, it's probably not the best example. It's valid if you have 
only 1 synchronized block for map. But you can use something like 
this:


void bar()
{
   synchronized(map)
   {
 map[1] = 1;
   }

   synchronized(map)
   {
 map[2] = 2;
   }
}

Or this:

//Note: valid only if you have 1 function that use map
synchronized void bar()
{
 map[1] = 1;

 map[2] = 2;
}

Or this:

shared myMap;

synchronized class MyMap
{
   HashMap!(int, int) map;

   void foo()
   {
  map[1] = 1;
   }

   void bar()
   {
  map[2] = 2;
   }
}

//init map
shared static this()
{
   myMap = new MyMap();
}

Probably, it's the best example.


Re: What is the closest to ConcurrentHashMap and NavigableMap in Java?

2013-11-14 Thread ilya-stromberg
On Thursday, 14 November 2013 at 21:31:52 UTC, Jacek 
Furmankiewicz wrote:
hashmap per thread is not an option. The cache may be a few GBs 
of data, there is no way we can duplicate that data per thread.


Not to mention the start up time when we have to warm up the 
cache.


How often do you change the data? Probably, you should use 
`immutable` variables.


Re: What is the closest to ConcurrentHashMap and NavigableMap in Java?

2013-11-14 Thread ilya-stromberg
On Thursday, 14 November 2013 at 22:12:10 UTC, Jacek 
Furmankiewicz wrote:
On Thursday, 14 November 2013 at 21:36:46 UTC, ilya-stromberg 
wrote:
On Thursday, 14 November 2013 at 21:31:52 UTC, Jacek 
Furmankiewicz wrote:
How often do you change the data? Probably, you should use 
`immutable` variables.


Customer specific. It may change once a year. It may change 
multiple times per second for a while, then nothing again for 
weeks.


Others may do mass loads of business rules, hence do mass 
changes every few hours.


Next to impossible to predict.


You can use `immutable` variables. It allows you to share the 
data without any synchronization. Like this:


class MyData
{
   int data1;
   string data2;

   //creates new object
   this(int data1, string data2)
   {
  this.data1 = data1;
  this.data2 = data2;
   }

   //modify the data
   immutable(MyData) editData(int i) const
   {
  //copy this object - we can't change immutable variables
  MyData dataCopy = new MyData(this.data1, this.data2)

  //modify the data copy
  dataCopy.data1 += i;

  //assume that `dataCopy` is immutable
  return cast(immutable(MyData)) dataCopy;
   }
}

shared myMap;

//map implementation
synchronized class MyMap
{
   HashMap!(int, immutable(MyData)) map;

   void foo()
   {
  map[1] = new immutable MyData(1, data);
   }

   void bar()
   {
  map[1] = map[1].editData(5);
   }
}

//init map
shared static this()
{
   myMap = new MyMap();
}

void main()
{
   myMap.foo();
   myMap.bar();
}


Re: D / GtkD for SQL Server

2013-10-31 Thread ilya-stromberg

On Sunday, 27 October 2013 at 00:06:35 UTC, John Joyus wrote:

On 10/25/2013 04:04 AM, Gary Willoughby wrote:


1). Does D has any support for MSSQL?


See here:
http://forum.dlang.org/thread/qcxoafwuachwnnwqk...@forum.dlang.org


Thanks for the link, but what I meant by MSSQL is Microsoft SQL 
Server. Not MySQL.


John, It's interesting if you can connect to the MS SQL.


Re: D / GtkD for SQL Server

2013-10-20 Thread ilya-stromberg

On Sunday, 20 October 2013 at 08:13:35 UTC, John Joyus wrote:
I am learning D and itching to create some small tools 
(basically Windows executables) for our internal use, but any 
tool I think of creating also needs some support for SQL 
Server! So my question is:


1). Does D has any support for MSSQL?


Look at the OpenDBX bindings:
https://github.com/rikkimax/Derelict3-Extras/tree/master/import/derelict/opendbx

It supports a lot of databases, including SQL Server.

Disclaimer: I didn't use it.


Re: CommonType and non-built-in types

2013-10-01 Thread ilya-stromberg

On Tuesday, 1 October 2013 at 17:56:17 UTC, Dicebot wrote:
Definition of common type is pretty simple - it is a type both 
types can be implicitly converted to. For `int` and `BigInt` 
common type should be `BigInt` if it was possible to define 
that implicit conversion. AFAIK it is not possible and thus 
they can't have common type.


Yes, we need implicit conversion support for usage cases like 
this.
May be only from build-in types to the user-defined as you said 
before, but it will be really useful.
I really don't know any usage example for implicit conversion 
from `int` to `BigInt` when it produces problems.


Re: User defined attributes use

2013-09-20 Thread ilya-stromberg

On Monday, 16 September 2013 at 07:36:13 UTC, simendsjo wrote:
I don't have a full example without adding a lot of code, but 
this partial

example might give you the gist of it.


// This is the type that validates
struct matches(string mustMatch)
{
alias re = ctRegex!(mustMatch);

static string[] validate(T)(const ref T t)
{
static if(!isSomeString!T)
static assert(0, matches only works on strings, 
not ~T.stringof);

return match(t, re).empty ? [no match] : null;
}
}

// and this is the code that runs all validators for a variable
void validate(alias T)(ref Appender!(string[]) app)
{
static if(isTupleWrapper!T)
{
validate!(T.Head)(app);
validate!(T.Tail)(app);
}
else
{
foreach(memberAttr; getValidaterAttrs!T)
{
foreach(attr; memberAttr.Tail)
{
foreach(msg; attr.validate(T))
if(msg.length)
app.put(msg);
}
}
}
}

// .. And here is some of the plumbing

string[] validate(Vars...)()
{
auto app = appender!(string[])();
validate!Vars(app);
return app.data();
}


// The getMembersAndAttributesWhere are templates in my little 
library that isn't released. Uses quite some custom __traits 
stuff, but it's basically __traits(getAttributes

template getValidaterAttrs(alias T)
{
alias getValidaterAttrs = 
TypeTuple!(getMembersAndAttributesWhere!(T, 
isValidationAttr).Elements,
 
getMembersAndAttributesWhere!(TypeOf!T, 
isValidationAttr).Elements);

}

// Well.. Incomplete
template isValidationAttr(alias T)
{
enum isValidationAttr = hasMember!(TypeOf!T, validate);
}


Can I explicitly specify when I can use attribute? Something like
this:

@attribute(field)
struct matches(string mustMatch)
{
}

string wrongAttribute
{
}

class Foo
{
@matches([0-9]+)
string someNumber; //OK, it's a field
}

@matches([0-9]+) //Error, it's a class, not a field
class Bar
{
}

@wrongAttribute //Error, this attribute doesn't exist
class C
{
}


Re: User defined attributes use

2013-09-16 Thread ilya-stromberg

On Sunday, 15 September 2013 at 18:31:40 UTC, simendsjo wrote:

On Sunday, 15 September 2013 at 17:34:06 UTC, matovitch wrote:

Hi everyone,

I read the documentation about user defined attributes, but I 
don't see their uses. Ok, it'a a template expression you can 
link to a declaration, but what are they useful for ? (not 
sure about the syntax ;-))


Can you declare a template constraint as a user defined 
attribute to do something like :


void 
template_function_which_go_back_and_forth(@(Bidirectional) 
@(Range) BR)(BR br) {...}


This would be awesome (even if not really occidental) to do 
something like:


@(SmallTypeSet) @(MediumTypeSet) @(LargeTypeSet) Type

This could allow to build tree based category structure.


It enables declarative programming.
And because this is D, there is no runtime overhead.
A common use is to add semantics to types and instances that is 
difficult or very intrusive to do by creating structs/classes 
by hand.


A little validation example:

@nonNull // An instance shouldn't be allowed to be null
class C {
  @matches([0-9]+)
  string someNumber;

  @interval!(](0, 10) // (0, 10] range
  int someInt;
}

C c;
validate(c); // returns [C is null, someNumber doesn't match 
'[0-9]+', someInt is outside the interval '(0, 10]']


And ORMs usually use annotations:

@table(some_tablename)
class C {
  @id(id_field_name)
  int id;
}

Take a look at C# and Java libraries to see how many uses 
attributes/annotations - they are still quite new in D, so they 
are still underutilized.
A very big difference is of course that UDAs are available at 
compile time :)


Can you print a full examle? For example, can you implement 
matches UDA and validate function.
It's intresting how can I create new UDA and check if it's 
available for class/field.


Re: User defined attributes use

2013-09-16 Thread ilya-stromberg

On Monday, 16 September 2013 at 15:12:05 UTC, Maxim Fomin wrote:

On Monday, 16 September 2013 at 10:29:12 UTC, matovitch wrote:
All your examples are great, thank you ! Is there a way to 
omit validate such that the compiler would call it implicitly ?


For example :

class C {
 ...
}

void fun(@nonNull C c) {
 ...
};

C c;
fun(c);  //compilation error since C is null


No, this isn't doable with UDAs because what you want requires 
runtime check. It is doable using other language features.


It's intresting how can I check that pointer is not null at the 
compile time. Can you print a example, please?
I know that we can use contract programming, but it requires 
runtime check.


Re: User defined attributes use

2013-09-16 Thread ilya-stromberg

On Monday, 16 September 2013 at 17:50:16 UTC, Maxim Fomin wrote:
Ideally structs should have default constructors (hello to 
those who miss them - problem #2) which could initialize class 
instance.


Do you know why D structs don't have default constructors? I 
really miss.


Re: User defined attributes use

2013-09-16 Thread ilya-stromberg
On Monday, 16 September 2013 at 19:28:22 UTC, Andrei Alexandrescu 
wrote:

On 9/16/13 11:56 AM, Namespace wrote:
And I agree absolute, to disable default CTor's by struct's 
was a huge

mistake. But D is full of those. ;)


They are not disabled. It seems many people are having trouble 
with getting default constructors to evaluate code, so I assume 
you mean that. One possibility (or first step) would be to 
relax the language to allow CTFE-executable code in default 
constructors.


Yes, we REALLY need this. I know that we can init struct fields 
via user-defined value, but for many cases is not enough. And in 
that days I  remembered C++.


Buy the way, what does it mean They are not disabled?

struct Foo
{
int i = 5; //works
}

struct Bar
{
int i;

this()
{
i = 5;
}
}

DMD:
src/app.d(10): Error: constructor app.Bar.this default 
constructor for structs only allowed with @disable and no body


Re: User defined attributes use

2013-09-16 Thread ilya-stromberg

On Monday, 16 September 2013 at 20:16:45 UTC, Namespace wrote:
And maybe also for delete: we need something to delete the 
memory manually.


And we need built-in memory allocators, not only GC.


Support implicit conversion between types

2013-09-04 Thread ilya-stromberg

I have some code like this:

struct Foo
{
this(int i)
{
//do something useful
}
}

void bar(Foo f)
{
//do something else
}

void main()
{
Foo f = 5;//works

bar(f);//works

bar(Foo(5));//works

	bar(5);//Error: function app.bar (Foo f) is not callable using 
argument types (int)

}


D can't implicitly convert type int to type Foo, but 
constructor Foo(int) exists. Explicit conversion works fine.

What should I do to support this convertion implicitly?


Re: Support implicit conversion between types

2013-09-04 Thread ilya-stromberg

On Wednesday, 4 September 2013 at 19:44:17 UTC, Namespace wrote:


What's about:

void bar(int i) {
bar(Foo(i));
}

?


No, I wrote very simple example. I have 10 from types and a lot 
of different bar functions. Your way will be more painful then 
explicit conversion.


Re: Support implicit conversion between types

2013-09-04 Thread ilya-stromberg

On Wednesday, 4 September 2013 at 20:14:06 UTC, Kozzi wrote:

So you can use templates, something like this:


I know, it will work. But I really have **a lot of** different 
bar functions, so that way will be painful.


So, the question is: what should I add to Foo struct to allow 
implicit conversions from int to Foo?


How compile program with curl support?

2013-08-22 Thread ilya-stromberg

I try to compile program with curl support, but I have error:

import std.net.curl;

void main()
{
}

rdmd main.d
/usr/lib/x86_64-linux-gnu/libphobos2.a(curl.o): In function 
`_D3std3net4curl4Curl19_sharedStaticCtor34FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticCtor34FZv+0xf): 
undefined reference to `curl_global_init'
/usr/lib/x86_64-linux-gnu/libphobos2.a(curl.o): In function 
`_D3std3net4curl4Curl19_sharedStaticDtor35FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticDtor35FZv+0x5): 
undefined reference to `curl_global_cleanup'


I try to import libcurl.a library, the same problem:
rdmd -L/usr/lib/x86_64-linux-gnu/libcurl.a main.d
/usr/lib/x86_64-linux-gnu/libphobos2.a(curl.o): In function 
`_D3std3net4curl4Curl19_sharedStaticCtor34FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticCtor34FZv+0xf): 
undefined reference to `curl_global_init'
/usr/lib/x86_64-linux-gnu/libphobos2.a(curl.o): In function 
`_D3std3net4curl4Curl19_sharedStaticDtor35FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticDtor35FZv+0x5): 
undefined reference to `curl_global_cleanup'


What should I do to get curl support?
OS is Linux Ubuntu 12.10.


Re: How compile program with curl support?

2013-08-22 Thread ilya-stromberg

On Thursday, 22 August 2013 at 10:24:49 UTC, David wrote:

What should I do to get curl support?
OS is Linux Ubuntu 12.10.


Install libcurl-dev
http://packages.ubuntu.com/de/lucid/libcurl-dev

Add -L-lcurl to your commandline


Thanks for help. Correct answer was here:
http://forum.dlang.org/post/mailman.1089.1350735488.5162.digitalmar...@puremagic.com

$ dmd -L-lphobos2 -L-lcurl main.d

As I can see by google, this is common issue. How can we document 
the compilation proses?


Re: How compile program with curl support?

2013-08-22 Thread ilya-stromberg

On Thursday, 22 August 2013 at 13:20:44 UTC, evilrat wrote:

why do u link phobos when compiler do this for you?


Because without it doesn't work:

$ dmd -L-lcurl main.d
/usr/lib/x86_64-linux-gnu/libphobos2.a(curl.o): In function 
`_D3std3net4curl4Curl19_sharedStaticCtor34FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticCtor34FZv+0xf): 
undefined reference to `curl_global_init'
/usr/lib/x86_64-linux-gnu/libphobos2.a(curl.o): In function 
`_D3std3net4curl4Curl19_sharedStaticDtor35FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticDtor35FZv+0x5): 
undefined reference to `curl_global_cleanup'


Have you got any ideas why?!