Skip to content
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

Operator Overloading within a class #33

Closed
bruxisma opened this issue Apr 27, 2010 · 4 comments
Closed

Operator Overloading within a class #33

bruxisma opened this issue Apr 27, 2010 · 4 comments
Labels
Milestone

Comments

@bruxisma
Copy link

Mostly a reminder to nddrylliog that class should have operator overloading internally for instance

operator + (right: Bignum) -> Bignum {
    this + right
}
@nddrylliog
Copy link
Member

Note that your operator definition is recursive. Compile with -O2 and.. infinite loop o/

@solson
Copy link

solson commented Jul 31, 2010

Yeah, silly example. :P

But I also really want this. It's nice to put operator overloads inside the class they actually belong to.

@nddrylliog
Copy link
Member

Well, I have nothing against implementing this, but there will be a slight difference in semantics from the 'operator overloading outside of class' way.

Why? Easy.

Operator overloads outside a type definition (not necessarily only a class) allow to add operator overloads to any type. And I mean any. Especially if it's defined in completely another module, and especially if the type is defined in a library which you can't change the source (that can and will happen eventually, with the upcoming libs format allowing separate compilation)

The problem is that, if you do the following:

  • Define a 'List' class in structs/List
  • Define operator [] for List in structs/List
  • Define an 'ArrayList' class in structs/ArrayList

...then if you only import ArrayList, you won't get operator overloading for it. Why does that happen? Well, rock can't take into account the operator oveloading in List, otherwise - how could it choose? There might be operator overloads for some super-class of ArrayList in an other module that ArrayList imports. It would be messy and thus, that's not how it works.

It's handy sometimes, because it allows you to override existing operator overloads if you don't like them. If you want to monitor ArrayList accesses in your application, just define an operator [] to ArrayList and import it everywhere - if it's imported after List/ArrayList, it'll be chosen instead (or at least should be - report a bug if it's not.)

So now, how will that be handled with operator overloads in class? Well operator overloads in class should, in my opinion, behave different: ie. if you define operator overloads within the List class, then only impoting ArrayList should give you those operator overloads, ie. member operator overloads would be inherited.

That may be seen as a small difference for the day-to-day user but important enough that it should be discussed on this issue imho. If everyone agrees to that behavior then we'll be able to get started on the implementation.

@nddrylliog
Copy link
Member

Woop! #583 is a duplicated and has been closed in the 'templates' branch - will be in 0.9.6

nddrylliog added a commit that referenced this issue Feb 14, 2013
…ter). Still no inheritance.. will see about that laterz.
alexnask pushed a commit to alexnask/rock that referenced this issue Mar 10, 2016
Added variable and property support (obfuscation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants