objeck / objeck-lang

Objeck is a modern object-oriented programming language with functional features tailored for machine learning. It emphasizes expression, simplicity, portability, and scalability. The programming environment consists of a compiler, virtual machine, REPL shell, and command line debugger with IDE plugins.
https://objeck.org
Other
154 stars 11 forks source link

[discussion] Initialize Collections using Arrays #284

Closed ghost closed 1 year ago

ghost commented 1 year ago

I think we need something more powerful than https://github.com/objeck/objeck-lang/issues/276#issuecomment-1666850999

The syntax could look like this:

vec := Vector->New()<Int>;
vec->AddAll([1,2,3]);

The syntax on https://github.com/objeck/objeck-lang/issues/276#issuecomment-1666850999 is counter intuitive and I think should be removed and replaced with the new syntax.

objeck commented 1 year ago

Int and IntRef are not interchangeable array types.

ghost commented 1 year ago

Int and IntRef are not interchangeable array types.

OK. Then you could use the syntax like Java's Collections.addAll().

Update: What do you think about vec->FromArray([1,2,3])? Same problem with vec->AddAll([1,2,3])?

objeck commented 1 year ago

Added the following methods to all of the "Ref" classes

For example

# static int array to intref array to intref vector
r := Collection.Vector->New(IntRef->FromArray([1, 2, 3]))<IntRef>;
r->Size()->PrintLine();

# intref vector to int array
p := IntRef->ToArray(r->ToArray());
p->PrintLine();
ghost commented 1 year ago

Added the following methods to all of the "Ref" classes

  • ToArray(..) create a primitive array from a reference array
  • FromArray(..) create a reference array from a primitive array

For example

# static int array to intref array to intref vector
r := Collection.Vector->New(IntRef->FromArray([1, 2, 3]))<IntRef>;
r->Size()->PrintLine();

# intref vector to int array
p := IntRef->ToArray(r->ToArray());
p->PrintLine();

@objeck Perhaps you misunderstood me. What I suggested is something much more powerful. It's basically about initializing Collections using Arrays. This offers a way to directly initialize Collections in a reasonable way that will not increase the complexity of the language.

Btw, the current API introduced by you is so obscure:

p := IntRef->ToArray(r->ToArray());
p->PrintLine();

r is a Vector so r->ToArray() is an array. Why do you convert IntRef to an array which take r->ToArray() which is already an array? I don't understand much about the Ref types of Objeck but this is so counter intuitive.

objeck commented 1 year ago

Added the following methods to all of the "Ref" classes

  • ToArray(..) create a primitive array from a reference array
  • FromArray(..) create a reference array from a primitive array

For example

# static int array to intref array to intref vector
r := Collection.Vector->New(IntRef->FromArray([1, 2, 3]))<IntRef>;
r->Size()->PrintLine();

# intref vector to int array
p := IntRef->ToArray(r->ToArray());
p->PrintLine();

@objeck Perhaps you misunderstood me. What I suggested is something much more powerful. It's basically about initializing Collections using Arrays. This offers a way to directly initialize Collections in a reasonable way that will not increase the complexity of the language.

Btw, the current API introduced by you is so obscure:

p := IntRef->ToArray(r->ToArray());
p->PrintLine();

r is a Vector so r->ToArray() is an array. Why do you convert IntRef to an array which take r->ToArray() which is already an array? I don't understand much about the Ref types of Objeck but this is so counter intuitive.

Primitives and references to primitives are two different datatypes, thus requiring conversions between the types. It is possible to write code that converts an array of primitives (i.e., Int [1,2,3]) into an array of qualified generic references (i.e., IntRef[]); however, that breaks the properties of generic programming by enforcing tight coupling.

The Java Arrays class is primarily used to manipulate arrays and create Collections (i.e., List) from arrays via methods like asList() it is not for converting say a Java int array to a Java Integer array.

The following Java code does the latter: Integer[] array = new Integer[]{1, 2, 3, 4};

This Objeck code is equivalent: array := IntRef->FromArray([1,2,3,4])

Note the conversation work is done by the Integer class in Java and the IntRef class in Objeck. Conversely, one can write 3 to 5 lines of code to avoid using the above function.

objeck commented 1 year ago

The Integer array constructor does the conversion.

ghost commented 1 year ago

The Integer array constructor does the conversion.

If you have decided this is the optimal syntax, I don't want to argue with you any further. One more quirk of the language is noted.

objeck commented 1 year ago

The Integer array constructor does the conversion.

If you have decided this is the optimal syntax, I don't want to argue with you any further. One more quirk of the language is noted.

I make no claims that the syntax is "optimal," as that is a matter of opinion. I have implemented most of your feedback while looking past disparaging remarks. Initilizating a generic Vector can be done in many ways and is not a language limitation.

I am seeking maintainers so that I may work on other aspects of the language. The goal is to empower maintainers to make independent decisions and contribute new ideas.

ghost commented 1 year ago

I make no claims that the syntax is "optimal," as that is a matter of opinion. I have implemented most of your feedback while looking past disparaging remarks. Initilizating a generic Vector can be done in many ways and is not a language limitation.

English is not my native language, so the use of words by me is not optimal, either. I'm very sorry about that if it caused confusion or misunderstanding.

I am seeking maintainers so that I may work on other aspects of the language. The goal is to empower maintainers to make independent decisions and contribute new ideas.

I hope I could help but I can't. I don't have CS background.

objeck commented 1 year ago

@iahung2 Thank you for the feedback