Re: Is it possible to do this with a template?

2021-12-17 Thread rempas via Digitalmars-d-learn

On Friday, 17 December 2021 at 13:00:55 UTC, Ali Çehreli wrote:

On 12/17/21 1:57 AM, bauss wrote:

> You can also do it as a normal template:
>
> ```d
> template is_same(alias value, T)
> {
>  enum is_same = is(typeof(value) == T);
> }
> ```

And even shorter by realizing that it's an eponymous template:

enum is_same(alias value, T) = is(typeof(value) == T);

Ali


Thanks guys! Yeah, I prefer Ali's shorter syntax!


Re: Is it possible to do this with a template?

2021-12-17 Thread rempas via Digitalmars-d-learn

On Friday, 17 December 2021 at 16:02:45 UTC, RazvanN wrote:


There is also a compiler trait [1] which can do that for you:

```d
void main()
{
int val = 10;
static if (__traits(isSame, typeof(val), int)) {}
}
```


[1] https://dlang.org/spec/traits.html#isSame


Thanks! The other options were simpler and faster to type tho


Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Tejas via Digitalmars-d-learn

On Friday, 17 December 2021 at 18:51:56 UTC, Ali Çehreli wrote:

On 12/17/21 10:01 AM, Tejas wrote:

> [...]
Storage,

There is no such requirement nor guarantee.

[...]


Well, I got completely mislead by my experiment 

```d
struct S
{
~this() immutable {}
}

void main()
{
immutable S s = S();
}
```
This failed, so I just came up with reasons to justify this 
behaviour


Thanks for correcting me 


Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Ali Çehreli via Digitalmars-d-learn

On 12/17/21 10:01 AM, Tejas wrote:

> I think since `immutable` objects are kept in Read Only Storage,

There is no such requirement nor guarantee.

> you
> can't call destructors on them

Destructor is nothing but a piece of code that is executed when an 
object's life ends. A destructor need not touch any member of the object:


struct S {
  ~this() {
import std.stdio;
writeln("done");
  }
}

void main() {
  immutable a = S();
  auto b = immutable(S)();
}

Both objects are immutable there yet their destructor is executed.

> since the objects don't get erased when
> `~this` is called, but rather they get assigned their `.init` value,

That's true only for destroy(), which gets called only if the programmer 
asks for it. Otherwise, destroyed objects don't get assigned any special 
value.


> which tells the GC that they can be collected.

That's not true. The GC collects objects when there are no references to 
them. The values of the object's members have nothing to do with it.


> `immutable class` has nothing to do with it, even the following fails to
> compile:
> ```d
>
>
>
> struct S
> {
>  ~this() immutable {}

That immutable qualifier means "this destructor is for immutable objects 
of this type." However, it seems impossible to define two destructors:


  ~this() {
writeln(__FUNCTION__);
  }

  ~this() immutable {
writeln(__FUNCTION__);
  }

Error: destructor `deneme.S.~this` conflicts with destructor 
`deneme.S.~this` at deneme.d(79)


I think this is an unexplored corner of the language. Part of the 
complication may be due to implementations by an earlier compiler 
contributor, who I heard was responsible for qualifiers on constructors. 
Note two different constructors here:


import std.stdio;

struct S {
  this(int) {
writeln(__PRETTY_FUNCTION__);
  }

  this(int) immutable {
writeln(__PRETTY_FUNCTION__);
  }
}

void main() {
  auto a = immutable(S)(0);
  auto b = S(1);
}

I bet the problem here is that the implementation in the compiler is 
half-baked on these qualifiers.


Ali



Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Tejas via Digitalmars-d-learn
On Friday, 17 December 2021 at 18:32:43 UTC, Denis Feklushkin 
wrote:

On Friday, 17 December 2021 at 18:02:52 UTC, Tejas wrote:

I improved your sample:

```d
immutable struct S
{
~this() {}
}

immutable struct S2
{
S sss;
~this() {}
}

void main()
{
S2 s = S2();
}
```

```
Error: `immutable` method `serializer_bug.S.~this` is not 
callable using a mutable object
Error: mutable method `serializer_bug.S2.~this` is not callable 
using a `immutable` object
serializer_bug.d(17,5):Consider adding `const` or 
`inout` here

```

immutable dtor can't be called at all?


Nope, seems to go against the very promise it's making

Labelling `~this()` as const or immutable means it won't affect 
the state of the object, but it will, by it's very nature.


That's why I said it's not too much of a stretch to imagine why 
they're disallowed entirely.


Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Denis Feklushkin via Digitalmars-d-learn

On Friday, 17 December 2021 at 18:02:52 UTC, Tejas wrote:

I improved your sample:

```d
immutable struct S
{
~this() {}
}

