DEV Community


Would `repr` keyword fit into a C-like language (Janky)?

baenencalin profile image Calin Baenen ・1 min read

I was wondering if a repr keyword would work well in my C-like language, Janky.

Here's an example:

public class Character {
    private final repr char c;
    public Character(char c) this.c = c; // Don't mind the fact there aren't braces. This is prototype (and shorthand) syntax.
Enter fullscreen mode Exit fullscreen mode

Basically, the idea is that it lets you represent your class as another datatype (primitive or class) regardless of whether it extends from that type or not.

This can also be used with methods (though, said "repr-methods" mustn't take any arguments, adding parameters to the repr will cause an error):

public final class Random {
    private int seed = 0;
    private final int[] nums = {
        0, 5, 2, 1, 6, 3, 7, 0, 10, 9, -1, 5
    public repr int nextInt() {
        final int comp = try nums[seed] catch -4;
        final int ns =  Math.round(
            Math.dmxbmn(seed, nums[-1])
        )+comp; seed = ns;
        return ns;
Enter fullscreen mode Exit fullscreen mode

So, this Random class will return a (seemingly) random int every time nextInt() is called. And, as repr makes it out, whenever you do Random r = new Random(); 100+r; the 100+r becomes short for 100+r.nextInt().

Of course, you get the same result when passing r to a method that takes an int.

So... What do you think?
Is this a good or a bad keyword idea? Useful or useless (or maybe in between)?

Let me know your thoughts.


Discussion (0)

Editor guide