Dictionary Initialization & Use

Report core bugs regarding the Ultima Online Emulator Core release (version 097). You can attach your Core Dump. One bug per post.

Moderator: POL Developer

Locked
Luth
Former Developer
Posts: 200
Joined: Mon Jan 30, 2006 8:10 am

Dictionary Initialization & Use

Post by Luth »

I've noticed twice now that dictionaries (and likely structs) must be first put into a variable before being used. The following two incidences compiled, but failed for me:

Code: Select all

return dictionary { "opponent" -> who };
The function did not return any value.

Code: Select all

var entry := dictionary;
entry["disabled"] := dictionary { FIGHT_DISABLE_ATTACK -> 0, FIGHT_DISABLE_ITEMS -> 0 };
entry did not contain a "disabled" member at all; not a dict, not an error, nothing.

In each instance, putting the dictionary into a "var temp := dictionary {....};" before returning or assigning it has fixed the problem.
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Dictionary Initialization & Use

Post by Yukiko »

Luth, since my experience with programming languages is limited how does this compare with languages like C or Delphi? Can they return structured data types that have not been declared in advance?
Luth
Former Developer
Posts: 200
Joined: Mon Jan 30, 2006 8:10 am

Re: Dictionary Initialization & Use

Post by Luth »

C/C++ doesn't really have anything of this nature in its language. For example, an eScript struct:

Code: Select all

var myStruct := struct;
myStruct.+field1 := 1.0;
myStruct.+field2 := 2;

-or-

var myStruct := struct{"field1"->1.0, "field2"->2};
This results in a variable called "myStruct" of type struct with two fields, a float and an int.

A C-Struct of the same format would be declared like this:

Code: Select all

struct MyStruct
{
    float  field1;
    int    field2;
};

MyStruct myStruct;
myStruct.field1 = 1.0f;
myStruct.field2 = 2;
The end result is the similar, but different. Here, we have a variable called "myStruct" of type MyStruct (a difference here) with only ever two fields, a float and an int.

C/C++ are typed languages. Unlike eScript, structs are not defined as variables. Structs are data types, like int or float, except that they're defined by the programmer. Because structs are types, they cannot be returned by functions, or used to initialize a variable. It would be like writing "return int;" It just makes no sense. :)

However, eScript uses structs as data, thus they can be assigned to variables (see above). Since they can, it makes sense that they can be used as variables, such as in returning from functions, or assigning directly into a dictionary field. You would not, for instance, do this:

Code: Select all

var five := 5;
myDictionary["entry"] := five;

-or-

var five := 5;
return five;
when this would do:

Code: Select all

[code]myDictionary["entry"] := 5;

-or-

return 5;
As it stands now, it is allowable (ie: it compiles without error or warning) but gives incorrect results. One way or the other, it should be fixed. :)
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Dictionary Initialization & Use

Post by Yukiko »

I am an old Pascal programmer so I understand (and love) strong typed languages. I know you can't return a structured data type from a function in Pascal. Wasn't sure about Delphi and had absolutely no clue about C so thanks for the info.

In eScript you cannot use a variable that is undeclared and since structured data types are concepts until they are 'instantiated' (is that the right word?) by declaring them, then to me it would seem that logically the compiler should error out if given the example you posted since they aren't assigned to a variable. I don't know if I conveyed my idea clearly there or not but either way you are right about it needing fixed. It should work or give an error.
Luth
Former Developer
Posts: 200
Joined: Mon Jan 30, 2006 8:10 am

Re: Dictionary Initialization & Use

Post by Luth »

You articulated your thought sufficiently for me to understand and agree with. :) I've only ever made minor adjustments to the eCompiler before, so I'm not confident that I'd be able to make the changes, but I'll add it to the list anyway.....
User avatar
OldnGrey
POL Expert
Posts: 657
Joined: Sat Feb 04, 2006 6:26 pm

Re: Dictionary Initialization & Use

Post by OldnGrey »

The practice of defining the array in the start_script call itself is very common.
Before you think of making us declare array and structs before using them, give me some warning lol.
Not opposed to having to make the change, but it seems like it should work the same for all types.
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Re: Dictionary Initialization & Use

Post by Austin »

No need to change something like

Code: Select all

Start_Script("xxx", array{"a", "b"});
What does need to be fixed though is how structs and dictionaries get built - sometimes the indexes of a dict or properties of a struct come out as uninitialized object when using the { } initializers.

From the core-changes

Code: Select all

06-12 Syzygy
    escript changes:
        Added (yet another) syntax for declaring initialized arrays:
            var x := array { 4, 3, 2 };
            this is more consistent with struct { x, y, z } initializer syntax.
            array(8,3,2) looks too much like it's creating a multi-dimensional array, so I don't like it anymore.
        Added syntax for declaring initialized dictionaries:
            var x := dictionary { "abx" -> 72, "xyz" -> 32 }; // direct
            var key := 5, value := "x";
            var x := dictionary { key -> value };       // indirect
            var x := dictionary { "abc", "cyz" }; // values start as uninit
            var pfx := "X";
            var v := 7;
            var x := dictionary { pfx+"abx" -> 72, pfx+"xyz" -> 32+v }; // with expressions
        These are the preferred forms for declarations (see how they match?)
            var x := array;
            var x := dictionary;
            var x := array { 53, 76, 32 };
            var x := struct { x, y, z };
            var x := dictionary { "ABC" -> 62 };

Code: Select all

05-23 Syzygy
      escript: added more convenient syntax for declaring structures:
      var t := struct { a, b, c };                                  // basic
      var t := struct { a := 4, b := "hey", m := foo(bar(4)) };     // initialized
      var t := struct { a := 4, b := struct { g,h,i }, c };         // with nested structures
Luth
Former Developer
Posts: 200
Joined: Mon Jan 30, 2006 8:10 am

Re: Dictionary Initialization & Use

Post by Luth »

OldnGrey wrote:The practice of defining the array in the start_script call itself is very common.
Before you think of making us declare array and structs before using them, give me some warning lol.
Not opposed to having to make the change, but it seems like it should work the same for all types.
If I do anything, my first attempt would be to make inline-dictionaries work. It adds usefulness.
Locked