Skip to content

Instantly share code, notes, and snippets.

@TheBuzzSaw
Last active December 18, 2015 04:39
Show Gist options
  • Save TheBuzzSaw/5726900 to your computer and use it in GitHub Desktop.
Save TheBuzzSaw/5726900 to your computer and use it in GitHub Desktop.
This is my first attempt at openly imagining my finished language (which I have temporarily dubbed K++). I've done it 100 times in my head, but I need to see it in reality.
import Core; // Bring in standard library.
export Sample; // Set the namespace/package for the code in this file.
class Rational
{
// Like C#, constant values are inherently 'static'.
public constant int32 MinDenominator = 1;
// Class member variables are always all private.
int32 _numerator = 0;
int32 _denominator = MinDenominator;
// Properties similar to C# or Objective-C.
public get int32 Numerator { return _numerator; }
public get int32 Denominator { return _denominator; }
// Why does the traditional constructor match the class name?
// No more of that. Behold, the 'constructor' keyword!
public constructor()
{
}
public constructor(int32 numerator, int32 denominator)
{
_numerator = numerator;
_denominator = Math.Max(MinDenominator, denominator);
}
public constructor(immutable Rational& other)
{
_numerator = other._numerator;
_denominator = other._denominator;
}
public destructor
{
}
// Defining a cast should be fairly straightforward.
// Does it need any more than this?
explicit cast float
{
return (float)_numerator / (float)_denominator;
}
// By being implicit, one can simply check "if (rational)".
implicit cast bool
{
return _numerator != 0;
}
// After much deliberation, I believe operator overloading
// needs to exist.
public int32 operator*(int32 operand)
{
return operand * _numerator / _denominator;
}
}
// I love the C# 'params' concept, but I dislike the name.
// I will probably think up a new keyword.
int32 Sum(params int32[] values)
{
int32 result = 0;
// Can the type for 'value' be anticipated?
foreach (int32 value in values)
result += value;
return result;
}
int32 Main(String[] arguments)
{
Rational rational(22, 7); // Init to approximated pi.
int32 n = 7;
int32 result = rational * n; // Answer should be 22!
StandardOut.Write(result).Write(NewLine);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment