Skip to content

Use Array's type as return type for reduction fns #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
11 tasks done
9prady9 opened this issue Jun 16, 2017 · 2 comments · Fixed by #242
Closed
11 tasks done

Use Array's type as return type for reduction fns #154

9prady9 opened this issue Jun 16, 2017 · 2 comments · Fixed by #242
Assignees
Milestone

Comments

@9prady9
Copy link
Member

9prady9 commented Jun 16, 2017

The following functions currently return f64 values irrespective of the input Array data's native type.

  • sum_all
  • sum_nan_all
  • product_all
  • product_nan_all
  • min_all
  • max_all
  • all_true_all
  • any_true_all
  • count_all
  • imin_all
  • imax_all

The above functions need to return the appropriate type value instead of f64. This change is probably going to be
easier to implemented once Typed Arrays are implemented.

@9prady9 9prady9 added this to the timeless milestone Jun 16, 2017
@9prady9 9prady9 self-assigned this Jun 16, 2017
@9prady9
Copy link
Member Author

9prady9 commented Mar 31, 2020

Facing an issue related to conversions from f64 (primitive types but just defined as associated types for necessary operations) to output primitive types (defined as associated types for necessary operations). Given below is permalink to rust playground with minimal reproducible code for future reference.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e8403edbf12055939b5d1393650e907e

pub trait TestEnum {
    type InType;
    type OutType;
}

impl TestEnum for i32 {
    type InType = Self;
    type OutType = f32;
}

fn test<T: TestEnum>(value: f64) -> T::OutType
{
    value as T::OutType
}

fn main() {
    let a = 1.0;
    println!("a is {}", test::<i32>(a));
}

@9prady9
Copy link
Member Author

9prady9 commented Aug 6, 2020

I think figured out a working version of this, leaving a permalink for future reference
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2af92fc48acc4f3af238fb1571c2c314

use std::any::type_name;

fn type_of<T>(_: T) -> &'static str {
    type_name::<T>()
}

pub trait TestEnum {
    type InType;
    type OutType;
}

impl TestEnum for i64 {
    type InType = Self;
    type OutType = u64;
}

impl TestEnum for i32 {
    type InType = Self;
    type OutType = f32;
}

impl TestEnum for f32 {
    type InType = Self;
    type OutType = Self;
}

impl TestEnum for u64 {
    type InType = Self;
    type OutType = Self;
}

pub trait Fromf64 { fn fromf64(value: f64) -> Self; }

impl Fromf64 for f32 { fn fromf64(value: f64) -> Self { value as Self } }
impl Fromf64 for u64 { fn fromf64(value: f64) -> Self { value as Self } }
impl Fromf64 for i64 { fn fromf64(value: f64) -> Self { value as Self } }
impl Fromf64 for i32 { fn fromf64(value: f64) -> Self { value as Self } }
impl Fromf64 for i16 { fn fromf64(value: f64) -> Self { value as Self } }

fn test<T>(value: f64) -> T::OutType
    where
        T: TestEnum,
        T::OutType: TestEnum + Fromf64
{
    <T::OutType>::fromf64(value)
}

fn main() {
    let a = 1.0;
    let b = test::<i32>(a);
    println!("b is {:?}", type_of(b));
    let c = test::<i64>(a);
    println!("c is {:?}", type_of(c));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant