Here's a quirk of sealed classes I found while tes...
# announcements
v
Here's a quirk of sealed classes I found while testing - given
sealed class PostGeneration
, implemented by
class Error : PostGeneration()
and
class Post : PostGeneration()
, and a call to a method which returns
PostGeneration
, the compiler naturally enough doesn't know if it's a
Post
or an
Error
. But as soon as I introduce a new variable through a cast, the compiler now knows the class of the result of the function call:
Copy code
val result = Post.Builder.createPost(fileName,mDocument,project)
	val isPost = result is Post
	assertTrue(isPost)

	val post = result as Post            // _if I comment this out, then result.tags is undefined and the compiler rejects it. But I don't need to use this "post" variable so long as it is defined. Defining "post" is enough for the compiler to know what to do with "result"._
	assertEquals(1,result.tags.size)
	assertEquals("aTag",post.tags[0])