differential-datalog icon indicating copy to clipboard operation
differential-datalog copied to clipboard

Automatic string conversion for type variables

Open ryzhyk opened this issue 6 years ago • 2 comments

At the moment, the compiler refuses to convert type variables to strings.

This can be fixed by:

  1. Infering that a function expects a type variable to be printable
  2. In the generated Rust function, add the Display trait for this function
  3. Whenever the function is called, check that its concrete arguments satisfy the trait
  4. Implement Display trait for types that have a user-defined 2String method

ryzhyk avatar May 08 '18 18:05 ryzhyk

Here's a function that I wrote that will compile to Rust, then won't compile at that point. I think it's exactly this issue:

 function stringize_and_join(vec: Vec<'A>, sep: string): string {
    var strings: Vec<string> = vec_with_capacity(vec_len(vec));
    for (x in vec) {
        vec_push(strings, __builtin_2string(x))
    };
    string_join(strings, sep)
}

blp avatar Apr 06 '20 03:04 blp

Yes, exactly. There is no easy fix at the moment. We don't have the notion of traits or interfaces or anything like that, so there is no way to define a function that only accepts printable arguments.

ryzhyk avatar Apr 06 '20 03:04 ryzhyk