How to set the default of a JSONField to empty list in Django and django-jsonfield?

According to the Django documentation for JSONField you should indeed use default=list because using default=[] would create a mutable object that is shared between all instances of your field and could lead to some objects not having an empty list as a default. Please note that this does not only apply for django.contrib.postgres.fields.JSONField but for … Read more

Why can const char* const & = “hello” compile?

It’s essentially adhering to this formula T const & a = something_convertible_to_T; Where T is const char*. In the first case, a temporary pointer can be materialized, assigned the address of the literal, and then have itself bound to the reference. In the second case, since the lvalue reference isn’t const, it can’t happen. Another … Read more

Convert Vec into a slice of &str in Rust?

You can create a function that accepts both &[String] and &[&str] using the AsRef trait: fn test<T: AsRef<str>>(inp: &[T]) { for x in inp { print!(“{} “, x.as_ref()) } println!(“”); } fn main() { let vref = vec![“Hello”, “world!”]; let vown = vec![“May the Force”.to_owned(), “be with you.”.to_owned()]; test(&vref); test(&vown); }

How to return a class object by reference in C++?

You’re probably returning an object that’s on the stack. That is, return_Object() probably looks like this: Object& return_Object() { Object object_to_return; // … do stuff … return object_to_return; } If this is what you’re doing, you’re out of luck – object_to_return has gone out of scope and been destructed at the end of return_Object, so … Read more