January 05, 2023
On Thursday, 5 January 2023 at 20:58:52 UTC, areYouSureAboutThat wrote:
>

This would the absolute best approach:

int[] myDynamicArray = [0, 1]; // your typical dynamic D array.

int[] myStaticArray = [0, 1].staticArray;
static assert(is(typeof(myStaticArray) == int[2]));


Is there any reason the compiler cannot be made to do this?
January 05, 2023
On Thursday, 5 January 2023 at 21:08:28 UTC, areYouSureAboutThat wrote:
>
> int[] myStaticArray = [0, 1].staticArray;
> static assert(is(typeof(myStaticArray) == int[2]));
>
>
> Is there any reason the compiler cannot be made to do this?

In your example, wouldn't myStaticArray be a slice?
January 05, 2023
On Thu, Jan 05, 2023 at 08:58:52PM +0000, areYouSureAboutThat via Digitalmars-d wrote: [...]
> But in that case, what are people fussing about. Just use that.
> 
> Why introduce nonsense such as this: [$] [..]  ???

As you said yourself later, syntax. ;-)


> Mmm .. maybe [?]   ??

`?` is the ternary operator, it's weird to use it in this context with a totally unrelated meaning.


> btw. I don't consider syntax as bikeshedding. As a programmer, nothing is more important to me than sensible (and predictable) syntax.

Syntax certainly has its place (I wouldn't ever want to use C++ template syntax again unless I'm forced to, for example), but semantics is far more important in a programming language than syntax.  The prettiest syntax in the world is worthless if it cannot express what I want to express in my code, or if it has weird semantics with strange corner cases that make my code hard to understand.


> However, instead of having to do this:
> 
> auto myArray = [0, 1].staticArray;
> 
> I would like the compiler to infer that I'm creating a staticArray using this:
> 
> int[] myArray = [0, 1].staticArray;
> 
> I don't see why it requires my to only ever use auto.

This does work:

	int[2] myArray = [0, 1].staticArray;

Though it does also defeat the purpose of .staticArray. :-D


> I don't like using auto here.

It's already obvious from the initializer what the type of myArray is, why would you want to repeat it?  I prefer my code to be DRY.  I almost never write:

	int[] x = [1, 2, 3];
	MyStruct s = MyStruct(1, 2, 3);
	MyClass obj = new MyClass;

Too much redundant information.  This is better:

	auto x = [1, 2, 3];
	auto s = MyStruct(1, 2, 3);
	auto obj = new MyClass;

The compiler can already figure out the types for me; let the machine do its job while I focus on more important things. Like actually solving the programming problem I set out to solve, for example.


T

-- 
LINUX = Lousy Interface for Nefarious Unix Xenophobes.
January 05, 2023
On Thu, Jan 05, 2023 at 08:53:55PM +0000, Dibyendu Majumdar via Digitalmars-d wrote:
> Dennis Ritchie proposed fat pointers for C in 1990.
> 
> https://github.com/kenmartin-unix/UnixDocs/blob/master/VaribleSized_Arrays_in_C.pdf

Wow. So it took 30 years (and who knows how many buffer overflow security holes) for people to realize that this might have been a good idea?


T

-- 
What did the alien say to Schubert? "Take me to your lieder."
January 05, 2023
On Thursday, 5 January 2023 at 21:18:00 UTC, IGotD- wrote:
> On Thursday, 5 January 2023 at 21:08:28 UTC, areYouSureAboutThat wrote:
>>
>> int[] myStaticArray = [0, 1].staticArray;
>> static assert(is(typeof(myStaticArray) == int[2]));
>>
>>
>> Is there any reason the compiler cannot be made to do this?
>
> In your example, wouldn't myStaticArray be a slice?

how can this possibly be misinterpreted??

int[] myStaticArray = [0, 1].staticArray;

the '.staticArray' provides all the context needed to understand what type is being created.

January 05, 2023
On Thu, Jan 05, 2023 at 10:03:10PM +0000, areYouSureAboutThat via Digitalmars-d wrote:
> On Thursday, 5 January 2023 at 21:18:00 UTC, IGotD- wrote:
> > On Thursday, 5 January 2023 at 21:08:28 UTC, areYouSureAboutThat wrote:
> > > 
> > > int[] myStaticArray = [0, 1].staticArray;
> > > static assert(is(typeof(myStaticArray) == int[2]));
> > > 
> > > 
> > > Is there any reason the compiler cannot be made to do this?
> > 
> > In your example, wouldn't myStaticArray be a slice?
> 
> how can this possibly be misinterpreted??
> 
> int[] myStaticArray = [0, 1].staticArray;
> 
> the '.staticArray' provides all the context needed to understand what type is being created.

`int[]` is a slice. The static array is written as `int[2]`.

This is valid (though not exactly a good idea):

	// Take a slice of a static array
	int[] myStaticArray = [0, 1].staticArray[];


T

-- 
Lottery: tax on the stupid. -- Slashdotter
January 05, 2023

On Sunday, 1 January 2023 at 20:04:13 UTC, Walter Bright wrote:

>

On 12/31/2022 10:33 AM, monkyyy wrote:

>

When adr is making a video game on stream and defines a vec2 with default initialized floats; it's a video game it should be fail-safe and init to 0 rather than have him take 10 minutes on stage debugging it. Different situations can call for different solutions, why is safety within computer science universally without context?

You're right that a video game need not care about correctness or corruption.

I don't think that's a very apt take at all. Frankly it's insulting. You do realize video games are a business, right? They absolutely care about correctness and corruption. On this specific issue, it so happens that developers also tend to find it very useful and common for numeric types to initialize to zero (when they are initialized at all). Which is why they find it very surprising and confusing when they suddenly don't. This should not be interpreted to mean that their industry is lazy and "doesn't care" about the financial viability of releasing sound code.

"If they really cared, they'd just initialize everything like they're supposed to" is not a well-aimed criticism coming from D (and the same argument applies to aviation; should they not initialize everything to NaN, just to be safe? Or do they not care?). C++ requires initialization, because depending on compiler there's a good chance your program will just catastrophically explode otherwise. Many other heavy lifting languages in the marketplace have adopted default/value initialization. D, for some reason, has decided to free the programmer from worry by adopting usable default initialization, and then turn around and give you a value you can't use, for certain arbitrary extremely common primitive types, but not all variables, in order to satisfy what is, in some estimations, a minority use case, thus making things more difficult for the majority who might expect consistency, both internal and external. D almost solves the first problem, then creates a new one.

Yes, I realize the NaN thing is an old dead horse and isn't going to change. I had not intended to make any posts complaining about "D leadership" as I've at times witnessed in this thread and others, as I ordinarily have no direct problems with, engagement with, or influence over it. But it's very troubling to suddenly see a mentality of "ah, who cares about those use cases? They don't care about writing real programs, they're not flying airplanes!" coming from the top.

Developers are not just hobbyists. There are careers, employees, and families in the mix. $180 billion dollar industry. Shigeru Miyamoto was not harmed by the fact Mario can clip through otherwise solid blocks due to mathematical insufficiencies in his 1985 video game. These days, a buggy mess means jobs are lost. Does D only ever see itself as a "hobby language" in this field then? Should that be how we treat it? I believe it has the potential to be much more, enough to the point I was willing to stake at least a portion of my livelihood on it. I took a chance--and still am--because I believed the good parts of D are so good, even in its current state, it's worth the potential risks to grow alongside it, and so far it has been. Not every studio or developer can be in the position to incur this cost of what is in some ways a show of faith. I don't want to see those risks expand and those years lead to an eventual dead end because the designers don't consider my career a respectable use of time or the language.

January 05, 2023
On Thursday, 5 January 2023 at 22:09:15 UTC, H. S. Teoh wrote:
>
> `int[]` is a slice. The static array is written as `int[2]`.
>
> This is valid (though not exactly a good idea):
>
> 	// Take a slice of a static array
> 	int[] myStaticArray = [0, 1].staticArray[];
>
>
> T

to be honest, I've never needed a static array in D.

I just let the gc take care of everything with []

so I'll leave the 'controversy' over this to those that do ;-)

in the meantime.. this is hardly .. hard:

auto myStaticArray = [0, 1].staticArray;

int[2] myStaticArray = [0, 1].staticArray;

people can still count.. can't they?
January 05, 2023
On Thu, Jan 05, 2023 at 10:35:37PM +0000, areYouSureAboutThat via Digitalmars-d wrote: [...]
> to be honest, I've never needed a static array in D.

:-D  I haven't really used static arrays very much myself, but occasionally they're useful.  E.g. in one project where I use a lot of 4-element int arrays, using static arrays for them reduces a lot of GC pressure, and also gives me nice by-value semantics (useful in certain contexts).


[...]
> in the meantime.. this is hardly .. hard:
> 
> auto myStaticArray = [0, 1].staticArray;
> 
> int[2] myStaticArray = [0, 1].staticArray;
> 
> people can still count.. can't they?

It's not so much about counting, it's about maintainability / mutability of the code.  For example, if you had a long static array like this:

	int[100] x = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
		43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
		103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157,
		163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223,
		227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277,
		281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
		353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
		421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479,
		487, 491, 499, 503, 509, 521, 523, 541 ];

if one day you decide that some elements have to be added/removed from this array (based on some arbitrary criteria), then you have to recount and update the length, which, for a long array, isn't a trivial effort. It's also not very DRY; the compiler can already figure this out for you, so why make the programmer repeat the same work?

Note that static arrays isn't just arrays of numbers, it could potentially be an array of some aggregate type with complex initializers that makes it annoying to have to recount every time you update it.


T

-- 
Жил-был король когда-то, при нём блоха жила.
January 05, 2023
On 1/5/2023 12:53 PM, Dibyendu Majumdar wrote:
> Dennis Ritchie proposed fat pointers for C in 1990.
> 
> https://github.com/kenmartin-unix/UnixDocs/blob/master/VaribleSized_Arrays_in_C.pdf

Wow, didn't know about that. Thanks!

The way D does it is better, though!