Why does Go have typed nil?

It sounds like you’re asking about this error message: http://play.golang.org/p/h80rmDYCTI package main import “fmt” type A struct {} type B struct {} func (a *A) Foo() { fmt.Println(“A”) } func (b *B) Foo() { fmt.Println(“B”) } func main() { n := nil n.Foo() } This prints: prog.go:17: use of untyped nil [process exited with non-zero … Read more

How to correctly handle Byte values greater than 127 in Kotlin?

With Kotlin 1.3+ you can use unsigned types. e.g. toUByte (Kotlin Playground): private fun magicallyExtractRightValue(b: Byte): Int { return b.toUByte().toInt() } or even require using UByte directly instead of Byte (Kotlin Playground): private fun magicallyExtractRightValue(b: UByte): Int { return b.toInt() } For releases prior to Kotlin 1.3, I recommend creating an extension function to do … Read more

How do I idiomatically convert a bool to an Option or Result in Rust?

As of Rust 1.50 you can use bool::then: assert_eq!(false.then(|| val), None); assert_eq!(true.then(|| val), Some(val)); You can convert it to a Result by chaining Option::ok_or: assert_eq!(false.then(|| val).ok_or(err), Err(err)); assert_eq!(true.then(|| val).ok_or(err), Ok(val)); As of Rust 1.62, you can use bool::then_some and pass a value directly instead of creating a closure: assert_eq!(false.then_some(val), None); assert_eq!(true.then_some(val), Some(val)); Alternatively, you can … Read more

Why does the && operator produce the type of the second operand

Long story short, there’s no solution here that pleases everyone. Consider this common idiom: var customer = GetCustomer(…); // of type ‘Customer’ var address = customer && customer.address; if(address) { printAddressLabel(address); // Signature: (Address) => void } else { // Couldn’t find the customer or the customer has no address on file } It’d be … Read more

Python method-wrapper type?

It appears that the type <method-wrapper ..> is used by CPython for methods implemented in C code. Basically the type doesn’t wrap another method. Instead it wraps a C-implemented function as an bound method. In this way <method-wrapper> is exactly like a <bound-method> except that it is implemented in C. In CPython there are two … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)