August 02, 2010 Re: null dereference exception vs. segfault? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 08/03/2010 12:32 AM, bearophile wrote:
> Pelle:
>> I think a good thing would be NonNull!T, but I haven't managed to create
>> one. If this structure exists and becomes good practice to use, maybe we
>> can get the good syntax in D3. In 20 years or so :P
>
> Maybe we are talking about two different things, I was talking about nonnull class references/pointers, you seem to talk about nullable values :-) Both can be useful in D, but they are different things.
> Nullable values are simpler to design, they are just wrapper structs that contain a value plus a boolean, plus if you want some syntax sugar to manage them with a shorter syntax.
>
> Bye,
> bearophile
I am talking about non-nullable references indeed. I don't think I mentioned nullable types, really.
I also created this, as the simplest NotNull-type concievable:
struct NotNull(T) if(is(typeof(T.init !is null))) {
private T _instance;
this(T t) {
enforce(t !is null, "Cannot create NotNull from null");
_instance = t;
}
T get() {
assert (_instance !is null,
text("Supposed NotNull!(", T.stringof, ") is null"));
return _instance;
}
alias get this;
}
This has the obvious bug in that you can declare a nonnull without an initializer and get a null from it. If we ever get @disable this(){} for structs, this struct can become better.
I'll probably try it out in some code.
|
August 02, 2010 Re: null dereference exception vs. segfault? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pelle | Pelle: > struct NotNull(T) if(is(typeof(T.init !is null))) { Is this enough? struct NotNull(T) if (is(T.init is null)) { > this(T t) { > enforce(t !is null, "Cannot create NotNull from null"); enforce() is bad, use Design by Contract instead (a precondition with an assert inside). Bye, bearophile |
August 02, 2010 Re: null dereference exception vs. segfault? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > Is this enough?
> struct NotNull(T) if (is(T.init is null)) {
Sorry, I meant:
struct NotNull(T) if (T.init is null) {
Bye,
bearophile
|
August 02, 2010 Re: null dereference exception vs. segfault? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 08/03/2010 01:08 AM, bearophile wrote:
> Pelle:
>
>> struct NotNull(T) if(is(typeof(T.init !is null))) {
>
> Is this enough?
> struct NotNull(T) if (is(T.init is null)) {
>
>
>> this(T t) {
>> enforce(t !is null, "Cannot create NotNull from null");
>
> enforce() is bad, use Design by Contract instead (a precondition with an assert inside).
>
> Bye,
> bearophile
If NotNull will be in a library, it should probably use enforce, if I have understood things correctly. External input, and all that. I think most of phobos does it like this currently.
|
August 02, 2010 Re: null dereference exception vs. segfault? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pelle | Pelle:
> If NotNull will be in a library, it should probably use enforce, if I have understood things correctly. External input, and all that. I think most of phobos does it like this currently.
I suspect that Andrei has still to "get" DbC :-) (And your lib is not Phobos.)
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation