operator += in Covers

29 views
Skip to first unread message

david.h...@gmail.com

unread,
Aug 21, 2015, 6:55:06 AM8/21/15
to ooc-lang
//testfile.ooc

Foo: cover {
number: Int
init: func@ (=number)
}
// version 1
operator += (left: Foo@, right: Foo) { left number += right number }
// version 2
operator -= (left: Foo, right: Foo) -> Foo { Foo new(left number -= right number) }

foo := Foo new(1)
bar := Foo new(2)
foo += bar
foo number toString() println()

Version 2 results in the number 1 being printed, and not 3. This is, perhaps, the expected behavior, though.

Version 1 gives me the error

/home/dhesselbom/versioned/rock/./testfile.ooc: In function ‘___testfile_load’:
/home/dhesselbom/versioned/rock/./testfile.ooc:9:9: error: incompatible type for argument 1 of ‘___testfile____OP_ADD_ASS_Foo__star_Foo’
foo += bar
^
In file included from .libs/ooc/./testfile/./testfile.h:7:0,
from rock_tmp/ooc/./testfile/./testfile.c:3:
.libs/ooc/./testfile/./testfile-fwd.h:33:6: note: expected ‘struct ___testfile__Foo *’ but argument is of type ‘___testfile__Foo’
void ___testfile____OP_ADD_ASS_Foo__star_Foo(___testfile__Foo* left, ___testfile__Foo right);
^
C compiler failed on module ./testfile from ./testfile, bailing out

So apparently I'm doing something wrong here, but I'm *positive* this used to work in older versions of rock (unfortunately, I can't verify it now since I'm unable to build anything older than v0.9.10 for various reasons).

So, is either the correct way to implement the += operator for covers, currently?

david.h...@gmail.com

unread,
Aug 21, 2015, 6:56:18 AM8/21/15
to ooc-lang, david.h...@gmail.com
Correction:

// version 2
operator += (left: Foo, right: Foo) -> Foo { Foo new(left number += right number) }

But yeah, still doesn't work.

House Zet

unread,
Aug 21, 2015, 7:39:00 PM8/21/15
to ooc-lang, david.h...@gmail.com
+=, -=.. will be unwrapped to x = a + b by compiler, so just define + operator




 /// this works

Foo: cover {
        number: Int
        init: func@ (=number)
}
// version 1
operator + (left: Foo, right: Foo) -> Foo { Foo new(left number += right number) }
// version 2
operator - (left: Foo, right: Foo) -> Foo { Foo new(left number -= right number) }


foo := Foo new(1)
bar := Foo new(2)
foo += bar
foo number toString() println()


David Hesselbom

unread,
Aug 21, 2015, 7:45:11 PM8/21/15
to House Zet, ooc-lang
Ahh... Alright! So why is it implemented for ArrayList, and the + operator isn't? Are you ever supposed to implement the += operator explicitly?

House Zet

unread,
Aug 21, 2015, 8:30:17 PM8/21/15
to ooc-lang, zhai...@gmail.com, david.h...@gmail.com
+= can be implemented explicitly, but only works for classes now
Covers are passed by value so you can't change its content.

I notice that you make the first argument passed by reference, but it is not allowed in current rock.
`left += right` is always translated to `op_ass(left, right)` so you get the gcc errors.
Reply all
Reply to author
Forward
0 new messages