Page 1 of 6 1 2 3 ... LastLast
Results 1 to 10 of 58

Thread: Map literals in ASH (potential feature)

  1. #1
    Developer
    Join Date
    Aug 2009
    Posts
    2,709

    Default Map literals in ASH (potential feature)

    EDIT: The syntax I decided to go with (arbitrarily) looks something like value[key] { key_expr : value_expr[, ...] };. Patch attached to this post. Note that said syntax can be swapped out fairly easily.

    I'm considering playing around some more with ASH's internals in my spare time, and I wanted to know a) how many people want this feature, and b) what the desired syntax is.

    Examples of existing styles:

    JSON
    Code:
    { "key1":"value1",
      "key2":"value2" }
    Perl/Ruby (and more, I'm sure)
    Code:
    { 'key1' => 'value1',
      'key2' => 'value2' }
    C# (well, okay, this barely counts):
    Code:
    new Dictionary<string, string>
    {
        { "key1", "value1" }, 
        { "key2", "value2" }
    };
    Lua:
    Code:
    { key1="value1", key2="value2" }
    Python named tuples:
    Code:
    { key1="value1", key2="value2" )
    Granted, we don't have to look at existing implementations in current languages. We could do something totally crazy like <Key1|Val1; Key2|Val2>, but that'd probably confuse everyone involved, since that'd add new meanings to four (!) pre-existing symbols.

    Thoughts?
    Attached Files
    Last edited by heeheehee; 08-14-2012 at 01:53 AM.

  2. #2
    Developer jasonharper's Avatar
    Join Date
    Sep 2008
    Location
    Oklahoma
    Posts
    1,122

    Default

    Most of those examples are from dynamically-typed languages; they're completely unsuitable for a language like ASH that requires every expression to have a definite type at compile time. (And if you want to suggest deriving the type from the types of the supplied keys and values, keep in mind that the literal still has to have a defined type even if it contains zero key/value pairs.)

    The most ASHy way of doing this would obviously be:
    Code:
    typedef string[string] strmap;
    strmap myMap = $strmap[key1, value1, key2, value2];
    In game: Seventh (#363053), originally a Seal clubber. I am the Ap'ostle of Ak'gyxoth! (Or is that Ap'ostrophe?)

  3. #3
    Developer
    Join Date
    Mar 2006
    Posts
    1,367

    Default

    Obviously is my favorite word. Obviously.

  4. #4
    Senior Member
    Join Date
    Mar 2012
    Posts
    247

    Default

    I'd like to also request this. At the very least it would nice to initialize arrays.

  5. #5
    Senior Member
    Join Date
    Nov 2007
    Posts
    225

    Default

    I prefer the json syntax

  6. #6
    Developer
    Join Date
    Aug 2009
    Posts
    2,709

    Default

    Eh. There are potential problems with the JSON syntax, since you'd need to establish the type somehow -- the key point that Jason was making was this: suppose you have a polymorphic function, and you pass in as an argument a map literal. Which form of the function gets called?

    I'm not a huge fan of Jason's proposal, since it seems awkward to use the same syntax that's used for plural typed constants for a subtly different use case..

    Also, for arrays, we technically have the plural typed constants, which _are_ implemented internally as ArrayLists and preserve iteration order (you can even include a string multiple times), so that's basically moot.

    Currently pondering something where you declare the type up front, like
    Code:
    string [string] { "key1" : "value", variable_key: variable_value };
    But that's definitely not very ASHy (I've also toyed around with the idea of anonymous functions in this form). I do like the ability here to specify arbitrary expressions (esp. since the comma is unused in ASH outside of lists), but that's also not at all ASHy.

    Language extension is difficult to do well.

  7. #7
    Developer
    Join Date
    Aug 2009
    Posts
    2,709

    Default

    Blarg, it's far too late for me to be up. Anyways. Patch attached with syntax proposed in my most recent post -- one possible improvement (off the top of my head) would be to get ASH to recognize that an expression can end with a closing brace (instead of the hack where I changed currentToken() to not return null).

    Currently only works with map literals, but meh. Array literals are of questionable utility (plural typed constants preserve iteration order -- in fact, internally they're implemented as ArrayLists), and record constructors already exist.

    If this patch is found to be acceptable, I might start working on function references (which is really just a more convenient way to use call) or perhaps adding null to ASH (... which would break existing scripts if I'm not careful).
    Attached Files
    Last edited by heeheehee; 08-05-2012 at 07:57 PM.

  8. #8

    Default

    Blarg, it's far too late for me to be up. Anyways. Patch attached
    Originally Posted by heeheehee View Post
    The patch is not attached, which I guess just confirms the first part of your message.

  9. #9
    Developer
    Join Date
    Aug 2009
    Posts
    2,709

    Default

    The patch is not attached, which I guess just confirms the first part of your message.
    Originally Posted by lostcalpolydude View Post
    Yeah, I realized this as I was about to fall asleep, then decided that sleeping was more useful at the time. Edited it in.

    edit: Sample usage:
    Code:
    > ash item[int, int] map = item[int, int] {4: item[int] {4: $item[eyepatch]} };
    
    Returned: aggregate item [int, int]
    4 => aggregate item [int]
      4 => eyepatch
    Last edited by heeheehee; 08-05-2012 at 08:03 PM.

  10. #10
    Developer
    Join Date
    Aug 2009
    Posts
    2,709

    Default

    Been experimenting a bit more with said patch (attached in post #7) a bit more, and it, uh, breaks some existing scripts -- evidently it is currently valid in ASH to use a type as a variable name. I'd personally be in favor of changing this behavior, but I'm not in charge of making decisions around here.

Similar Threads

  1. flyeredML - potential rounding problem
    By Magus_Prime in forum Scripting Discussion
    Replies: 40
    Last Post: 02-17-2015, 01:38 PM
  2. Map literals
    By shademaster00 in forum Scripting Discussion
    Replies: 4
    Last Post: 02-12-2012, 05:43 PM
  3. 10018: Tweak ScriptMRUList for potential reuse.
    By RSS Bot in forum Latest SVN Changes
    Replies: 0
    Last Post: 11-21-2011, 05:40 PM
  4. set_auto_attack() – Potential bug?
    By Banana Lord in forum Community Support
    Replies: 4
    Last Post: 06-14-2011, 06:51 AM
  5. Feature Potential Earnings (actual)
    By NardoLoopa in forum Bug Reports
    Replies: 9
    Last Post: 09-07-2010, 08:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •