Standardize base types for metaprogramming

Dukc ajieskola at gmail.com
Fri Apr 5 08:57:50 UTC 2024


On Thursday, 4 April 2024 at 20:14:54 UTC, Monkyyy wrote:
> On Wednesday, 3 April 2024 at 19:14:01 UTC, Dukc wrote:
>> On Thursday, 29 February 2024 at 21:16:32 UTC, monkyyy wrote:
>>> [...]
>>
>> I have some gripes with this list. For example, `""` is *not* 
>> a good "zero" value. If we define one, it should be defined 
>> only for types that have a literal zero value. That means ints 
>> and floats and other numeric types, nothing else.
>>
>> I'm more sympathetic to the general notion though. We could 
>> well have a property called somthing like `.validDefault` that 
>> would be 0 for floats, `'\0'` or `' '` as opposed to `'\xff'` 
>> for `char`s and so on. Maybe it should be undefined for 
>> classes though. There's not really a good `.validDefault` 
>> value for `object`, as a null reference is hardly any more 
>> valid than NaNs are. Same for pointers.
>>
>> The thing with metaprogramming is, you really can't be fully 
>> generic no matter the language rules. Even if you had a nice 
>> valid value for every built-in type, people will want to 
>> define types there can be no good values for. An extreme 
>> example is the bottom type. It can only ever be:
>> 1. A type system breaking unsafe value.
>> 2. Never instantiated in actual execution flow.
>
> I kinda want to drop all debates about details and just want 
> more options then .init and the very questionable set of float 
> and int not even matching.

It's okay at this phase, but if you'll write a DIP you will have 
to propose a set.

I think we need to discuss one thing here though. Do you propose 
that there are some sorts of "alternative .init"s that are 
universal to all D types? Or is it okay that all the new 
properties would work for only a subset of D types?

If the former, one option would be that when there is no sensible 
value for the type in question, the value would be bottom 
instead. For example,

```D
// at object.d

class Object {
   //
   // implementation...
   //

   enum Object zero = assert(0); // no sensible zero value for 
this type
}
```

This would mean we could still use `Object.zero` to satisfy the 
typechecker or to infer the `Object` type for a variable, but 
trying to actually instantiate it would be a runtime crash.







More information about the dip.ideas mailing list