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

[question] How to initialize Collections directly? #276

Closed ghost closed 1 year ago

ghost commented 1 year ago

Same problem with array on #275, it seems we have to create the Collection container first, then add each of the elements manually.

objeck commented 1 year ago

What are you trying to do? Please send a code example.

ghost commented 1 year ago

What are you trying to do? Please send a code example.

On modern Java and C#, you could initialize Collections with predefined values just like what you can do with an array directly without having to create it first and use methods to add each elements later; everything is done in one line. This is convenient but also somewhat make the languages more difficult to use since it will add a great deal of complexity to the language and you will have to remember extra syntax (which brackets to use with which Collections for example). I want to discuss with you if it worth to add support for this on Objeck.

You could get the idea by searching on Google with how to initialize Dictionary in Java, how to initialize Dictionary in C# or how to initialize map in C++. Just replace Dictionary or map with the name of the Collections you want to search for. They will list various ways to initialize the Collections (indeed there are too many ways to initialize the Collections!) and you will see there is syntax to both declare and initialize everything just in one line.

objeck commented 1 year ago

Without modifying the compiler, I added support for the following in commit ccde224:

elems := Collection.Vector->New(IntRef->FromArray([1,2,3]))<IntRef>;
elems->Size()->PrintLine();