immutable struct S2
{
S sss;
~this() {}
}

void main()
{
S2 s = S2();
}
```

```
Error: `immutable` method `serializer_bug.S.~this` is not 
callable using a mutable object
Error: mutable method `serializer_bug.S2.~this` is not callable 
using a `immutable` object
serializer_bug.d(17,5):Consider adding `const` or `inout` 
here

```

immutable dtor can't be called at all?



Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Tejas via Digitalmars-d-learn
On Friday, 17 December 2021 at 18:19:34 UTC, Denis Feklushkin 
wrote:

On Friday, 17 December 2021 at 18:01:03 UTC, Tejas wrote:


I think since `immutable` objects are kept in Read Only Storage


Some of them can be stored in ROM in some cases, but actually 
"immutable" keyword means "not mutable for whole its lifetime"


Well, it would be really weird if destructors successfully 
executed for some class of `immutable` qualified objects but 
didn't for others.


Not too much of a stretch to imagine that destruction for 
immutable objects was outright disallowed.


Someone who can explain this behaviour more thoroughly would be 
much appreciated 


Maybe we should allow finalizers to mutate their instance?


Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Denis Feklushkin via Digitalmars-d-learn

On Friday, 17 December 2021 at 18:01:03 UTC, Tejas wrote:


I think since `immutable` objects are kept in Read Only Storage


Some of them can be stored in ROM in some cases, but actually 
"immutable" keyword means "not mutable for whole its lifetime"


Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Tejas via Digitalmars-d-learn

On Friday, 17 December 2021 at 18:01:03 UTC, Tejas wrote:
On Friday, 17 December 2021 at 17:34:05 UTC, Denis Feklushkin 
wrote:
On Friday, 17 December 2021 at 17:27:53 UTC, Denis Feklushkin 
wrote:



[...]


("serializer_bug" is just name of my local .d file)


I think since `immutable` objects are kept in Read Only 
Storage, you can't call destructors on them since the objects 
don't get erased when `~this` is called, but rather they get 
assigned their `.init` value, which tells the GC that they can 
be collected.


`immutable class` has nothing to do with it, even the following 
fails to compile:

```d



struct S
{
~this() immutable {}
}

void main()
{
S s = S();
}

Error: `immutable` method `onlineapp.S.~this` is not callable 
using a mutable object

```


Correction:
`immutable S s = S();` inside the `void main()`


Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Tejas via Digitalmars-d-learn
On Friday, 17 December 2021 at 17:34:05 UTC, Denis Feklushkin 
wrote:
On Friday, 17 December 2021 at 17:27:53 UTC, Denis Feklushkin 
wrote:


~this() {} // Comment out this to fix this compilation  
error:

// Error: `immutable` method `serializer_bug.Imm.~this` is


("serializer_bug" is just name of my local .d file)


I think since `immutable` objects are kept in Read Only Storage, 
you can't call destructors on them since the objects don't get 
erased when `~this` is called, but rather they get assigned their 
`.init` value, which tells the GC that they can be collected.


`immutable class` has nothing to do with it, even the following 
fails to compile:

```d



struct S
{
~this() immutable {}
}

void main()
{
S s = S();
}

Error: `immutable` method `onlineapp.S.~this` is not callable 
using a mutable object

```



Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Denis Feklushkin via Digitalmars-d-learn
On Friday, 17 December 2021 at 17:27:53 UTC, Denis Feklushkin 
wrote:


~this() {} // Comment out this to fix this compilation  
error:

// Error: `immutable` method `serializer_bug.Imm.~this` is


("serializer_bug" is just name of my local .d file)



This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Denis Feklushkin via Digitalmars-d-learn

```d
/+ dub.json:
{
  "name": "test",
  "dependencies": {
  }
}
+/

struct S
{
~this() {}
}

immutable class Imm
{
S s; // this is immutable value because whole class is 
immutable


this()
{
s = S();
}

~this() {} // Comment out this to fix this compilation  error:
// Error: `immutable` method `serializer_bug.Imm.~this` is 
not callable using a mutable object

// What mutable object is meant here?
}


