Questions tagged [coercion]
Coercion, type conversion and typecasting are different ways of, implicitly or explicitly, changing an entity of one data type into another.
402
questions
0
votes
0
answers
37
views
Equivalent Casting of JPEG to Data, but with .mov files in Swift
I have some code that works fine, using Amplify to push and pull images from an AWS-S3 bucket.
let uploadTask = Amplify.Storage.uploadData(key: imageKey, data: srcData, options: nil)
Task {
...
0
votes
0
answers
63
views
'length(x) = 2 > 1' in coercion to 'logical(1)'
I am getting above error whenever I am passing more than two conditions in if statement. I also tried 'all' instead of if statement but it also did not work.
Error is :
Warning in distType == "...
0
votes
0
answers
21
views
I am doing LCGM and GMM but I am getting an error that i cannot solve that is related to convergence of the model. please advise
lcga1 <- hlme(logMBZP ~ trimester, subject = "CHEAR_id", ng = 1, data = phthalate_MBZP, posfix = NULL)
lcga2 <-gridsearch(rep = 20, maxiter = 100, minit = lcga1,m=hlme(logMBZP ~ ...
0
votes
0
answers
11
views
Is there a way to get the data from an Applescript image (AXImage), perhaps using Applescript/Obj-C?
Hi there — I’m writing an Applescript that uses System Events to get an image:
tell application “System Events”
tell process “appName”
tell window 1
set theImage to image 1
...
0
votes
0
answers
54
views
Is there a Rust signature signaling that any type that coerces into T would be allowed? [duplicate]
So I just started learning about Rust and the Implicit Deref Coercion. A "problem" I now often tumble over, is that this automatic deref does not work inside of other types, e.g. as Item ...
2
votes
0
answers
59
views
Specify in Rust that a generic type supports coercion to a primitive type
In Rust, I have several different enums that are all #[repr(u32)], meaning I can use as to cast them to a u32:
#[repr(u32)]
enum Foo {A, B, C}
fn f(foo: Foo) {
dbg!(foo as u32);
}
Now I want to ...
0
votes
1
answer
103
views
ToPrimitive VS OrdinaryToPrimitive
I am reading the Book, YDKJS by Kyle Simpson, topic Coercion. While looking at the specs I found that ToPrimitive calls OrdinaryToPrimitive conditionally. I read in a blog that:
JavaScript view ...
2
votes
1
answer
80
views
Why do I need to use `&` to trigger deref coercion in rust?
From The Rust Programming Language book:
Deref coercion converts a reference to a type that implements the
Deref trait into reference to another type
Questions in code snippet below:
Why & is ...
3
votes
1
answer
152
views
Why do I need to implement `From` for both a value and a reference? Shouldn't methods be automatically dereferenced or borrowed?
The behaviour of a Rust method call is described in The Rust Reference. It states that "when looking up a method call, the receiver may be automatically dereferenced or borrowed in order to call ...
3
votes
0
answers
2k
views
R shiny Warning: Error in &&: 'length = 3' in coercion to 'logical(1)'
Update:
The error is occuring specifically when the shiny app tries to add the shinyalert pop-up, which has three elements (this is what the length = 3 refers to in the error). If I remove the HTML ...
0
votes
0
answers
35
views
TS: Conditional property based on the value of a generic
Here I have a stripped down code snippet to demonstrate the sort of thing I'm trying to achieve:
interface A<RequiresB extends boolean = true> {
alwaysRequired: string
b: RequiresB extends ...
0
votes
0
answers
15
views
Reading in multiple csv files, adding file name, but all columns combining?
I am reading in multiple csv files (~50) from a folder and combining them into a single dataframe. I want to keep their original file names attached to their data and add it as its own column.
I have ...
2
votes
0
answers
53
views
Rust trait objects: specific use case of variance and/or coercion
Consider the following (playground)
trait Module {}
struct Foo {
module_box: Box<dyn Module + 'static>,
module_rc: Rc<dyn Module + 'static>,
}
impl Foo {
fn mut_box<'s>(...
0
votes
2
answers
135
views
How to enforce strict typing when setting a variable?
I would like to ensure that the data I read from a YAML file is aligned with the type of the variable it goes into:
type Http = {
name: string
api: string
resp: number
status: string
...
0
votes
2
answers
177
views
Boolean coercion fails
I am wondering why the Boolean coercion fails in this case:
!!(new Boolean(false)) === true
Although:
(new Boolean(false).valueOf()) === false
Mozilla says:
Booleans are returned as-is.
I am ...
0
votes
1
answer
59
views
Struct won't coerce to implemented dyn Trait type in function return
I have the following Rust code
use std::{
cell::{Ref, RefCell},
rc::Rc,
};
trait TraitA {
fn data(&self) -> Ref<Vec<Rc<dyn TraitB>>>;
}
struct A {
data: ...
0
votes
1
answer
93
views
How to change the data type from multiple columns in R?
I am working on a beginner's case study, and I have uploaded the relevant data in R. But there is an issue when I checked the data types in multiple columns.
I wish to change the character format to ...
0
votes
0
answers
37
views
Error in !is.na(mvi) && v %in% mvi : (converted from warning) 'length(x) = 2 > 1' in coercion to 'logical(1)'
I recently updated R to 4.2.2 and am receiving this error:
Error in !is.na(mvi) && v %in% mvi : (converted from warning) 'length(x) = 2 > 1' in coercion to 'logical(1)'
This is something ...
0
votes
0
answers
95
views
Error in as.data.frame.default, class coercion 'structure("RasterStack", package = "raster")' in data.frame not possible
`Hello everybody
I get an error when I try to use the predict function. I'm doing a habitat suitability study.
this function requires the model (in this case a glmm) in which I have used trimmed ...
1
vote
1
answer
41
views
R: Change '5-10' character to 7.5 numeric in dataframe row, NAs introduced by coercion
I have survey data of minutes to complete a journey in a dataframe, of character type. Some entries are written as a range, e.g. '5-10'. I want to change these entries to the mean of the range.
My ...
0
votes
1
answer
41
views
Getting the NA's introduced by coercion from a data frame
I have data as follows:
library(data.table)
dat <- fread("A B C
1 2 4
1 1 1
one 3 5")
When converting an entire data.frame to numeric, I want to ...
-1
votes
4
answers
96
views
If number is not in list return next highest number + python
I need method of checking whether a number exists is a list/series, and if it does not returning the next highest number in the list.
For example:- in the list nums = [1,2,3,5,7,8,20] if I were to ...
2
votes
0
answers
109
views
Can't initialize Struct with Closure in its impl block
(See Bottom For Addendum)
Let's say I defined the following Struct:
struct Test<F>
where
F: Fn()
{
func: F
}
I then write a constructor:
impl<F> Test<F>
where
F: Fn()
{
...
1
vote
3
answers
41
views
Coercion in JavaScript - Abstract Methods
I have recently started learning more about coercion in Js and I have been going through the ECMAScript documentation for knowing more about it and how different cases are handled in Js.
While doing I ...
1
vote
1
answer
37
views
Strange as.Date() behavior
I'm using R 4.2.1 with all packages updated to the latest version.
The two lines below differ only in the order of the elements in a concatenated vector, yet the output is completely different.
as....
1
vote
1
answer
257
views
How to convert a string with numbers and spaces in between into a number string? [duplicate]
Say that I have a number string which I've copied and pasted from the internet.
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
...
3
votes
1
answer
336
views
How to turn a "specific function type" into a function pointer type genericaly?
In rust when a name denotes a function, its type is actually specific to that function:
fn f(){}
The type of f is not fn() but fn() {f}. This actually causing me serious trouble in production code.
I ...
4
votes
1
answer
199
views
`Coercible` for uni-constructor types and `()`
This type can be coerced to ():
newtype Foo = Foo ()
f :: Foo -> ()
f = coerce
But the following cannot:
data Foo = Foo
f :: Foo -> ()
f = coerce -- Fails: Couldn't match representation of ...
0
votes
1
answer
319
views
Why does this example involving std::accumulate compile (badly), and how to guard against misuse?
Surprinsingly (for me), this compiles:
std::vector<int> v;
std::accumulate(std::cbegin(v), std::cend(v), 0, [](double sum, auto i){
return sum + 0.1;
});
(gcc 12 with --std=...
0
votes
0
answers
261
views
Powershell decimal to [Int] not failing
I am trying to validate a string of what should be comma separated integer values to use as success codes. If I start with an array with some invalid values, like this...
$test = @(0, 100, -200, 3.3, '...
1
vote
2
answers
632
views
How to convert "numeric string" to a date object using `as.Date()`?
I have a list of calendar dates in a data frame given in the format:
20000110
20000117
20000124
which standard for 2000-01-10, 2000-01-17, 2000-01-24, etc.
However, using
df %>% mutate(date= as....
2
votes
1
answer
138
views
How to prevent R from inserting newline character when coercing long formula to character?
I am using missRanger::missRanger that coerces a formula into it's character components, similar to this:
x1 <- as.character(as.formula(paste(paste(rep('foo', 84), collapse=' + '), '~ .')))[[2]]
x1
...
1
vote
1
answer
234
views
(R) Question regarding type coercion when converting a data frame to a matrix in R
apologies for the rather rudimentary questions, but I haven't been able to easily find any answers, and also just want some solid confirmation on things.
I have a data frame which contains numeric, ...
2
votes
1
answer
188
views
Unable to coerce during `DerivingVia` when using optics types like `Prism'`
I'm unable to automatically derive instances using DerivingVia on any type that uses types from optics-core like Prism'; the error I get from compiler is:
src/Main.hs:24:13-19: error:
• Couldn't ...
2
votes
1
answer
155
views
How does destructuring of borrowed data work at compile time? [duplicate]
I've been using destructuring to create references into nested data, just for practice. I created a method that uses destructuring to break apart a borrowed tuple:
fn print_strings((x, y): &(...
1
vote
0
answers
34
views
Implicit deref coercion not working with impl Trait [duplicate]
Consider the following setup.
pub trait Draw {
fn draw(&self) {}
}
pub struct Square;
impl Draw for Square {}
fn draw_it(v: &dyn Draw) {}
fn main() {
let bs: Box<dyn Draw> = ...
0
votes
0
answers
277
views
dplyr::mutate on objects with multiple classes
I have the following code:
x <- data.frame(a = 1:10, b = 1:10)
class(x) <- c("data.frame", "somthingelse")
x %>% dplyr::mutate(c = a)
and I get the following error:
Error:...
0
votes
1
answer
159
views
Coq - Coercion of list nat
I would like to do a coercion from the type list nat to the type list bool. I would think that this could be done in the following way:
From Coq Require Import Lists.List.
Definition nat_to_bool (n : ...
0
votes
1
answer
663
views
Calculating distance between multiple points at the same time of the day
I have two dataframes, one with my boat GPS positions (5512 records) and one with fishing boats positions (35381 records). I want to calculate the distance between my boat and all other fishing boats ...
2
votes
1
answer
443
views
Why does lifetime coercion work with structs but not with traits?
The following code is not accepted by the compiler:
struct Struct<'a, 'b: 'a> {
value: &'a dyn Value<'b>,
}
impl<'a, 'b: 'a> Struct<'a, 'b> {
fn foo(&self) {
...
-2
votes
1
answer
551
views
Can I declare an enum value that takes a String or &str without needing additional functions?
I have an enum with a String:
enum MyLovelyEnum {
Thing(String),
}
For tests, I would like to be able to pass in a &'static str to avoid MyLovelyEnum::Thing("abc".to_string) over ...
0
votes
1
answer
535
views
How to convert a variable to boolean in AutoHotkey?
In JavaScript, we could do the following:
!!0 // false
Boolean(1) // true
How to achieve this in AutoHotkey? For example, convert WinExist("ahk_exe foo.exe") to a boolean value and print it ...
-2
votes
1
answer
371
views
Difference between !typeof str == "string" and typeof str != "string" [duplicate]
I was writing a simple if condition that checks the type of the input. I used !typeof str == "string"
it did not work, so i used typeof str != "string" instead of it. it worked. ...
2
votes
0
answers
98
views
Is it possible to introduce extra constraints using e.g. unsafeCoerce?
Is it possible to use unsafeCoerce (or some other unsafe functionality) to 'make up' e.g. a Typeable constraint for a type? I.e., to change a type from a forall v. v into an existential forall v. ...
1
vote
2
answers
220
views
How to coerce multiple columns for multiple data.frames as character in R?
I want to coerce all columns for multiple data.frames to character in order to rbind them later. The problem is I can't create the appropriate function to use it within lapply.
# Fake dataset
set.seed(...
1
vote
4
answers
236
views
How to coerce multiple column classes?
I have a data frame and columns are different classes including strings, numeric, integer, etc.
I have a vector of classes class_vector <- c("string", "integer" ,"numeric&...
0
votes
0
answers
25
views
Implicit character coercion in R: why is "4" > 10 == TRUE? [duplicate]
R uses rules of implicit coercion to convert values across data types, and this also applies to character vectors (link to manual, link to Advanced R). The documentation (available at ?==``) also ...
-2
votes
1
answer
53
views
Why do arrays coerce in a specific way? [duplicate]
I'm struggling to understand why this examples below end up with this kind of results.
String([,,,]); // ',,' why?
Number([8,8]); // NaN why?
please explain with details if possible
0
votes
0
answers
36
views
how does the js coercion work in some cases [duplicate]
I've learnt that converting null and undefined to string via String() method, the result will be 'null' and 'undefined', but why does String([null,undefined]) return ','
String(null); //'null'
String(...
2
votes
2
answers
115
views
`unknown` type is coerced to `string`, despite official semantics for `unknown`
I've read the official doc about the unknown type but I have some difficulties to really understand how it works.
One can read in the doc : "no operations are permitted on an unknown without ...