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

ELI5: is optional behavior #2601

Open
Morriar opened this issue Dec 21, 2017 · 0 comments
Open

ELI5: is optional behavior #2601

Morriar opened this issue Dec 21, 2017 · 0 comments

Comments

@Morriar
Copy link
Member

Morriar commented Dec 21, 2017

Two questions in one.

First when redefining an optional attribute:

class A
	var foo: String = "foo" is optional
end

class B
	super A

	redef var foo = "bar" # is optional
end

Without the optional annotation on the redef, the compiler will return:

file.nit:8,12--14: Redef Error: expected `nullable String` type for parameter `foo'; got `String`.
		redef var foo = "bar"
		          ^

With the annotation, no problem.

I'm not sure about the spec here. Is the annotation really needed on the redef? If yes, shouldn't the error message be more explicative? Either the spec or the compiler is wrong here.

Then when setting the default value of optional (with the annotation obviously):

var b = new B("ba")
print b.foo

I expected the previous piece of code to display ba but it display bar.
So the attribute is allowed as optional in the constructor but the passed value is never assigned.
Here again, I'm not sure about the spec of this annotation.

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

No branches or pull requests

1 participant