Get ocl string attribute length - string

I need to check an string attribute length and I don't know what function I have to use, size() maybe?
Context Myclass inv:self.string_attribute->size() <7

-> is for navigating from Collections.
. is for objects/values and an attribute is a value so:
...string_attribute.size()

Related

Groovy By default in Map Numeric values considered as Big decimal

I have created a Map in groovy:
def measureMap = [:]
measureMap.put('engine.temperature', 70.0)
measureMap.put('fuel.level', 71.0)
The values for this map considered as Bigdecimal.
Is there any reason for this.
Why I am asking this i know the hierarchy
java.lang.Object
java.lang.Number
java.math.BigDecimal
so I thought by default it should consider it as double/float .
By default, Groovy uses BigDecimal for decimal numbers. This is documented:
Conveniently for exact decimal number calculations, Groovy choses java.lang.BigDecimal as its decimal number type. In addition, both float and double are supported, but require an explicit type declaration, type coercion or suffix. Even if BigDecimal is the default for decimal numbers, such literals are accepted in methods or closures taking float or double as parameter types.
If you need your values to be double-typed in the map, you can add the familiar D or F suffixes for double/float literals:
measureMap.put('engine.temperature', 70.0d) //java.lang.Double
measureMap.put('engine.temperature', 71.0f) //java.lang.Float

Check if variable is instance of ArrayList in SSJS?

I have in a SSJS javascript a function that takes in a parameter. In the function I need to check what type of element the parameter is.
I can check for example if that type is an array:
if(false == values instanceof Array)
I notice that sometimes the incoming parameter is an ArrayList of strings. Is it possible to check against this type also in SSJS?
For now I just convert the object type before I send it to the function.
You can check with
values instanceof java.util.ArrayList
or with
values instanceof java.util.List
if you want to cover all sorts of Java Lists.

swift println float using string

I wish to ask a conceptual question. My code is to print an array of float values of 5 decimal places onto the console. Why must it be String instead of Float? Ans[y] is an array of type float.
println(String(format: "%.5f", Ans[y]))
Instead of Float
println(Float(format: "%.5f", Ans[y]))
Float gives an error of extra argument 'format' in call
You can use map() to format your Float array as string array. Btw you should give it a name starting with a lowercase letter. Try doing as follow:
let floatArray:[Float] = [1.23456,3.21098,2.78901]
let formattedArray = floatArray.map{String(format: "%.5f", $0)}
println(formattedArray) // "[1.23456, 3.21098, 2.78901]"
It's just a matter of understanding what your words mean. String is an object type (a struct). Float is an object type (a struct). The syntax Thing(...) calls a Thing initializer - it creates a new object of type Thing and calls an initializer method init(...). That's what you're doing when you say String(...) and Float(...).
Well, there is a String init(format:) initializer (it actually comes from Foundation NSString, to which String is bridged), but there is no Float init(format:) initializer - the Float struct doesn't declare any such thing. So in the second code you're calling a non-existent method.
You can use NSLog instead of println. NSLog is still in the foundation class and has the flexibility of specifying the exact format you need.

Type as a String

How do I convert a type to a string?
I thought something like this should work
import std.stdio: writeln;
import std.conv: to;
writeln(to!string(int));
Update: I found it at http://dlang.org/phobos/std_traits.html#.fullyQualifiedName
I guess all logic in D operating on types are given as templates arguments right?
int is already a type. You don't need to typeof it.
You could use the .stringof property to get a string representation. http://ideone.com/T4yYmo
writeln(int.stringof);

Using the Object class in Java

I understand that declaring variables of type Object or passing an Object variable as a parameter in a method is so that we can pass any object type e.g. Integer or String or an array. I just wanted to ask if we can also pass primitive data types or cast to integer primitive types too?
For example if I have a class Stack which allows us to push and pop objects of type Object, then i can use this class for Integer objects BUT can i use it for a primitive type int?
Yes you can, because Java will "auto-box" your primitive type. In other words, if you pass an int to your method, it will first get converted to an Integer, then that Integer will be passed as an argument to your method.
This tutorial gives more details about how it works.

Resources