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

Option sugar #297

Open
JonathanAldrich opened this issue Feb 23, 2019 · 2 comments
Open

Option sugar #297

JonathanAldrich opened this issue Feb 23, 2019 · 2 comments

Comments

@JonathanAldrich
Copy link
Member

See the design at https://github.com/wyvernlang/wyvern/wiki/Sugar-for-Options

@sychoo
Copy link
Contributor

sychoo commented Oct 2, 2019

An option type sugar design of Kotlin can be found in https://kotlinlang.org/docs/reference/null-safety.html

@JonathanAldrich
Copy link
Member Author

Pull request #359 covers the following:

  • 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].

Still to implement:

  • 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())

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

2 participants