How to coerce a value to a String in Pony? - ponylang

I am trying to learn Pony, and for obvious reasons, one of the first things I want to do is print values.
However, it does not seem to work for most things, like:
env.out.print(2 + 2)
Gives the error:
Could not infer literal type, no valid types found
I also tried:
let four: U32 = 2 + 2
env.out.print(four)
But this gives an uglier error saying I need something that is a subtype of ByteSeq. Fine, but how do I get one of those?

You'll have to convert the integer into a String.
In Pony there is an interface called Stringable which declares the function string(fmt), and a lots of classes implements that interface. Integers do for instance.
So just call .string() to convert a value in something printable.

Related

Built-in list of numeric types in Matlab

Does Matlab provide a list of numeric datatypes?
Some functions, like zeros, take an optional typename argument, a string naming a numeric type (e.g., 'double' or 'uint8'), that determines the type of the returned array. I would like to add something similar to my own code, and a complete list of numeric types would help with argument validation, as in:
ip = inputParer()
...
ip.addParameter('DataType', 'double', #(x) ismember(x, numeric_types()));
It seems as though something similar must exist, but I haven't been able to find it.
isnumeric checks an instance, rather than the typename itself.
superclasses doesn't return anything for numeric types on 2018b.
Obviously, this is not hard to implement, but it'd be nice to use a built-in if it existed.

Understanding Safe.Coerce in Purescript

I've been looking at the compiled version of some code snippets to dig into how my code is represented at runtime.
I'm not surprised to see that safe coerce and unsafe coerce are the same in the compiled JavaScript. There's a dictionary passed into safe coerce that's unused/undefined in the code I've looked at so far.
It seems like if the runtime representation of two types is the same, then there shouldn't be any runtime overhead for having/using such a type. This isn't true and I'm curious to understand why.
Coercing two types requires a function call at runtime. What I'm not understanding is that this function call appears to do nothing.
exports.unsafeCoerce = function (x) {
return x;
};
If I declare a newtype
newtype Selection = Selection Int
I see something similar in JavaScript:
var Selection = function (x) {
return x;
};
Which means I might see something like this in JavaScript:
return Selection(Data_Int.pow(2)(Safe_Coerce.coerce()(n) - 1 | 0));
which should be identical to this:
return Data_Int.pow(2)(n - 1 | 0);
In this case, both Selection and coerce only ever return what they're given. Once the type checking has been done, I'm not sure what purpose they continue to serve.
The reason the code is generated like it is, and not in a more efficient form like you suggest, is because the purs compiler has only a simplistic optimiser.
Why unsafeCoerce is defined as it is is because it is a PureScript function and must match the runtime representation of PureScript functions. Same goes for Selection.
Constraints are represented as functions on dictionaries. Some time ago there was an optimisation added such that empty dictionaries do not have to be constructed as empty JavaScript objects, and instead can be represented by undefined. That is why there is no argument passed to coerce.

Scala implicit type casting

I'm just beginning Scala, coming from Java.
So I know that in Scala, all things are objects, and Scala matches the longest token (source: http://www.scala-lang.org/docu/files/ScalaTutorial.pdf), so if i understand correctly:
var b = 1.+(2)
then b is a Double, plus and Int, which in Java would be a Double.
But when I check its type via println(b.isInstanceOf[Int]) I see that it is an Int. Why is it not a Double like in Java?
According to the specification:
1. is not a valid floating point literal because the mandatory digit after the . is missing.
I believe it's done like that, exactly because expressions like 1.+(2) should be parsed as an integer 1, method call ., method name + and method argument (2).
The compiler would treat 1 and 2 as Ints by default. You could force either one of these to be a Double using 1.toDouble And the result (b) would be a double.
Btw - did you mean to write 1.0+2 - in which case b would be a double?

Convert Standard.Natural to Ada.Containers.Count_Type

I instanced the Ada.Containers.Vectors generic package like this:
package My_Vectors is new Ada.Containers.Vectors(
Element_Type => My_Type,
Index_Type => Natural);
Say, I have a vector and a Standard.Natural value declared:
Foo_Vector: My_vectors.Vector;
Bar_Natural: Natural := 4;
If I call
Foo_Vector.Set_Length(Bar_Natural);
I get the following error
expected type "Ada.Containers.Count_Type"
found type "Standard.Natural"
Is there a way to cast Bar_Natural to be of Ada.Containers.Count_Type?
Sorry, I was too stupid to actually read all that my error said. I tried converting the Natural using:
Ada.Containers.Vectors.Count_Type(Bar_Natural)
Which makes zero sense!
Reading the error, it is trivial to see that Count_Type is defined in package Ada.Containers.
The correct conversion would therefore be:
Ada.Containers.Count_Type(Bar_Natural);
Giving
Foo_Vector.Set_Length(Ada.Containers.Count_Type(Bar_Natural));

string option to string conversion

How do I convert the string option data type to string in Ocaml?
let function1 data =
match data with
None -> ""
| Some str -> str
Is my implementation error free? Here 'data' has a value of type string option.
To answer your question, yes.
For this simple function, you can easily find it in Option module. For example, Option.default totally fits your purpose:
let get_string data =
Option.default "" data
There are many other useful functions for working with option types in that module, you should check them out to avoid redefine unnecessary functions.
Another point is that the compiler would tell you if there's something wrong. If the compiler doesn't complain, you know that the types all make sense and that you have covered every case in your match expression. The OCaml type system is exceptionally good at finding problems while staying out of your way. Note that you haven't had to define any types yourself in this small example--the compiler will infer that the type of data is string option.
A lot of the problems the compiler can't detect are ones that we can't detect either. We can't tell whether mapping None to the empty string is what you really wanted to do, though it seems very sensible.