"The trait bound `Bson: From<u128>` is not satisfied" error when using the doc! macro with u128 attribute - mongodb

The following function leads to the error mentioned in the title:
pub async fn read_foo(client: mongodb::Client, key: u128) -> Option<mongodb::bson::Document> {
client
.database("db")
.collection("col")
.find_one(mongodb::bson::doc! {"key": key}, None)
.await
.expect("Could not connect to database")
}
This function works with u32, i64 but not u128. I need u128 here, what do I do?

I think the functionBson::From(u128) was called to convert the value of type u128 into one of the variants of the enum Bson, which defines all the supported BSON types. However, because there is no valid variant for u128 the compiler complained and threw the error message.
I can think of two workarounds, but don't know if any of them works for you. One is use string instead of u128 as the value type if you just wanted to store it in MongoDB without calculations, and the other is use Decimal128 if 34 decimal digits of precision is acceptable.
If you go the latter way, please pay attention that it seems the support for Decimal128 has not fully supported yet. See the doc.

Related

Dart: Null-safety and retrieving values from maps

there is one thing I can't quite wrap my head around concerning null-safety in Dart, and that concerns how to safely retrieve values from Map<String,dynamic> (I've read the FAQ from the Dart docs).
Basically, the following code in the DartPad with null-safty enabled is valid:
void main() {
int i;
Map<String, dynamic> map = {"key": 1};
i = map["key"];
print(i);
}
Which I do not understand. Why can I assign map["key"] to i without the compiler shouting at me? From the docs:
Code should be safe by default. If you write new Dart code and don’t
use any explicitly unsafe features, it never throws a null reference
error at runtime.
But exactly this is happening. If, in the code above, the key is not in the map, or contains some random type, the program will crash on runtime, which I though is what should never happen with null safety.
I'm particular interested in this since I'm writing a Flutter app and don't understand how to properly deserialize the JSON data I fetch from the DB (try..catch? Special syntax like ??= ?). Even though I don't have the 'non-nullable' language feature enabled (I can't even write int? val without getting a warning), the compiler does not seem to mind that I assign nullable values to non-nullable variables, and will happily crash on runtime if they are null.
Basically, my question does not only concern null-safety, but the type system in general, since from my understanding it shouldn't be possible to assign a dynamic value to an int variable, but obviously this works with Map. Any explanation is greatly appreciated!
A dynamic variable can contain any data type, "dynamic" keyword is used when you don't know the specific data type that might be returned.
and what you're actually doing here:
i = map["key"];
is assigning a "dynamic" variable to an "int" variable and since dynamic in this case the value in the key/value pair of your Map is an integer which matches the data type "int" of variable "i" it won't crash because of type inference done at runtime and not compile time. if the "dynamic" variable was a String it would crash at runtime because of a type mismatch. Hope this is explanatory.

Specman e: "keep type .. is a" fails to refine the type of a field

I have the next code in my verification environment:
// seq_file.e
extend SPECIFIC_TYPE sequence {
keep type driver is a SPECIFIC_TYPE sequence_driver;
event some_event is #driver.as_a(SPECIFIC_TYPE sequence_driver).some_event;
};
extend SPECIFIC_TYPE SEQ_NAME sequence {
body()#driver.clock is only {
var foo := driver.specific_type_field;
};
};
Note that thank to keep type driver is a.. there is no need in driver's casting in the line starting var foo..
BUT, the keep type driver is a.. does not affect the some_event, i.e. if the as_a casting is removed from the line, there is a compilation error says 'driver' does not have 'some_event' though its subtype do. use driver.as_a(SPECIFIC_TYPE sequence_driver).
Why the keep type driver is a.. fails to cast the driver in some_event.. line?
Thank you for your help
Your observation is correct: type constraint is taken into account for a field access (and for a method call), but not for event sampling. This is a limitation, and it might be removed in upcoming versions. I suggest contacting official Specman support for the exact info on the plans.

How to coerce a value to a String in Pony?

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.

in F#, when is a "constant string expression" not a "constant string expression"

New to F#, so I'm still kind of getting used to "type inference".
I am trying to use EntityFramework in F# and when I try to build the connection, I get the error that my connection string (that's read from the app.config) is "not a valid constant expression or custom attribute value". If I hard-code the connection string it's fine, just like you'd expect.
open FSharp.Configuration
type Settings = AppSettings<"app.config">
let ConnStr = Settings.ConnectionStrings.Model
type private Connection = SqlEntityConnection<ConnectionString=ConnStr, Pluralize = true>
what am I doing wrong? is there a different, "F# way", of doing this?
SqlEntityConnection is a Type Provider. Type Providers work at compile time (think of them as compiler plugins; or if you're into Lisp, think of them as a poor man's macros). Therefore, all Type Provider arguments need to be known at compile time.
Now, ask the question: is ConnStr known at compile time?
No, of course it isn't, because you want to lift it from your config file.
The way you're supposed to work with this is:
Hard code the connection string, point it to a database that is available at compile time. This will give the type provider an opportunity to look at the database at compile time, and generate all the types from it.
When you call Connection.GetDataContext, give it the runtime connection string as argument. This will tell it to connect to whatever database is actually specified in your config, not the one you hard coded at compile time.

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.