Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Overload some operators #375

Closed
jeromesimeon opened this issue Aug 6, 2018 · 4 comments
Closed

Overload some operators #375

jeromesimeon opened this issue Aug 6, 2018 · 4 comments

Comments

@jeromesimeon
Copy link
Member

Now that we have a proper type checker, we could consider overloading some of the operators and consolidate the syntax for those. Some prime candidates:

// Arithmetics
1 +i 1 => 1 + 1
1.0 + 1.0 => 1.0 + 1.0

// Comparisons
1 <i 2 => 1 < 2
1.0 < 2.0 => 1.0 < 2.0

// String concatenation
"hello " ++ "world!" => "hello " + "world!"

// Field access
AccordContractState{ stateId : "123" }.stateId
{ stateId : "123" }.stateId
@kach
Copy link
Contributor

kach commented Aug 8, 2018

I'm not sure about doing operator overloading for numbers just yet, because it can get confusinga. We should first sort out the numeric type situation, and then choose operators based on which numeric types will be most common.

In the meantime, if +i bothers you, we can do +. for float addition and + for integer addition, as in OCaml.

I think ++ is okay for string concatenation. Otherwise people will keep trying to do "The amount is: " + 10 and complaining about the error.

Field access can definitely be overloaded, possibly just by modifying the syntactic sugar? @jeromesimeon what's the top brand in the brand relation? I'm thinking compiling a.b to match a.b with let a : TopBrand then unbrand(a).b else a.b would work. This strategy would also fix #385 I think.

@jeromesimeon
Copy link
Member Author

About field access:

I don't think I can tell if it's working without knowing what the typing rule for match is... I thought match would work either on Brand as input type (match against type name) or on other values as input type.

@jeromesimeon
Copy link
Member Author

Some core support for overloaded operators is in place. It's quite conservative, doing static dispatch for clearly identified pairs of types:

Double + Double
Integer + Integer
etc.

Double >= Double
Integer >= Integer
etc.

-Integer
-Double

It also overloads .foo in order to access fields on either records or branded records (aka objects). See #385

Now that the infrastructure is in place, we can decide to expand on this or not based on user experience and feedback.

@jeromesimeon
Copy link
Member Author

Note: ++ is still used for string concatenation. (I personally quite like that it is not confused with + which is an arithmetic operator).

Possible other candidates for using operators include:

  • Arithmetics and comparisons on DateTime
  • arrayAdd and arraySubtract

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

No branches or pull requests

2 participants