Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I see the usefulness of spans, but I'm not sure if they fit well with the C model. Consider these cases:

    void foo1(char *p, size_t sz);
    void foo2(char p[2]);
If I understand correctly, your proposal applies to the first case. The two arguments could be reduced to one, which is currently not possible for the general case without generic or template structs, but that alone would in my view not be worth introducing fat data types to the standard. An implementation doesn't need the new type either, it can already bounds check deferences of the pointer against the lvalue it was created from[1].

For the second case, I agree that would be nice but it can be worked around by using a struct.

[1] "recalling that pointers are anchored to the lvalue from which they were created" Kell, Some Were Meant for C, 2017 (https://sjmulder.nl/dl/pdf/2017%20-%20Kell%20-%20Some%20Were...)



> which is currently not possible for the general case

Not sure what you're thinking, since it does not require templates or generics, and works for the general case. I know it does, because I implemented it for D without generics or templates.

> it can be worked around by using a struct

Yes, but nobody does since it is clumsy, hence all the array bounds overflows in C. Even tiny bits of syntactic sugar, which is what this is, can have dramatic and transformative effects.

> [1]

That inescapably makes all pointers fat pointers, which is far more overhead than what I proposed. My proposal does not have any unavoidable overhead.

--

As for implementation experience, the D community finds they work extremely well. It's much more than an "it would be nice" feature.


> Not sure what you're thinking, since it does not require templates or generics, and works for the general case. I know it does, because I implemented it for D without generics or templates.

This was in favour of your proposal, about how it isn't currently possible to implement this construct generically as a user of the language, hence, it would need to be implemented in the language itself.

I think I do see the point you're trying to make. However:

> Even tiny bits of syntactic sugar, which is what this is, can have dramatic and transformative effects.

This is true, but its use would come mainly from bounds checking at compile time and runtime (otherwise it's just the struct) which is hardly even done for regular arrays.


> its use would come mainly from bounds checking

That is where the memory safety comes from. But the use comes from things like:

1. strlen, strcat, etc., become obsolete, replaced by far more efficient code

2. looping over an array contents becomes straightforward

3. documenting the extra variable holding the dimension of the array becomes unnecessary

I.e. a host of benefits.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: