Re: GtkD help

2017-11-19 Thread Ivan Trombley via Digitalmars-d-learn

On Sunday, 19 November 2017 at 13:59:10 UTC, Mike Wey wrote:

On 18-11-17 22:57, Ivan Trombley wrote:

[...]


To change how a cell is rendered you will need to add a 
CellRenderer to the column, a CellRendererText would be used 
for rendering text and it has a foreground property to change 
the text color.


A small example on using CellRenderers can be found here: 
https://github.com/gtkd-developers/GtkD/blob/master/demos/gtkD/DemoMultiCellRenderer/DemoMultiCellRenderer.d


If the color needs to differ between the different rows you can 
use TreeViewColumn.addAttribute to map a property of an 
renderer to a value on the TreeModel.


If the color depends on the value of the cell 
TreeViewColumn.setCellDataFunc() is also an option.


Thanks. It looks like setCellDataFunc is what I need.


Re: GtkD help

2017-11-19 Thread Ivan Trombley via Digitalmars-d-learn

On Sunday, 19 November 2017 at 09:54:06 UTC, Antonio Corbi wrote:
On Saturday, 18 November 2017 at 22:31:15 UTC, Ivan Trombley 
wrote:
Any information about using gio.Settings would be really 
appreciated too.


Hi Ivan,

I would recommend you to search for information about Gtk under 
valadoc pages [1]. You'll get Vala syntax but doing the 
mental-mapping to D+GtkD is automatic, i.e TreeView [2] and 
Glib Settings [3].


A. Corbi

[1] https://valadoc.org/index.htm
[2] https://valadoc.org/gtk+-3.0/Gtk.TreeView.html
[3] https://valadoc.org/gio-2.0/GLib.Settings.html



Thanks. I'll give it a look.


Re: Template Question

2017-11-19 Thread Jiyan via Digitalmars-d-learn
On Sunday, 19 November 2017 at 19:42:02 UTC, Jonathan M Davis 
wrote:
On Sunday, November 19, 2017 19:25:40 Jiyan via 
Digitalmars-d-learn wrote:

[...]


Okay. For starters,

[...]


Ah ok thanks very much, this helped me a lot :)


Re: Template Question

2017-11-19 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 19 November 2017 at 19:31:53 UTC, Jiyan wrote:

Text X;


You still need to instantiate it, even with default args.

Text!() X;

will work



Re: Template Question

2017-11-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, November 19, 2017 19:25:40 Jiyan via Digitalmars-d-learn wrote:
> With working i mean that
> Text X;
> Doesnt compile!

Okay. For starters,

struct Text(T : char)
{
size_t _len;
T* _ptr;
}

is a template specialization, which means that it's only going to compile if
T is char or a type that implicitly converts to char. It's similar (but not
quite the same as) doing

struct Text(T)
if(is(T : char))
{
}

https://dlang.org/spec/template.html#TemplateTypeParameterSpecialization

I suspect that what you meant to do was

struct Text(T = char)
{
size_t _len;
T* _ptr;
}

which would make char the default value for T. However, not even that is
going to allow you to do

Text t;

There is no implicit instantiation of templated types. Having the default
value for T will allow you to do

Text!() x;

instead of

Text!char x;

but it won't allow you to do

Text x;

and there's no way to do that. Templated types must always be explicitly
instantiated. What you could do that would give you a similar effect would
be to do

struct TextImpl(T)
{
size_t len;
T* ptr;
}

alias Text = TextImpl!char;

and then when you use Text, it will be replaced with TextImpl!char, which
would allow you to do

Text x;

The only time that templates are ever implicitly instantiated is with
function templates where all of the template parameters can be inferred from
the function arguments. It never occurs with any other kind of template,
even if the template has no parameters.

- Jonathan M Davis



Re: Template Question

2017-11-19 Thread Jiyan via Digitalmars-d-learn
On Sunday, 19 November 2017 at 19:28:37 UTC, Jonathan M Davis 
wrote:
On Sunday, November 19, 2017 19:22:51 Jiyan via 
Digitalmars-d-learn wrote:

Hello,

i wanted to ask why this isnt working:

struct Text(T : char)
{
  size_t _len;
  T* _ptr;
}

Thanks :)


What about it isn't working? I think that you need to explain 
what you're trying to do and what you think that this code 
should be doing but isn't.


- Jonathan M Davis


Text X;

Here X would be an Object from type Text with T being char when 
i'm right.

But this doesn't compile!
Thanks :)


Re: Template Question

2017-11-19 Thread Jiyan via Digitalmars-d-learn

With working i mean that
Text X;
Doesnt compile!



Re: Inserting and removing elements from a sorted container

2017-11-19 Thread Dirk via Digitalmars-d-learn

Thank you very much for your thorough answer!

/Dirk


Re: Template Question

2017-11-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, November 19, 2017 19:22:51 Jiyan via Digitalmars-d-learn wrote:
> Hello,
>
> i wanted to ask why this isnt working:
>
> struct Text(T : char)
> {
>   size_t _len;
>   T* _ptr;
> }
>
> Thanks :)

What about it isn't working? I think that you need to explain what you're
trying to do and what you think that this code should be doing but isn't.

- Jonathan M Davis



Template Question

2017-11-19 Thread Jiyan via Digitalmars-d-learn

Hello,

i wanted to ask why this isnt working:

struct Text(T : char)
{
size_t _len;
T* _ptr;
}

Thanks :)


Re: Inserting and removing elements from a sorted container

2017-11-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, November 19, 2017 16:48:00 Dirk via Digitalmars-d-learn wrote:
> On Sunday, 19 November 2017 at 16:05:53 UTC, Jonathan M Davis
>
> wrote:
> > I'd suggest that you use std.container.rbtree..RedBlackTree. A
> > red-black tree exactly the sort of data structure that is
> > typically used in a sorted set.
> >
> > - Jonathan M Davis
>
> Thank you, i will look into it.
>
>
> I have a question related to ranges:
>
> An input range has an interface like this:
>
> struct inputRange
> {
>  void popFront();
>  @property bool empty();
>  @property int front();
> }
>
>
> So if i use a foreach loop to iterate over all elements of a
> range, i would expect the range to shrink for every iteration:
>
> auto list = SList!uint(1,2,3);
> auto range = list[].save();
> foreach( i; range ) {
>writeln(range);
> }
>
> Expected output:
> [1,2,3]
> [2,3]
> [3]
>
> But i get:
> [1,2,3]
> [1,2,3]
> [1,2,3]
>
> Why don't i see the expected output?

foreach(e; range)
{
...
}

gets lowered to something like

for(auto __range = range; !__range.empty; __range.popFront())
{
auto e = __range.front;
...
}

So, the range gets copied when it's used with foreach. foreach then consumes
the copy. If a range is a value type such that copying the range is
equivalent to calling save on a forward range, then using the range with
foreach implicitly saves it, and so you iterate over a separate copy, and if
you then iterate over the original, then it continues to iterate from
wherever it was. However, if the range is a reference type, then copying the
range just copies its reference rather than implicitly saving it, and when
you try to iterate over the original, it won't work, because it's empty. And
if the range is a pseudo-reference type such that copying the range does not
simply copy a reference, and copying the range is not the same as calling
save, then if you try to iterate over the original after copying the range
with foreach will result in really weird behavior.

So, in the general case, you should never use a range again after having
copied it. It will work in some cases, but in others, it will fail miserably
- potentially in very weird ways. So, if you want to iterate over a range
with foreach and then do something with the range afterwards, call save when
you pass it to foreach. e.g.

foreach(e; range.save)
{
...
}

though if you're just grabbing the range from a container, you can just use
the container in foreach. e.g.

foreach(e; container)
{
...
}

The compiler implicitly calls [] on the container to get its range. So, it
would be equivalent to

foreach(e; container[])
{
...
}

Also, I would point out that your use of save in your example doesn't really
make sense:

> auto list = SList!uint(1,2,3);
> auto range = list[].save();

list[] will give you a fresh range every time you call it. You only need to
be calling save if you already have a range and want an indepedent copy to
iterate over. e.g.

auto list = SList!uint(1, 2, 3);
auto range = list[];
range.popFront();
foreach(e; range.save)
writeln(e); // prints 2 and then 3
writeln(range.save); // prints [2. 3]

On a side note, you should be calling save without parens. In most cases,
with parens will work, but the range API specifically treats save like a
property (much as that doesn't really make sense, since save doesn't fit the
typical definition of what a property is), so it's technically possible for
someone to implement a range where save is a variable (which would be really
weird, but as long as copying it then resulted in range.save giving you a
range that was the same as the original range, it would work).

I wouldn't expect you to have problems calling save with parens in practice,
but it's best to use range primitives in the way that isInputRange,
isForwardRange, etc. test for them (the code that they test should be in the
docs). For other range primitives, it really does matter in practice (e.g.
empty is typically a function for most ranges but for infinite ranges, it's
an enum, so if you call it with parens, some ranges will fail to compile
with your code). So, it's best to get in the habit of calling them the same
way that the range API traits call them - which for forward ranges, means
front, empty, length, popFront(), and save.

- Jonathan M Davis



Re: Inserting and removing elements from a sorted container

2017-11-19 Thread Dirk via Digitalmars-d-learn
On Sunday, 19 November 2017 at 16:05:53 UTC, Jonathan M Davis 
wrote:


I'd suggest that you use std.container.rbtree..RedBlackTree. A 
red-black tree exactly the sort of data structure that is 
typically used in a sorted set.


- Jonathan M Davis


Thank you, i will look into it.


I have a question related to ranges:

An input range has an interface like this:

struct inputRange
{
void popFront();
@property bool empty();
@property int front();
}


So if i use a foreach loop to iterate over all elements of a 
range, i would expect the range to shrink for every iteration:


auto list = SList!uint(1,2,3);
auto range = list[].save();
foreach( i; range ) {
  writeln(range);
}

Expected output:
[1,2,3]
[2,3]
[3]

But i get:
[1,2,3]
[1,2,3]
[1,2,3]

Why don't i see the expected output?

Thank you,
Dirk


Re: Inserting and removing elements from a sorted container

2017-11-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, November 19, 2017 13:41:51 Dirk via Digitalmars-d-learn wrote:
> Hi!
>
> I want to add an uint into a container, but avoid duplicate
> uints, similar to a set<> from C++ STL.
>
> To find out if an uint is already present in the container, it
> would make sense if the container is sorted.
>
> This is some pseudo-D-code that should make clear what i want to
> do:
>
> auto sortedlist = SList!uint(1, 2, 3, 5);
> uint newValue = 4;
> bool appendValue = true;
> foreach( i; sortedlist[] ) {
>  if( value == i ) {
>  // value already in list
>  appendValue = false;
>  break;
>  }
>  if( i > newValue ) {
>  // value not there. Insert value in front of i
>  sortedlist.insertBefore(i, newValue);
>  appendValue = false;
>  }
> }
> if( appendValue )
>  sortedlist.append( newValue );
>
> Can someone help me?
>
> Thank you,
> Dirk

I'd suggest that you use std.container.rbtree..RedBlackTree. A red-black
tree exactly the sort of data structure that is typically used in a sorted
set.

- Jonathan M Davis



Re: How to define variadic delegate with a ref/out argument?

2017-11-19 Thread pham via Digitalmars-d-learn

On Friday, 17 November 2017 at 06:21:50 UTC, Jerry A. wrote:

On Friday, 17 November 2017 at 05:08:23 UTC, pham wrote:

struct DelegateList(Args...)
{
public:
alias DelegateHandler = void delegate(Args args) nothrow;

DelegateHandler[] items;

void opCall(Args args) nothrow
{
foreach (i; items)
i(args);
}
}

DelegateList!(string, int) list; // Compile OK so far
DelegateList!(string, int*) list2; // Compile OK so far
DelegateList!(string, ref int) list3; // Compile error -> How 
to make it work?


Thanks
Pham


The only way I know of is using  a template which behaves like 
a reference.
Which can be done with nullableRef I suppose. Haven't actually 
tried it.


import std.typecons : NullableRef, nullableRef;

DelegateList!(NullableRef!int)(nullableRef(some_int));


nullableRef is same as passing pointer. Using "ref" is stronger 
guarantee that the var is never be passed as null.


variadic parameter passed by "ref" should be supported

Thanks.
Pham




Re: GtkD help

2017-11-19 Thread Mike Wey via Digitalmars-d-learn

On 18-11-17 22:57, Ivan Trombley wrote:
I have this small application for viewing select log data from a certain 
game that I originally wrote in C++/Qt. For various reasons, I decided 
to rewrite this app in D using gtk-d. First, I have to say that the 
documentation for gtk-d is atrocious! However, I managed to cobble 
together enough information to get 80% of it done.


I would like to be able to controll how the cells in a TreeView are 
rendered (ie the text color used) but I'm not able to find any 
information about how to do this. Does any one have experience here? If 
so, can you please give me a clue?


To change how a cell is rendered you will need to add a CellRenderer to 
the column, a CellRendererText would be used for rendering text and it 
has a foreground property to change the text color.


A small example on using CellRenderers can be found here: 
https://github.com/gtkd-developers/GtkD/blob/master/demos/gtkD/DemoMultiCellRenderer/DemoMultiCellRenderer.d


If the color needs to differ between the different rows you can use 
TreeViewColumn.addAttribute to map a property of an renderer to a value 
on the TreeModel.


If the color depends on the value of the cell 
TreeViewColumn.setCellDataFunc() is also an option.


--
Mike Wey


Inserting and removing elements from a sorted container

2017-11-19 Thread Dirk via Digitalmars-d-learn

Hi!

I want to add an uint into a container, but avoid duplicate 
uints, similar to a set<> from C++ STL.


To find out if an uint is already present in the container, it 
would make sense if the container is sorted.


This is some pseudo-D-code that should make clear what i want to 
do:


auto sortedlist = SList!uint(1, 2, 3, 5);
uint newValue = 4;
bool appendValue = true;
foreach( i; sortedlist[] ) {
if( value == i ) {
// value already in list
appendValue = false;
break;
}
if( i > newValue ) {
// value not there. Insert value in front of i
sortedlist.insertBefore(i, newValue);
appendValue = false;
}
}
if( appendValue )
sortedlist.append( newValue );

Can someone help me?

Thank you,
Dirk


Re: Json

2017-11-19 Thread bauss via Digitalmars-d-learn

On Sunday, 19 November 2017 at 10:50:58 UTC, Mafi wrote:
On Saturday, 18 November 2017 at 14:25:58 UTC, kerdemdemir 
wrote:
I am using vibe.d's json(http://vibed.org/api/vibe.data.json/) 
module without a problem and really happy with it. There are 
also some 
examples(https://github.com/vibe-d/vibe.d/tree/master/examples/json). If you are using "dub" package manager it is also very easy to integrate vibe.d.


Well, this would pull the whole vibe.d into the executable, 
wouldn't it? I'll stick with stdx.data.json for now, which I 
have tried and it does work in my case. But I'll minimize the 
actual json-interaction code so I can switch out the lib at any 
time.


Thank you all! It's rather unfortunate that the (std lib) json 
situation is still not sorted out!


Well you could hand pick the two sub packages.

https://github.com/vibe-d/vibe.d/tree/master/data
https://github.com/vibe-d/vibe.d/tree/master/utils (data depends 
on this)




Re: Json

2017-11-19 Thread Mafi via Digitalmars-d-learn

On Saturday, 18 November 2017 at 14:25:58 UTC, kerdemdemir wrote:
I am using vibe.d's json(http://vibed.org/api/vibe.data.json/) 
module without a problem and really happy with it. There are 
also some 
examples(https://github.com/vibe-d/vibe.d/tree/master/examples/json). If you are using "dub" package manager it is also very easy to integrate vibe.d.


Well, this would pull the whole vibe.d into the executable, 
wouldn't it? I'll stick with stdx.data.json for now, which I have 
tried and it does work in my case. But I'll minimize the actual 
json-interaction code so I can switch out the lib at any time.


Thank you all! It's rather unfortunate that the (std lib) json 
situation is still not sorted out!


Re: GtkD help

2017-11-19 Thread Antonio Corbi via Digitalmars-d-learn
On Saturday, 18 November 2017 at 22:31:15 UTC, Ivan Trombley 
wrote:
Any information about using gio.Settings would be really 
appreciated too.


Hi Ivan,

I would recommend you to search for information about Gtk under 
valadoc pages [1]. You'll get Vala syntax but doing the 
mental-mapping to D+GtkD is automatic, i.e TreeView [2] and Glib 
Settings [3].


A. Corbi

[1] https://valadoc.org/index.htm
[2] https://valadoc.org/gtk+-3.0/Gtk.TreeView.html
[3] https://valadoc.org/gio-2.0/GLib.Settings.html