RP2: LatestOption

Assax
Windoof-User
Windoof-User
Beiträge: 41
Registriert: 2. Dez 2011 10:38

RP2: LatestOption

Beitrag von Assax »

Hey everyone,

I have a question about the slide "Latesoption" in the RP2 set.
The description states that the latest value is either Some(value) or "None".
Reading up on the option type I found this:
Option[A] is a container for an optional value of type A. If the value of type A is present, the Option[A] is an instance of Some[A], containing the present value of type A. If the value is absent, the Option[A] is the object None
My question is, should it not be:

Code: Alles auswählen

assert(s.get == Some(1))
instead of

Code: Alles auswählen

assert(s.get==Option(1))
Thanks :)

Banashri
Windoof-User
Windoof-User
Beiträge: 25
Registriert: 19. Dez 2013 02:55

Re: RP2: LatestOption

Beitrag von Banashri »

No. Not necessary.
You can have a look into this code snippet.

Code: Alles auswählen


/** conversion function: latestOption **/
    val e4 = new ImperativeEvent[Int]
    val s4: Signal[Option[Int]] = e4.latestOption
    
    e4(1)
    println("s4: " + s4.get) // prints Some(1)
    assert(s4.get == Some(1)) // true
    assert(s4.get == Option(1)) // true

salvaneschi
Mausschubser
Mausschubser
Beiträge: 49
Registriert: 29. Mär 2013 23:51

Re: RP2: LatestOption

Beitrag von salvaneschi »

Right. Essentially Option(1) builds Some(1) and Option(null) builds a None.
You may argue that it is more explicit and therefore a better programming practice to avoid Option(null) in favor of None, though.

Antworten

Zurück zu „Archiv“