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

Supported Option Type Syntactic Sugar #359

Merged
merged 7 commits into from
Nov 9, 2019

Conversation

sychoo
Copy link
Contributor

@sychoo sychoo commented Nov 1, 2019

Working on option type syntactic sugar. Referred to null safety in Kotlin.

  • Supported T? as the sugar for option.Option[T].
  • Supported a built-in None value that works for all T.
    • NONE = option.None[Dyn] <: option.None[T] for all T.
    • Note that NONE is equivalent to option.None[Dyn].

@sychoo sychoo changed the title Support Option Type Syntactic Sugar Supported Option Type Syntactic Sugar Nov 1, 2019
Copy link
Member

@JonathanAldrich JonathanAldrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Simon, did you test this? Can you include a test case in the PR?

@sychoo
Copy link
Contributor Author

sychoo commented Nov 3, 2019

Hi Simon, did you test this? Can you include a test case in the PR?

Hi Jonathan, I have tested the features I have implemented, and I have added a test case in the example folder. There are still several other features to be supported:

  • The implicit conversion from T to T?.
    // this will convert "Hello" to option.Some[String]("Hello").
    val str: String? = "Hello"
  • Support "!" operator that explicitly convert T? to T, raise an error if str is NONE.
    // this will convert type of str from "String?" to the type of str2 "String".
    val str2: String = str!
  • Propagate None using ?. operator.
    type Nested
       val str: String?
    
    val n1: Nested? = new
       val str: String? = "Hello"
    
    // this will print "Hello" successfully.
    stdout.print(n1?.str.get())
    
    val n2: Nested? = NONE
    
    // this will print "NONE", since n2 is of NONE type, "?." operator is used to propagate NONE.
    stdout.print(n2?.str.get())
    
    val n3: Nested? = new
       val str: String? = NONE
    
    // this will print "NONE, since n3.str is of NONE type, "?." operator is used to propagate NONE.
    stdout.print(n3?.str.get())

@JonathanAldrich JonathanAldrich merged commit caace7c into wyvernlang:master Nov 9, 2019
@JonathanAldrich JonathanAldrich mentioned this pull request Nov 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants