HaxeFoundation / haxe.org-comments

Repository to collect comments of our haxe.org websites
2 stars 2 forks source link

[code.haxe.org] Beginner - Using arrays #6

Open utterances-bot opened 5 years ago

utterances-bot commented 5 years ago

Using arrays - Beginner - Haxe programming language cookbook

In Haxe, the Array type represents a collection of elements ordered by their index (order number) in the collection.

https://code.haxe.org/category/beginner/arrays.html

chikega commented 2 years ago

Please consider avoiding variable names (eg. strings) that resemble keywords (eg. String) which can lead to confusion for beginners. The first example is much more clear when 'strings' is replaced with 'fruits', which is more descriptive.

var fruits:Array<String> = ["Apple", "Pear", "Banana"];
trace(fruits);
Simn commented 2 years ago

Your confusion confuses me, String isn't even a keyword, it's just a type name. Avoiding these kinds of similarities for that reasons seems like a pointless endeavor to me.

... having said that, I still agree that fruits would be a better name here. And while we're at it, I don't like the Array<String> type-hint because it's not very haxe-idiomatic to have explicit types for local variables.

chikega commented 2 years ago

Maybe I should have said that 'String' is a 'reserved word or identifier' perhaps? This documentation is supposedly for beginners and I try to be mindful of what it was like when I started learning. An experienced programmer will have no problems sifting through the noise of 'strings' vs 'String', but it still creates noise when variable names and datatypes or identifiers resemble each other. Case in point below:

static function function1(int1: Int, int2: Int): Int 
        {
            return int1 + int2;
        }

VS.

static function sumTwoNums(num1: Int, num2: Int): Int 
        {
            return num1 + num2;
        }
Simn commented 2 years ago

I looked at the example in its context again and now I actually disagree: The name strings here is quite descriptive because the plural suggests that it's some sort of collection of strings, which is exactly what an Array<String> is. The fact that they are names of fruits has no relevance here. This is amplified by the fact that the next example uses floats, in which case you couldn't come up with any semantic name anyway.

So overall I prefer to not make any changes here.

chikega commented 2 years ago

That's fine, to each his own. In reference to reserved identifiers: This code snippet actually compiles with no errors. I know I'm being a bit explicit in the datatype identification. But datatype identifiers can be used as variable names in Haxe? So, it appears that datatype identifiers are indeed not reserved words in Haxe? The equivalent code snippet would throw an error in most languages I'm familiar with. This is kinda cool, but also kinda scary,

class BasicTest {

    static function main()
        {
            var String: String = "Zach";
            Sys.println(String);

            var Int: Int = 5;
            Sys.println(Int);
        }
}
Simn commented 2 years ago

That's why I said that String is a mere type name, there's nothing inherently reserved about this. Although it's still a bad idea to use it because the compiler might generate String.fromCharCode or something like that, which could then fail if a String local variable shadows the type.

Out of curiosity, which languages don't allow this?

chikega commented 2 years ago

Thanks for the insight. So far this is what I have. C# Screenshot The assignment target must be an assignable variable, property or indexer. Same warning for both string and int Go Screenshot Variable 'string' collides with the 'builtin' type, but Go allowed it to compile Dart Screenshot Verbose warning Object Pascal test.pas(4,5) Fatal: Syntax error, "identifier" expected but "STRING" found

themelz commented 2 years ago

Multidimensional arrays needs further documentation if not better implementation. I have a few things that don't make sense. Using these: var a1:Array=[1,2,3]; var a2:Array=[3,2,1]; var a3:Array=[2,3,1]; var all1:Array<Array>=[a1,a2,a3]; var all2:Array<Array>=[[1,2,3],[3,2,1],[2,3,1]];

I can easily sort either all1 or all2 which tells me that Haxe can handle each inner array as a value. But if I do trace(all1.indexOf(a1),all1.indexOf([1,2,3]),all2.indexOf(a1),all2.indexOf([1,2,3])); output is 0, -1, -1, -1. I think I should get 0,0,0,0.

I also haven't found a way to use mapping to make the string array ["1,2,3","3,2,1","2,3,1"] from all1 or all2. Using trace(all1.map(item -> item.toString)); I get [,,].

Also removing an inner array is problematic. If I make an array [[x,y],[2x,y+10],[3x,y],[4x,y-20]], even though one of those would = [10,20], the functions indexOf and remove won't find it either by passing the specific values or using [a ,b] where a = 10, b = 20.

Yes I can create code to do what I need. The problem is that it took awhile before I could figure out why my code wasn't working.

I assume that these behaviors is because the actual values stored in the arrays are not used, everything is pointer based and not value based. If I'm missing something, let me know. In the meantime I write workaround code.