May 02

On Thursday, 2 May 2024 at 07:06:34 UTC, Richard (Rikki) Andrew Cattermole wrote:

>
int adder(int a, int b) {
	alias Perform = a + b;
	return Perform;
}

This is far more expected as a feature (I have tried to do simple aliasing of variables with it in the past).

It basically offers lazy expression inlining.

ref perform() => a + b;
return perform;
May 02
Sure, they're all different.

> in C#, to pass by reference, you have to put ref also before the argument in the function call

That makes refactoring much more work. By putting ref at the function declaration, you can switch it on and off without having to change anything else.

C++ can have refs to null:

```
int *p = NULL;
int& r = *p;
```

You're not supposed to do that, but compilers don't detect it.

As for Zig, in D a ref is a single element pointer, a [] is a pointer to an array.

A special pointer to a 0 terminated array does not confer any useful semantics I can think of - one could do it in D by just making the pointer a field in a struct and overload the operators.

Essentially, we're in good shape here.
May 05
On 5/2/24 08:51, Walter Bright wrote:
>> (Of course, it would be even better if fields could also be `ref`, but then you get into initialization safety. This is a general soundness problem in the current language though.)
> 
> `ref` is not rebindable, so a `ref` field means that support is needed in the constructor or default initializer, which needs some investigation on how to do it right.

Yes, exactly. My point was we need to think about that anyway. There are other things that ought not be rebindable:

```d
@safe:
class S{
    immutable int x;
    this(int y){
        foo();
        x=y;
        foo();
    }
    static int[immutable(int)*] t;
    void foo(){
        if(&x in t) assert(t[&x]==x); // error, immutable data modified
        t[&x]=x;
    }
}

void main(){
    auto s=new S(2);
}
```

Of course, with `ref` there is an additional challenge: it cannot be default-initialized.
May 05
On 5/2/24 08:58, Walter Bright wrote:
> I forgot to mention: ref's are not rebindable:
> 
> ```
> void test(int i, int j)
> {
>      ref int r = i; // r cannot be rebound to j
> }
> ```
> This has an interesting consequence. The lifetime of the ref will always be less than the lifetime of what it was bound to.
> 

Well, not always, as not every lifetime in a program follows lexical scoping.

```d
Array!int a;
a ~= 1;
ref x = a[0];
a.length = 0;
// (`x` still alive here, but `a[0]` is not)
```

(But this is not `@safe` code.)
1 2 3 4 5
Next ›   Last »