void main()
{
auto ic = new immutable Imm();
}
```

Run: $ dub --single bug.d


Re: Is it possible to do this with a template?

2021-12-17 Thread RazvanN via Digitalmars-d-learn

On Friday, 17 December 2021 at 07:52:18 UTC, rempas wrote:
I want to use an expression and put it in place inside the `if` 
parentheses. The expression is: `is(typeof(val) == type)`. I 
want to use a template called "is_same" that will take the 
value and a type to place them to the respective places. I have 
tried the following but it doesn't seem to work:


```
mixin template is_same(val, type) {
  is(typeof(val) == type)
}

void main() {
  int val = 10;
  static if (is_same!(val, int)) {}
}
```

When trying to compile, I'm taking the following error message:

```
Error: declaration expected, not `is`
```

Is this a limitation of templates in D or is there a way to 
bypass this?


There is also a compiler trait [1] which can do that for you:

```d
void main()
{
int val = 10;
static if (__traits(isSame, typeof(val), int)) {}
}
```


[1] https://dlang.org/spec/traits.html#isSame


Re: Is it possible to do this with a template?

2021-12-17 Thread Ali Çehreli via Digitalmars-d-learn

On 12/17/21 1:57 AM, bauss wrote:

> You can also do it as a normal template:
>
> ```d
> template is_same(alias value, T)
> {
>  enum is_same = is(typeof(value) == T);
> }
> ```

And even shorter by realizing that it's an eponymous template:

enum is_same(alias value, T) = is(typeof(value) == T);

Ali



Re: Is it possible to do this with a template?

2021-12-17 Thread bauss via Digitalmars-d-learn

On Friday, 17 December 2021 at 08:59:19 UTC, rempas wrote:

On Friday, 17 December 2021 at 08:44:39 UTC, Mitacha wrote:


It isn't really about limitation of templates. You're trying 
to use mixin template and it's main purpose is to inject 
declarations. If you want to replace `is expression` with 
template you could use something like this:


```d
bool is_same(alias value, T)() {
return is(typeof(value) == T);
}

void main() {
int value = 10;
static if (is_same!(value, int)) {
writeln("it is true!");
} else {
writeln("it is false!");
}
}
```


Oh! That's nice! I didn't even knew it was possible to create 
template functions like this! Thanks!


Personally, I don't see any benefit with replacing that kind 
of `is expressions` with templates. Perhaps I'm missing 
something :)


The benefits are typing less code and make it more readable and 
easy on the eyes ;)


You can also do it as a normal template:

```d
template is_same(alias value, T)
{
enum is_same = is(typeof(value) == T);
}
```


Re: Is it possible to do this with a template?

2021-12-17 Thread rempas via Digitalmars-d-learn

On Friday, 17 December 2021 at 08:44:39 UTC, Mitacha wrote:


It isn't really about limitation of templates. You're trying to 
use mixin template and it's main purpose is to inject 
declarations. If you want to replace `is expression` with 
template you could use something like this:


```d
bool is_same(alias value, T)() {
return is(typeof(value) == T);
}

void main() {
int value = 10;
static if (is_same!(value, int)) {
writeln("it is true!");
} else {
writeln("it is false!");
}
}
```


Oh! That's nice! I didn't even knew it was possible to create 
template functions like this! Thanks!


Personally, I don't see any benefit with replacing that kind of 
`is expressions` with templates. Perhaps I'm missing something 
:)


The benefits are typing less code and make it more readable and 
easy on the eyes ;)




Re: Is it possible to do this with a template?

2021-12-17 Thread Mitacha via Digitalmars-d-learn

On Friday, 17 December 2021 at 07:52:18 UTC, rempas wrote:
I want to use an expression and put it in place inside the `if` 
parentheses. The expression is: `is(typeof(val) == type)`. I 
want to use a template called "is_same" that will take the 
value and a type to place them to the respective places. I have 
tried the following but it doesn't seem to work:


```
mixin template is_same(val, type) {
  is(typeof(val) == type)
}

void main() {
  int val = 10;
  static if (is_same!(val, int)) {}
}
```

When trying to compile, I'm taking the following error message:

```
Error: declaration expected, not `is`
```

Is this a limitation of templates in D or is there a way to 
bypass this?


It isn't really about limitation of templates. You're trying to 
use mixin template and it's main purpose is to inject 
declarations. If you want to replace `is expression` with 
template you could use something like this:


```d
bool is_same(alias value, T)() {
return is(typeof(value) == T);
}

void main() {
int value = 10;
static if (is_same!(value, int)) {
writeln("it is true!");
} else {
writeln("it is false!");
}
}
```
Personally, I don't see any benefit with replacing that kind of 
`is expressions` with templates. Perhaps I'm missing something :)