Wish CreateMap and CreateList but shorter

aeric

Expert
Licensed User
Longtime User
I use Maps and List a lot.
I love CreateMap() function.
However, sometimes I think it has a bit more characters (9). I think the ideal length is 3 to 5 max.
I am not complaining during typing since we have auto-complete but when reading as code, I feel a bit uneasy. 🄲

Can we have a clone of CreateMap, which behave the same but choose a word with less characters?

Suggestions:
  • Map()
  • Hash()
  • Pair()
I like Array(), it only has length of 5.

I also agree to have one for List().
@LucaMs has wished before to have a CreateList() function.
Same, I wish it has a short name too.

I think it has been quite a long time since B4X introduced new syntax. When it happens, I hope Erel will consider this wish in the list.
 

aeric

Expert
Licensed User
Longtime User
I prefer a single noun instead of combination of verb+noun.
 

Sandman

Expert
Licensed User
Longtime User
I wouldn't be opposed to an even simpler syntax using { ... } for CreateMap and [ ... ] for creating a list.

B4X:
' Basic new map
Dim FirstNewMap as Map = {"Robot": True, "Mood": "beep-boop"}

' Nested new map, split over several lines
Dim SecondNewMap as Map = { _
    "Name": "Bob", _
    "Address": { _
        "Street": "Mystreet 1", _
        "Planet": "Mars" _
        } _
    }

' Basic list
Dim MyNewList as List = ["Red", "Green", "Blue"]

I can't see it happening, but that doesn't stop me from posting. :)
 

AHilton

Active Member
Licensed User
Longtime User
Isn't it already fairly obvious that you're creating a map/object from the "... as Map" and a list from the "... as List" in the declaration on the same line (or continuing line as the case may be)?

JSON uses it because it's human readable on it's own with no context around it, unlike B4X code. After creation of the map or list, it's not like you're going to view that declaration with the { or [ to determine what it is.
 

Sandman

Expert
Licensed User
Longtime User
I suspect you haven't thought this through. Or perhaps I'm missing something fairly obvious. šŸ™‚

Please take my example and remove {} and [] and adjust as you think makes sense and post it here for us to see.
 

aeric

Expert
Licensed User
Longtime User
Isn't it already fairly obvious that you're creating a map/object from the "... as Map" and a list from the "... as List" in the declaration on the same line (or continuing line as the case may be)?

JSON uses it because it's human readable on it's own with no context around it, unlike B4X code. After creation of the map or list, it's not like you're going to view that declaration with the { or [ to determine what it is.
The idea is
  1. to let the compiler convert the code starting with { or [ straight to the Map or List variable,
  2. without using any library (e.g JSON parser) that you need to initialize
  3. or method that you only pass as a String literals to escape some special characters
  4. and the compiler can immediately show a warning in the IDE on realtime.
 

aeric

Expert
Licensed User
Longtime User
I wouldn't be opposed to an even simpler syntax using { ... } for CreateMap and [ ... ] for creating a list.

B4X:
' Basic new map
Dim FirstNewMap as Map = {"Robot": True, "Mood": "beep-boop"}

' Nested new map, split over several lines
Dim SecondNewMap as Map = { _
    "Name": "Bob", _
    "Address": { _
        "Street": "Mystreet 1", _
        "Planet": "Mars" _
        } _
    }

' Basic list
Dim MyNewList as List = ["Red", "Green", "Blue"]

I can't see it happening, but that doesn't stop me from posting. :)
It is nice to see if we can pass the object without the underscores like this.
B4X:
Dim SecondNewMap as Map = {
    "Name": "Bob",
    "Age": 25,
    "Married": True,
    "Address": {
        "Street": "Mystreet 1",
        "Planet": "Mars"
        }
    "Kids": [
        "Jane", 
        "Oscar"
    ]
}
 

Sandman

Expert
Licensed User
Longtime User
It is nice to see if we can pass the object without the underscores like this.
Sure, but that would not be very consistent with how the language is designed. I wouldn't oppose it either, but I would consider it a different wish (with its own thread).

(Smart strings don't use the underscores, but that is consistent with the concept of the smart strings. Adding an underscore at the end of each line would interfere with how the strings are parsed.)
 

AHilton

Active Member
Licensed User
Longtime User
Simply replace your { } and [ ] with the standard ( ) would be much easier in typing. Might as well just start enforcing ; at the end of each line and call it B4Javascript. 😳

Personally, I have no problem with CreateMap() and would certainly consider CreateList() a simple and effective extension to what the language already provides without introducing a new syntax. As for the problem that Aeric has with typing more than 4 characters to do either of these, well, I don't. If you really want to go cryptic, then just make it CM() and CL(). ;)
 

Sandman

Expert
Licensed User
Longtime User
Simply replace your { } and [ ] with the standard ( ) would be much easier in typing.
That would probably be a unique solution, I don't think I've ever seen maps or lists defined so casually in code. Can you give an example of a well-known language that went that route? I'd actually go so far as saying that my suggestion is the standard.

I'm curious, do you use a keyboard layout where those characters are extra difficult to reach? That would make it easier to understand why you find them problematic.
 

cklester

Well-Known Member
Licensed User
(Smart strings don't use the underscores, but that is consistent with the concept of the smart strings. Adding an underscore at the end of each line would interfere with how the strings are parsed.)

In this case, any unclosed bracket on a line means "starting a map or list" (depending on the bracket type), and then it would function like a Smart String in its handling of subsequent lines. That should be real doable.
 

Sandman

Expert
Licensed User
Longtime User
In this case, any unclosed bracket on a line means "starting a map or list" (depending on the bracket type), and then it would function like a Smart String in its handling of subsequent lines. That should be real doable.
Yeah, I suppose. I have no love for the underscores (but also no better suggestion), and if Erel can figure out a solid solution for how to skip them in these cases I'm all for it.
 

Sandman

Expert
Licensed User
Longtime User
Yeah, I suppose. I have no love for the underscores (but also no better suggestion), and if Erel can figure out a solid solution for how to skip them in these cases I'm all for it.
Come to think of it, if it's reasonable to ignore the underscores in maps and lists, there's no real reason for them to exist in ( ... ) either. Meaning this would be fine:
B4X:
Dim Beep as String = GetBeep(
    "boop",
    "and",
    "other",
    "inputs"
)

That would make their use sort-of like "no need for underscore if you're within an obvious scope", but we'd have to use them if we want to split other lines, like this (weird) example:
B4X:
Dim Beep _
as String = _
"boop"

If it's possible, I'd say that would be a win for the language. :)
 

aeric

Expert
Licensed User
Longtime User
I don't mind about the other scenarios but for maps and list I think is good for readibility.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
This innovation would also address wishes for variable length argument lists and named arguments or even a mixture.

B4X:
    x = computeValue([arg0, arg1, arg2])
    
    y = computeValue([arg0, arg1, arg2, {
        "arg3name": arg3,
        "arg4name": arg4
    }])

Sub computeValue (args As List)
    Dim arg0 As String = args.get(0)
    Dim arg3 As Map = args.get(3)
    Dim arg3A as String = arg3.GetDefault("arg3name", "N/A")
    Dim arg3B as String = arg3.GetDefault("arg4name", "N/A")
    
    'caution use same caseness for keys - or always convert {}keys inside () to lowerCase :) 
End Sub
 
Top