Storing Array in Hashmap - mql4

I have been looking for solutions on how to store an Array or List inside a Hasmap for mql4, but did not find a way to implement this.
I tried using the Hashmap Implementation for MQL4, but it only supports the standard string, int, double, ... and only constant expressions. I also tried a CHashMap Library, but it doesn't work at all.
What i would expect:
A map with the order ticket number as key (either default type or string) and a list of double values as the value
map =
{
"#1111111" : {1.1, 1.2, 1.3, 1.4},
"#2222222" : {2.1, 3.2, 1.6, 1.4},
...
}

Related

Find function/method body explicit dependency types using Dart analyzer package

I would like to understand how can I analyze methods / functions body to find types that are explicitly referenced from it. I have success analyzing method declaration (return type, parameter types, etc..), however I have no idea how to do that for body.
Assuming following function:
String someFunction(int param) {
final list = <String>['a', 'b', 'c']; // -> DartTypes: String, List<String>
final myClass = MyClass<Arg>(); // -> DartTypes: Arg, MyClass<Arg>
final functionCall = anotherFunction<FunctionArg<Arg>>(); // -> DartTypes: Arg, FunctionArg<Arg>
return 'result';
}
// At is point I would like to know that my function depends on
// String, List<String>, Arg, MyClass<Arg>, FunctionArg<Arg>
// in term of DartType instances with proper typeArguments.
I tried getting AstNode for method element described here: https://stackoverflow.com/a/57043177/2033394
However I could not get elements from nodes to figure out their types. Their declaredElement values are always null. So I can not get back to Element API from AST API.
If you've used the exact snippet from the answer you've referenced, the problem is likely in getParsedLibraryByElement(). This method only parses the referenced library - meaning that you'll get an AST that doesn't necessarily have semantic references (like the declaredElement of AST nodes) set.
Instead, you'll want to use getResolvedLibraryByElement. The AST returned by that method will have its types and references fully resolved.
With the resolved AST, you could visit the body of the method with a custom visitor to find type references. Your definition of "referenced types" isn't really exact - but perhaps you can collect types in visitNamedType for type references and visitVariableDeclaration to collect the types of variables.

Why jsonDecode in Dart has no type and no documentation?

As you can see in https://api.dart.dev/stable/2.7.1/dart-convert/jsonDecode.html, it has no type and no documentation. I don't know which methods I can invoke on the result neither I don't know which type to but on a parameter that should be a json object.
Why is Dart like this? And what are the advantages?
It does have documentation, and you are linking to it.
If you want it to have more documentation, then that is reasonable. The returned value is admittedly not documented very well.
The function jsonDecode is a shorthand for json.decode, which again forwards to JsonDecoder.convert.
It returns a "JSON value" object which depends on the JSON source that it decodes.
A "JSON value" can be any of:
* null
* an int
* a double
* a String
* a bool (true or false)
* a List<dynamic> containing zero or more JSON values.
* a Map<String, dynamic> mapping keys to JSON values.
Those are also the same values that are accepted by the JsonEncoder which converts object structures to JSON strings.
Since these types have no common superclass other than Object, the function cannot have a return type which is more specific than dynamic or Object.
The chosen return type is dynamic because the dynamic type allows the receiver to optimistically call any member on the value. They might know that the value will always be a map, so they can just do jsonParse(jsonSource)["key"] to look up a value. Obviously, if the source was not a JSON object, that call will fail.
If you don't know which type the result is, you have to check:
var data = jsonDecode(jsonSource);
if (data is Map<String, dynamic>) {
something something data["key"] something
} else if (data is List<dynamic>) {
something something list[2] something
} else ... etc ...
A valid JSON file is actually a valid Dart expression too. The value returned by jsonDecode is similar to the value you would get if you wrote the JSON code directly as Dart code (in Dart 1 it was exactly the same, in Dart 2, the Dart code might infer a more precise type for maps and lists).

Using stringInterpolationSegment string initializer in swift

While researching the String Structure Reference from Apple (String Structure Reference)
There are initializer methods that accepts Int parameters such as:
init(stringInterpolationSegment expr: Int)
I attempted by writing the code below to learn how to use it plus learn the difference between pass by reference vs. pass by value but can't get it to work using the following:
struct Soho {
var myCountry = "America"
init(stringInterpolationSegment expr: Int){
}
}
How should swift code be structured in order to use this string initializer?
From https://developer.apple.com/reference/swift/string/1539185-init, Apple says:
Creates a string containing the given value’s textual representation.
Do not call this initializer directly. It is used by the compiler when interpreting string interpolations.
(emphasis mine)
And they show you an example in https://developer.apple.com/reference/swift/stringinterpolationconvertible, where we see that indeed we should use String interpolation with "\()".

add dictionary to dictionary in swift

according to this page it is possible to add an entire dictionary to another
http://code.tutsplus.com/tutorials/an-introduction-to-swift-part-1--cms-21389
but running the code gave me compilation error
var dictionary = ["cat": 2,"dog":4,"snake":8]; // mutable dictionary
dictionary["lion"] = 7; // add element to dictionary
dictionary += ["bear":1,"mouse":6]; // add dictionary to dictionary
error :
[string: Int] is not identical to UInt8
is there a right way to do this functionality in swift ?
of i should add them 1 by 1 ?
The page you are referring to is wrong, += is not a valid operator for a dictionary, although it is for arrays. If you'd like to see all the defined += operators, you can write import Swift at the top of your playground and command+click on Swift, then search for +=. This will take you to the file where all of the major Swift types and functions are defined.
The page you linked to also includes some other erroneous information on quick glance in the array section where it says you can do this: array += "four". So, don't trust this page too much. I believe you used to be able to append elements like this to an array in earlier versions of Swift, but it was changed.
The good news is that with Swift you can define your own custom operators! The following is quick implementation that should do what you want.
func +=<U,T>(inout lhs: [U:T], rhs: [U:T]) {
for (key, value) in rhs {
lhs[key] = value
}
}
Almost invariably when swift complains something is not like UInt8, there's a casting error in your code that may not be obvious, especially in a complex expression.
The problem in this case is that the + and += operators are not defined for that data type. A very nifty way to join arrays is described here:
How do you add a Dictionary of items into another Dictionary

Grails BigDecimal constraints not working

I am using Grails version 2.3.3, and groovy version 2.1.8.
I am using an online tutorial to help learn Grails web dev, and I have created a domain class with the following constraints
package racetrack
class Race {
static constraints = {
name(blank:false, maxSize:50)
city(blank:false)
state(inList:["GA", "NC", "SC", "VA"])
startDate()
distance(min: 0.0)
cost(min: new BigDecimal(0.0), max: new BigDecimal(100.0))
maxRunners(min:0, max:100000)
}
String name
Date startDate
String city
String state
BigDecimal distance
BigDecimal cost
Integer maxRunners
}
I'm using scaffolding so I have full CRUD functionality. The problem is, when I go to create a new Race, the app allows me to input values like "-1" in both the distance and cost fields, and values like "200" for the cost field. I noticed that the Integer field maxRunners was working correctly, as it displays a warning message if I try to put in -1 maxRunners. I changed the cost field to be of type Integer, and then the constraints started working.
Why is this happening? I copy and pasted the code from the tutorial into my text file and the constraints to not work for BigDecimal type fields.
According to the docs for the min and max constraints, the parameter must be
a class that implements java.lang.Comparable. The type of the value must be the same as the property.
so I can't see any reason why your definition shouldn't work. As a workaround, you could try replacing this
cost(min: new BigDecimal(0.0), max: new BigDecimal(100.0))
with
cost(range: 0..100)
and you could replace this constraint:
distance(min: 0.0)
with a custom validator, e.g.
distance validator: {
it >= 0.0
}
I've just noticed the following oddity which appears before the 2 problematic constraints
startDate()
Perhaps replacing it with either:
startDate(nullable: true)
or
startDate(nullable: false)
might fix the problem?
Doesn't Groovy use java.math.BigDecimal by default? Could you change new BigDecimal(0.0) to simply 0.0 and new BigDecimal(100.00) to 100.00 like this:
cost(min: 0.0, max: 100.0)
Here is the documentation that states:
To support the 'least surprising' approach, groovy literals with decimal points are instantiated as java.math.BigDecimal types rather than binary floating point types (Float, Double).
Also, you could try forcing it by using the G suffix like this:
cost(min: 0.0G, max: 100.0G)

Resources