to_native: cast value to the correct type