Skip to content

ELI5: is optional behavior #2601

Description

@Morriar

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.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions